Skip to content

Commit 6d922bf

Browse files
committed
try to improve type safety in JavaTypeRegistry and MutableMutabilityPlant
1 parent 6bb96fc commit 6d922bf

File tree

8 files changed

+189
-143
lines changed

8 files changed

+189
-143
lines changed

hibernate-core/src/main/java/org/hibernate/dialect/function/array/DdlTypeHelper.java

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,28 @@
77
import java.lang.reflect.Type;
88
import java.util.List;
99

10-
import org.hibernate.dialect.Dialect;
1110
import org.hibernate.engine.jdbc.Size;
12-
import org.hibernate.internal.build.AllowReflection;
1311
import org.hibernate.metamodel.mapping.JdbcMappingContainer;
1412
import org.hibernate.metamodel.mapping.SqlTypedMapping;
1513
import org.hibernate.metamodel.model.domain.DomainType;
1614
import org.hibernate.metamodel.model.domain.ReturnableType;
1715
import org.hibernate.sql.ast.spi.AbstractSqlAstTranslator;
1816
import org.hibernate.type.BasicType;
1917
import org.hibernate.type.descriptor.java.BasicPluralJavaType;
20-
import org.hibernate.type.descriptor.sql.DdlType;
21-
import org.hibernate.type.descriptor.sql.spi.DdlTypeRegistry;
2218
import org.hibernate.type.internal.ParameterizedTypeImpl;
2319
import org.hibernate.type.spi.TypeConfiguration;
2420

2521
public class DdlTypeHelper {
2622
@SuppressWarnings("unchecked")
27-
@AllowReflection
23+
// @AllowReflection
2824
public static BasicType<?> resolveArrayType(DomainType<?> elementType, TypeConfiguration typeConfiguration) {
29-
@SuppressWarnings("unchecked")
3025
final var arrayJavaType =
3126
(BasicPluralJavaType<Object>)
3227
typeConfiguration.getJavaTypeRegistry()
3328
.resolveArrayDescriptor( elementType.getJavaType() );
34-
final Dialect dialect = typeConfiguration.getCurrentBaseSqlTypeIndicators().getDialect();
3529
return arrayJavaType.resolveType(
3630
typeConfiguration,
37-
dialect,
31+
typeConfiguration.getCurrentBaseSqlTypeIndicators().getDialect(),
3832
(BasicType<Object>) elementType,
3933
null,
4034
typeConfiguration.getCurrentBaseSqlTypeIndicators()
@@ -43,7 +37,6 @@ public static BasicType<?> resolveArrayType(DomainType<?> elementType, TypeConfi
4337

4438
@SuppressWarnings("unchecked")
4539
public static BasicType<?> resolveListType(DomainType<?> elementType, TypeConfiguration typeConfiguration) {
46-
@SuppressWarnings("unchecked")
4740
final BasicPluralJavaType<Object> arrayJavaType =
4841
(BasicPluralJavaType<Object>)
4942
typeConfiguration.getJavaTypeRegistry()
@@ -52,10 +45,9 @@ public static BasicType<?> resolveListType(DomainType<?> elementType, TypeConfig
5245
new ParameterizedTypeImpl( List.class, new Type[]{ elementType.getJavaType() }, null ),
5346
typeConfiguration
5447
);
55-
final Dialect dialect = typeConfiguration.getCurrentBaseSqlTypeIndicators().getDialect();
5648
return arrayJavaType.resolveType(
5749
typeConfiguration,
58-
dialect,
50+
typeConfiguration.getCurrentBaseSqlTypeIndicators().getDialect(),
5951
(BasicType<Object>) elementType,
6052
null,
6153
typeConfiguration.getCurrentBaseSqlTypeIndicators()
@@ -79,11 +71,9 @@ public static String getTypeName(JdbcMappingContainer type, Size size, TypeConfi
7971
return AbstractSqlAstTranslator.getSqlTypeName( sqlTypedMapping, typeConfiguration );
8072
}
8173
else {
82-
final BasicType<?> basicType = (BasicType<?>) type.getSingleJdbcMapping();
83-
final DdlTypeRegistry ddlTypeRegistry = typeConfiguration.getDdlTypeRegistry();
84-
final DdlType ddlType = ddlTypeRegistry.getDescriptor(
85-
basicType.getJdbcType().getDdlTypeCode()
86-
);
74+
final var basicType = (BasicType<?>) type.getSingleJdbcMapping();
75+
final var ddlTypeRegistry = typeConfiguration.getDdlTypeRegistry();
76+
final var ddlType = ddlTypeRegistry.getDescriptor( basicType.getJdbcType().getDdlTypeCode() );
8777
return ddlType.getTypeName( size, basicType, ddlTypeRegistry );
8878
}
8979
}
@@ -97,11 +87,9 @@ public static String getTypeName(ReturnableType<?> type, Size size, TypeConfigur
9787
return AbstractSqlAstTranslator.getSqlTypeName( sqlTypedMapping, typeConfiguration );
9888
}
9989
else {
100-
final BasicType<?> basicType = (BasicType<?>) ( (JdbcMappingContainer) type ).getSingleJdbcMapping();
101-
final DdlTypeRegistry ddlTypeRegistry = typeConfiguration.getDdlTypeRegistry();
102-
final DdlType ddlType = ddlTypeRegistry.getDescriptor(
103-
basicType.getJdbcType().getDdlTypeCode()
104-
);
90+
final var basicType = (BasicType<?>) ( (JdbcMappingContainer) type ).getSingleJdbcMapping();
91+
final var ddlTypeRegistry = typeConfiguration.getDdlTypeRegistry();
92+
final var ddlType = ddlTypeRegistry.getDescriptor( basicType.getJdbcType().getDdlTypeCode() );
10593
return ddlType.getTypeName( size, basicType, ddlTypeRegistry );
10694
}
10795
}
@@ -123,11 +111,9 @@ public static String getCastTypeName(JdbcMappingContainer type, Size size, TypeC
123111
return AbstractSqlAstTranslator.getCastTypeName( sqlTypedMapping, typeConfiguration );
124112
}
125113
else {
126-
final BasicType<?> basicType = (BasicType<?>) type.getSingleJdbcMapping();
127-
final DdlTypeRegistry ddlTypeRegistry = typeConfiguration.getDdlTypeRegistry();
128-
final DdlType ddlType = ddlTypeRegistry.getDescriptor(
129-
basicType.getJdbcType().getDdlTypeCode()
130-
);
114+
final var basicType = (BasicType<?>) type.getSingleJdbcMapping();
115+
final var ddlTypeRegistry = typeConfiguration.getDdlTypeRegistry();
116+
final var ddlType = ddlTypeRegistry.getDescriptor( basicType.getJdbcType().getDdlTypeCode() );
131117
return ddlType.getCastTypeName( size, basicType, ddlTypeRegistry );
132118
}
133119
}
@@ -141,11 +127,9 @@ public static String getCastTypeName(ReturnableType<?> type, Size size, TypeConf
141127
return AbstractSqlAstTranslator.getCastTypeName( sqlTypedMapping, typeConfiguration );
142128
}
143129
else {
144-
final BasicType<?> basicType = (BasicType<?>) ( (JdbcMappingContainer) type ).getSingleJdbcMapping();
145-
final DdlTypeRegistry ddlTypeRegistry = typeConfiguration.getDdlTypeRegistry();
146-
final DdlType ddlType = ddlTypeRegistry.getDescriptor(
147-
basicType.getJdbcType().getDdlTypeCode()
148-
);
130+
final var basicType = (BasicType<?>) ( (JdbcMappingContainer) type ).getSingleJdbcMapping();
131+
final var ddlTypeRegistry = typeConfiguration.getDdlTypeRegistry();
132+
final var ddlType = ddlTypeRegistry.getDescriptor( basicType.getJdbcType().getDdlTypeCode() );
149133
return ddlType.getCastTypeName( size, basicType, ddlTypeRegistry );
150134
}
151135
}

hibernate-core/src/main/java/org/hibernate/dialect/function/array/JsonArrayViaElementArgumentReturnTypeResolver.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
import java.util.List;
88
import java.util.function.Supplier;
99

10-
import org.hibernate.internal.build.AllowReflection;
1110
import org.hibernate.metamodel.mapping.BasicValuedMapping;
12-
import org.hibernate.metamodel.mapping.MappingModelExpressible;
1311
import org.hibernate.metamodel.model.domain.DomainType;
1412
import org.hibernate.metamodel.model.domain.ReturnableType;
1513
import org.hibernate.query.sqm.produce.function.FunctionReturnTypeResolver;
@@ -20,7 +18,6 @@
2018
import org.hibernate.type.SqlTypes;
2119
import org.hibernate.type.descriptor.java.BasicPluralJavaType;
2220
import org.hibernate.type.descriptor.jdbc.DelegatingJdbcTypeIndicators;
23-
import org.hibernate.type.descriptor.jdbc.JdbcTypeIndicators;
2421
import org.hibernate.type.spi.TypeConfiguration;
2522

2623
import org.checkerframework.checker.nullness.qual.Nullable;
@@ -44,12 +41,12 @@ public ReturnableType<?> resolveFunctionReturnType(
4441
TypeConfiguration typeConfiguration) {
4542
if ( converter != null ) {
4643
if ( converter.isInTypeInference() ) {
47-
// Don't default to a Json array when in type inference mode.
44+
// Don't default to a JSON array when in type inference mode.
4845
// Comparing e.g. `array() = (select array_agg() ...)` will trigger this resolver
4946
// while inferring the type for `array()`, which we want to avoid.
5047
return null;
5148
}
52-
final MappingModelExpressible<?> inferredType = converter.resolveFunctionImpliedReturnType();
49+
final var inferredType = converter.resolveFunctionImpliedReturnType();
5350
if ( inferredType != null ) {
5451
if ( inferredType instanceof ReturnableType<?> returnableType ) {
5552
return returnableType;
@@ -62,8 +59,8 @@ else if ( inferredType instanceof BasicValuedMapping basicValuedMapping ) {
6259
if ( impliedType != null ) {
6360
return impliedType;
6461
}
65-
for ( SqmTypedNode<?> argument : arguments ) {
66-
final DomainType<?> sqmType = argument.getExpressible().getSqmType();
62+
for ( var argument : arguments ) {
63+
final var sqmType = argument.getExpressible().getSqmType();
6764
if ( sqmType instanceof ReturnableType<?> ) {
6865
return resolveJsonArrayType( sqmType, typeConfiguration );
6966
}
@@ -78,14 +75,14 @@ public BasicValuedMapping resolveFunctionReturnType(
7875
return null;
7976
}
8077

81-
@AllowReflection
78+
// @AllowReflection
8279
public static <T> BasicType<?> resolveJsonArrayType(DomainType<T> elementType, TypeConfiguration typeConfiguration) {
8380
@SuppressWarnings("unchecked")
8481
final var arrayJavaType =
8582
(BasicPluralJavaType<T>)
8683
typeConfiguration.getJavaTypeRegistry()
8784
.resolveArrayDescriptor( elementType.getJavaType() );
88-
final JdbcTypeIndicators currentBaseSqlTypeIndicators = typeConfiguration.getCurrentBaseSqlTypeIndicators();
85+
final var currentBaseSqlTypeIndicators = typeConfiguration.getCurrentBaseSqlTypeIndicators();
8986
return arrayJavaType.resolveType(
9087
typeConfiguration,
9188
currentBaseSqlTypeIndicators.getDialect(),

hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/MappingMetamodelImpl.java

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.hibernate.metamodel.model.domain.JpaMetamodel;
3636
import org.hibernate.metamodel.model.domain.ManagedDomainType;
3737
import org.hibernate.metamodel.model.domain.NavigableRole;
38+
import org.hibernate.query.sqm.tree.domain.SqmManagedDomainType;
3839
import org.hibernate.type.BindingContext;
3940
import org.hibernate.query.sqm.tuple.TupleType;
4041
import org.hibernate.metamodel.model.domain.spi.JpaMetamodelImplementor;
@@ -113,12 +114,12 @@ public class MappingMetamodelImpl
113114
// - ignoring Hibernate's representation mode (entity mode), the Class
114115
// object for an entity (or mapped superclass) always refers to the same
115116
// JPA EntityType and Hibernate EntityPersister. The problem arises with
116-
// embeddables. For an embeddable, as with the rest of its metamodel,
117+
// embeddables. For an embeddable type, as with the rest of its metamodel,
117118
// Hibernate combines the embeddable's relational/mapping while JPA does
118119
// not. This is perfectly consistent with each paradigm. But it results
119120
// in a mismatch since JPA expects a single "type descriptor" for a
120-
// given embeddable class while Hibernate incorporates the
121-
// relational/mapping info so we have a "type descriptor" for each usage
121+
// given embeddable class, while Hibernate incorporates the
122+
// relational/mapping info, so we have a "type descriptor" for each usage
122123
// of that embeddable type. (Think embeddable versus embedded.)
123124
//
124125
// To account for this, we track both paradigms here.
@@ -258,11 +259,9 @@ private void processBootCollections(
258259
for ( final var model : collectionBindings ) {
259260
final String role = model.getRole();
260261
final var persister =
261-
persisterFactory.createCollectionPersister(
262-
model,
262+
persisterFactory.createCollectionPersister( model,
263263
cacheImplementor.getCollectionRegionAccess( new NavigableRole( role ) ),
264-
modelCreationContext
265-
);
264+
modelCreationContext );
266265
collectionPersisterMap.put( role, persister );
267266
if ( persister.getIndexType() instanceof org.hibernate.type.EntityType entityType ) {
268267
registerEntityParticipant( entityType, persister );
@@ -360,32 +359,29 @@ public boolean isEntityClass(Class<?> entityJavaType) {
360359

361360
@Override
362361
public EntityPersister getEntityDescriptor(Class<?> entityJavaType) {
363-
var entityPersister = entityPersisterMap.get( entityJavaType.getName() );
364-
if ( entityPersister == null ) {
365-
final String mappedEntityName = entityProxyInterfaceMap.get( entityJavaType );
366-
if ( mappedEntityName != null ) {
367-
entityPersister = entityPersisterMap.get( mappedEntityName );
368-
}
369-
}
370-
if ( entityPersister == null ) {
371-
throw new UnknownEntityTypeException( entityJavaType );
372-
}
373-
return entityPersister;
362+
return getEntityPersister( entityJavaType );
374363
}
375364

376365
@Override @Deprecated(forRemoval = true) @SuppressWarnings( "removal" )
377366
public EntityPersister locateEntityDescriptor(Class<?> byClass) {
378-
var entityPersister = entityPersisterMap.get( byClass.getName() );
379-
if ( entityPersister == null ) {
367+
return getEntityPersister( byClass );
368+
}
369+
370+
private EntityPersister getEntityPersister(Class<?> byClass) {
371+
final var entityPersister = entityPersisterMap.get( byClass.getName() );
372+
if ( entityPersister != null ) {
373+
return entityPersister;
374+
}
375+
else {
380376
final String mappedEntityName = entityProxyInterfaceMap.get( byClass );
381377
if ( mappedEntityName != null ) {
382-
entityPersister = entityPersisterMap.get( mappedEntityName );
378+
final var persister = entityPersisterMap.get( mappedEntityName );
379+
if ( persister != null ) {
380+
return persister;
381+
}
383382
}
384-
}
385-
if ( entityPersister == null ) {
386383
throw new UnknownEntityTypeException( byClass );
387384
}
388-
return entityPersister;
389385
}
390386

391387
@Override
@@ -644,15 +640,15 @@ public String[] getAllCollectionRoles() {
644640
public <T> @Nullable BindableType<T> resolveParameterBindType(Class<T> javaType) {
645641
final var typeConfiguration = getTypeConfiguration();
646642

647-
final BasicType<T> basicType = typeConfiguration.getBasicTypeForJavaType( javaType );
643+
final var basicType = typeConfiguration.getBasicTypeForJavaType( javaType );
648644
// For enums, we simply don't know the exact mapping if there is no basic type registered
649645
if ( basicType != null || javaType.isEnum() ) {
650646
return basicType;
651647
}
652648

653649
final var managedType = jpaMetamodel.findManagedType( javaType );
654650
if ( managedType != null ) {
655-
return (BindableType<T>) managedType;
651+
return (SqmManagedDomainType<T>) managedType;
656652
}
657653

658654
final var javaTypeRegistry = typeConfiguration.getJavaTypeRegistry();
@@ -685,12 +681,12 @@ public String[] getAllCollectionRoles() {
685681
return null;
686682
}
687683

688-
final Class<T> clazz = unproxiedClass( bindValue );
689-
690-
// Resolve superclass bindable type if necessary, as we don't register types for e.g. Inet4Address
684+
final var clazz = unproxiedClass( bindValue );
685+
// Resolve the superclass bindable type if necessary,
686+
// as we don't register types for e.g. Inet4Address
691687
Class<? super T> c = clazz;
692688
do {
693-
final BindableType<? super T> type = resolveParameterBindType( c );
689+
final var type = resolveParameterBindType( c );
694690
if ( type != null ) {
695691
return type;
696692
}

hibernate-core/src/main/java/org/hibernate/type/AbstractStandardBasicType.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ public final void beforeAssemble(Serializable cached, SharedSessionContractImple
289289
public final Object replace(Object original, Object target, SharedSessionContractImplementor session, Object owner, Map<Object, Object> copyCache) {
290290
return original == null && target == null
291291
? null
292-
: javaType.getReplacement( javaType.cast( original ) , javaType.cast( target ) , session );
292+
: javaType.getReplacement( javaType.cast( original ), javaType.cast( target ), session );
293293

294294
}
295295

@@ -341,7 +341,7 @@ public void nullSafeSet(CallableStatement st, T value, String name, SharedSessio
341341
}
342342

343343
protected final void nullSafeSet(CallableStatement st, Object value, String name, WrapperOptions options) throws SQLException {
344-
getJdbcValueBinder().bind( st, javaType.cast( value ) , name, options );
344+
getJdbcValueBinder().bind( st, javaType.cast( value ), name, options );
345345
}
346346

347347
@Override
@@ -363,8 +363,7 @@ public CastType getCastType() {
363363
// and the cast type determination here. Note that we interpret the converter in ConvertedBasicTypeImpl
364364
// to properly determine the correct cast type
365365
final var jdbcType = getJdbcType();
366-
final int jdbcTypeCode = jdbcType.getDdlTypeCode();
367-
switch ( jdbcTypeCode ) {
366+
switch ( jdbcType.getDdlTypeCode() ) {
368367
case Types.BIT:
369368
case Types.SMALLINT:
370369
case Types.TINYINT:

hibernate-core/src/main/java/org/hibernate/type/descriptor/converter/internal/AttributeConverterBean.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,16 @@ private JavaType<O> getTypeDescriptor(
7474
private MutabilityPlan<O> getMutabilityPlan(
7575
ManagedBean<? extends AttributeConverter<O,R>> attributeConverterBean,
7676
JpaAttributeConverterCreationContext context) {
77-
final MutabilityPlan<O> mutabilityPlan =
77+
final var mutabilityPlan =
7878
RegistryHelper.INSTANCE.determineMutabilityPlan(
79-
attributeConverterBean.getBeanClass(),
79+
(java.lang.reflect.Type)
80+
attributeConverterBean.getBeanClass(),
8081
context.getTypeConfiguration()
8182
);
83+
//noinspection unchecked
8284
return mutabilityPlan == null
8385
? new AttributeConverterMutabilityPlan<>( this, true )
84-
: mutabilityPlan;
86+
: (MutabilityPlan<O>) mutabilityPlan;
8587
}
8688

8789
@Override

hibernate-core/src/main/java/org/hibernate/type/descriptor/java/MutableMutabilityPlan.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@
1515
*/
1616
public abstract class MutableMutabilityPlan<T> implements MutabilityPlan<T> {
1717

18-
public static final MutableMutabilityPlan<Object> INSTANCE = new MutableMutabilityPlan<>() {
18+
public static <T> MutableMutabilityPlan<T> instance() {
19+
//noinspection unchecked
20+
return INSTANCE;
21+
}
22+
23+
public static final MutableMutabilityPlan INSTANCE = new MutableMutabilityPlan<>() {
1924
@Override
2025
protected Object deepCopyNotNull(Object value) {
2126
return value;

0 commit comments

Comments
 (0)