Skip to content

Commit a283fc9

Browse files
committed
even code cleanups around MappingHelper and especially class instantiation
1 parent eb56902 commit a283fc9

File tree

15 files changed

+165
-340
lines changed

15 files changed

+165
-340
lines changed

hibernate-core/src/main/java/org/hibernate/boot/model/convert/internal/AbstractConverterDescriptor.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,9 @@ public AbstractConverterDescriptor(
4747
}
4848

4949
private AutoApplicableConverterDescriptor resolveAutoApplicableDescriptor(
50-
Class<? extends AttributeConverter> converterClass,
50+
Class<? extends AttributeConverter<?,?>> converterClass,
5151
Boolean forceAutoApply) {
5252
final boolean autoApply;
53-
5453
if ( forceAutoApply != null ) {
5554
// if the caller explicitly specified whether to auto-apply, honor that
5655
autoApply = forceAutoApply;

hibernate-core/src/main/java/org/hibernate/boot/model/internal/CollectionBinder.java

Lines changed: 9 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@
6969
import org.hibernate.annotations.SqlFragmentAlias;
7070
import org.hibernate.annotations.Synchronize;
7171
import org.hibernate.boot.model.IdentifierGeneratorDefinition;
72-
import org.hibernate.boot.model.TypeDefinition;
7372
import org.hibernate.boot.models.JpaAnnotations;
7473
import org.hibernate.boot.models.annotations.internal.JoinColumnJpaAnnotation;
7574
import org.hibernate.boot.models.annotations.internal.MapKeyColumnJpaAnnotation;
@@ -113,7 +112,6 @@
113112
import org.hibernate.usertype.CompositeUserType;
114113
import org.hibernate.usertype.UserCollectionType;
115114

116-
117115
import jakarta.persistence.Access;
118116
import jakarta.persistence.AttributeOverride;
119117
import jakarta.persistence.AttributeOverrides;
@@ -236,9 +234,6 @@ public abstract class CollectionBinder {
236234
private SortNatural naturalSort;
237235
private SortComparator comparatorSort;
238236

239-
private String explicitType;
240-
private final Map<String,String> explicitTypeParameters = new HashMap<>();
241-
242237
protected CollectionBinder(
243238
Supplier<ManagedBean<? extends UserCollectionType>> customTypeBeanResolver,
244239
boolean isSortedCollection,
@@ -855,17 +850,9 @@ private static CollectionBinder getCollectionBinder(
855850
final CollectionType typeAnnotation =
856851
property.getAnnotationUsage( CollectionType.class,
857852
buildingContext.getMetadataCollector().getSourceModelBuildingContext() );
858-
if ( typeAnnotation != null ) {
859-
binder = createBinderFromCustomTypeAnnotation( property, typeAnnotation, buildingContext );
860-
// todo (6.0) - technically, these should no longer be needed
861-
binder.explicitType = typeAnnotation.type().getName();
862-
for ( Parameter param : typeAnnotation.parameters() ) {
863-
binder.explicitTypeParameters.put( param.name(), param.value() );
864-
}
865-
}
866-
else {
867-
binder = createBinderAutomatically( property, buildingContext );
868-
}
853+
binder = typeAnnotation != null
854+
? createBinderFromCustomTypeAnnotation( property, typeAnnotation, buildingContext )
855+
: createBinderAutomatically( property, buildingContext );
869856
binder.setIsHibernateExtensionMapping( isHibernateExtensionMapping );
870857
return binder;
871858
}
@@ -1160,7 +1147,6 @@ private void bind() {
11601147
collection.setMappedByProperty( mappedBy );
11611148

11621149
checkMapKeyColumn();
1163-
bindExplicitTypes();
11641150
//set laziness
11651151
defineFetchingStrategy();
11661152
collection.setMutable( isMutable() );
@@ -1222,22 +1208,6 @@ private void bindCache() {
12221208
collection.setQueryCacheLayout( queryCacheLayout );
12231209
}
12241210

1225-
private void bindExplicitTypes() {
1226-
// set explicit type information
1227-
final InFlightMetadataCollector metadataCollector = getMetadataCollector();
1228-
if ( explicitType != null ) {
1229-
final TypeDefinition typeDef = metadataCollector.getTypeDefinition( explicitType );
1230-
if ( typeDef == null ) {
1231-
collection.setTypeName( explicitType );
1232-
collection.setTypeParameters( explicitTypeParameters );
1233-
}
1234-
else {
1235-
collection.setTypeName( typeDef.getTypeImplementorClass().getName() );
1236-
collection.setTypeParameters( typeDef.getParameters() );
1237-
}
1238-
}
1239-
}
1240-
12411211
private void detectMappedByProblem(boolean isMappedBy) {
12421212
if ( isMappedBy ) {
12431213
if ( property.hasDirectAnnotationUsage( JoinColumn.class )
@@ -1751,13 +1721,11 @@ private String toPhysicalName(String logicalName) {
17511721
}
17521722

17531723
private void bindFilters(boolean hasAssociationTable) {
1754-
property.forEachAnnotationUsage( Filter.class, sourceModelContext(), (usage) -> {
1755-
addFilter( hasAssociationTable, usage );
1756-
} );
1724+
property.forEachAnnotationUsage( Filter.class, sourceModelContext(),
1725+
usage -> addFilter( hasAssociationTable, usage ) );
17571726

1758-
property.forEachAnnotationUsage( FilterJoinTable.class, sourceModelContext(), (usage) -> {
1759-
addFilterJoinTable( hasAssociationTable, usage );
1760-
} );
1727+
property.forEachAnnotationUsage( FilterJoinTable.class, sourceModelContext(),
1728+
usage -> addFilterJoinTable( hasAssociationTable, usage ) );
17611729
}
17621730

17631731
private void addFilter(boolean hasAssociationTable, Filter filterAnnotation) {
@@ -2510,9 +2478,8 @@ private void handleOwnedManyToMany(PersistentClass collectionEntity, boolean isC
25102478
}
25112479

25122480
private void handleCheckConstraints(Table collectionTable) {
2513-
property.forEachAnnotationUsage( Check.class, sourceModelContext(), (usage) -> {
2514-
addCheckToCollection( collectionTable, usage );
2515-
} );
2481+
property.forEachAnnotationUsage( Check.class, sourceModelContext(),
2482+
usage -> addCheckToCollection( collectionTable, usage ) );
25162483
property.forEachAnnotationUsage( jakarta.persistence.JoinTable.class, sourceModelContext(), (usage) -> {
25172484
TableBinder.addTableCheck( collectionTable, usage.check() );
25182485
TableBinder.addTableComment( collectionTable, usage.comment() );

hibernate-core/src/main/java/org/hibernate/mapping/Any.java

Lines changed: 27 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.hibernate.metamodel.spi.ImplicitDiscriminatorStrategy;
1212
import org.hibernate.type.AnyType;
1313
import org.hibernate.type.MappingContext;
14+
import org.hibernate.type.MetaType;
1415
import org.hibernate.type.Type;
1516

1617
import java.util.HashMap;
@@ -46,7 +47,6 @@ public Any(MetadataBuildingContext buildingContext, Table table) {
4647

4748
public Any(MetadataBuildingContext buildingContext, Table table, boolean annotations) {
4849
super( buildingContext, table );
49-
5050
if ( ! annotations ) {
5151
metaMapping = new MetaValue( this::applySelectableToSuper, buildingContext, table );
5252
metaMapping.setTypeName( "string" );
@@ -56,7 +56,6 @@ public Any(MetadataBuildingContext buildingContext, Table table, boolean annotat
5656
metaMapping = null;
5757
keyMapping = null;
5858
}
59-
6059
}
6160

6261
public Any(Any original) {
@@ -85,15 +84,13 @@ public Any copy() {
8584
}
8685

8786
public void addSelectable(Selectable selectable) {
88-
if ( selectable == null ) {
89-
return;
90-
}
91-
92-
if ( selectable instanceof Column ) {
93-
super.justAddColumn( (Column) selectable );
94-
}
95-
else {
96-
super.justAddFormula( (Formula) selectable );
87+
if ( selectable != null ) {
88+
if ( selectable instanceof Column column ) {
89+
super.justAddColumn( column );
90+
}
91+
else if ( selectable instanceof Formula formula ) {
92+
super.justAddFormula( formula );
93+
}
9794
}
9895
}
9996

@@ -134,32 +131,12 @@ public void setIdentifierType(String identifierType) {
134131
@Override
135132
public AnyType getType() throws MappingException {
136133
if ( resolvedType == null ) {
137-
final Type discriminatorType;
138-
if ( discriminatorDescriptor != null ) {
139-
discriminatorType = discriminatorDescriptor.getType();
140-
}
141-
else {
142-
discriminatorType = metaMapping.getType();
143-
}
144-
145-
final Type identifierType;
146-
if ( keyDescriptor != null ) {
147-
identifierType = keyDescriptor.getType();
148-
}
149-
else {
150-
identifierType = keyMapping.getType();
151-
}
152-
153-
resolvedType = MappingHelper.anyMapping(
154-
discriminatorType,
155-
identifierType,
156-
metaValueToEntityNameMap,
157-
implicitValueStrategy,
158-
isLazy(),
159-
getBuildingContext()
160-
);
134+
final Type discriminatorType =
135+
discriminatorDescriptor != null ? discriminatorDescriptor.getType() : metaMapping.getType();
136+
final Type identifierType = keyDescriptor != null ? keyDescriptor.getType() : keyMapping.getType();
137+
final MetaType metaType = new MetaType( discriminatorType, implicitValueStrategy, metaValueToEntityNameMap );
138+
resolvedType = new AnyType( getTypeConfiguration(), metaType, identifierType, isLazy() );
161139
}
162-
163140
return resolvedType;
164141
}
165142

@@ -181,7 +158,6 @@ public void addFormula(Formula formula) {
181158
private void applySelectableLocally(Selectable selectable) {
182159
// note: adding column to meta or key mapping ultimately calls back into `#applySelectableToSuper`
183160
// to add the column to the ANY super.
184-
185161
if ( discriminatorDescriptor == null && getColumnSpan() == 0 ) {
186162
if ( selectable instanceof Column ) {
187163
metaMapping.addColumn( (Column) selectable );
@@ -214,9 +190,7 @@ public Map<Object,String> getMetaValues() {
214190
return metaValueToEntityNameMap;
215191
}
216192

217-
@SuppressWarnings( "rawtypes" )
218-
public void setMetaValues(Map metaValueToEntityNameMap) {
219-
//noinspection unchecked
193+
public void setMetaValues(Map<Object,String> metaValueToEntityNameMap) {
220194
this.metaValueToEntityNameMap = metaValueToEntityNameMap;
221195
}
222196

@@ -242,8 +216,7 @@ public void setLazy(boolean lazy) {
242216
}
243217

244218
@Override
245-
public void setTypeUsingReflection(String className, String propertyName)
246-
throws MappingException {
219+
public void setTypeUsingReflection(String className, String propertyName) {
247220
}
248221

249222
@Override
@@ -258,10 +231,10 @@ public boolean isSame(SimpleValue other) {
258231

259232
public boolean isSame(Any other) {
260233
return super.isSame( other )
261-
&& Objects.equals( getTypeNameOrNull( keyMapping ), getTypeNameOrNull( other.keyMapping ) )
262-
&& Objects.equals( getTypeNameOrNull( metaMapping ), getTypeNameOrNull( other.metaMapping ) )
263-
&& Objects.equals( metaValueToEntityNameMap, other.metaValueToEntityNameMap )
264-
&& lazy == other.lazy;
234+
&& Objects.equals( getTypeNameOrNull( keyMapping ), getTypeNameOrNull( other.keyMapping ) )
235+
&& Objects.equals( getTypeNameOrNull( metaMapping ), getTypeNameOrNull( other.metaMapping ) )
236+
&& Objects.equals( metaValueToEntityNameMap, other.metaValueToEntityNameMap )
237+
&& lazy == other.lazy;
265238
}
266239

267240
private String getTypeNameOrNull(SimpleValue simpleValue) {
@@ -277,18 +250,17 @@ public boolean isValid(MappingContext mappingContext) throws MappingException {
277250
}
278251

279252
private static String columnName(Column column, MetadataBuildingContext buildingContext) {
280-
final JdbcServices jdbcServices = buildingContext
281-
.getBootstrapContext()
282-
.getServiceRegistry()
283-
.requireService( JdbcServices.class );
253+
final JdbcServices jdbcServices =
254+
buildingContext.getBootstrapContext().getServiceRegistry()
255+
.requireService( JdbcServices.class );
284256
return column.getQuotedName( jdbcServices.getDialect() );
285257
}
286258

287259
public void setDiscriminator(BasicValue discriminatorDescriptor) {
288260
this.discriminatorDescriptor = discriminatorDescriptor;
289-
if ( discriminatorDescriptor.getColumn() instanceof Column ) {
261+
if ( discriminatorDescriptor.getColumn() instanceof Column column ) {
290262
justAddColumn(
291-
(Column) discriminatorDescriptor.getColumn(),
263+
column,
292264
discriminatorDescriptor.isColumnInsertable( 0 ),
293265
discriminatorDescriptor.isColumnUpdateable( 0 )
294266
);
@@ -300,16 +272,14 @@ public void setDiscriminator(BasicValue discriminatorDescriptor) {
300272

301273
public void setDiscriminatorValueMappings(Map<Object, Class<?>> discriminatorValueMappings) {
302274
metaValueToEntityNameMap = new HashMap<>();
303-
discriminatorValueMappings.forEach( (value, entity) -> {
304-
metaValueToEntityNameMap.put( value, entity.getName() );
305-
} );
275+
discriminatorValueMappings.forEach( (value, entity) -> metaValueToEntityNameMap.put( value, entity.getName() ) );
306276
}
307277

308278
public void setKey(BasicValue keyDescriptor) {
309279
this.keyDescriptor = keyDescriptor;
310-
if ( keyDescriptor.getColumn() instanceof Column ) {
280+
if ( keyDescriptor.getColumn() instanceof Column column ) {
311281
justAddColumn(
312-
(Column) keyDescriptor.getColumn(),
282+
column,
313283
keyDescriptor.isColumnInsertable( 0 ),
314284
keyDescriptor.isColumnUpdateable( 0 )
315285
);
@@ -379,10 +349,8 @@ public void addColumn(Column column) {
379349
if ( columnName != null ) {
380350
throw new MappingException( "ANY discriminator already contained column" );
381351
}
382-
383352
super.addColumn( column );
384353
this.columnName = columnName( column, getBuildingContext() );
385-
386354
selectableConsumer.accept( column );
387355
column.setValue( this );
388356
}
@@ -392,10 +360,8 @@ public void addColumn(Column column, boolean isInsertable, boolean isUpdatable)
392360
if ( columnName != null ) {
393361
throw new MappingException( "ANY discriminator already contained column" );
394362
}
395-
396363
super.addColumn( column, isInsertable, isUpdatable );
397364
this.columnName = columnName( column, getBuildingContext() );
398-
399365
selectableConsumer.accept( column );
400366
column.setValue( this );
401367
}
@@ -405,10 +371,8 @@ public void addFormula(Formula formula) {
405371
if ( columnName != null ) {
406372
throw new MappingException( "ANY discriminator already contained column" );
407373
}
408-
409374
super.addFormula( formula );
410375
columnName = formula.getFormula();
411-
412376
selectableConsumer.accept( formula );
413377
}
414378

@@ -468,21 +432,18 @@ public void setTypeName(String typeName) {
468432
@Override
469433
public void addColumn(Column column) {
470434
super.addColumn( column );
471-
472435
selectableConsumer.accept( column );
473436
}
474437

475438
@Override
476439
public void addColumn(Column column, boolean isInsertable, boolean isUpdatable) {
477440
super.addColumn( column, isInsertable, isUpdatable );
478-
479441
selectableConsumer.accept( column );
480442
}
481443

482444
@Override
483445
public void addFormula(Formula formula) {
484446
super.addFormula( formula );
485-
486447
selectableConsumer.accept( formula );
487448
}
488449
}

hibernate-core/src/main/java/org/hibernate/mapping/Array.java

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

99
import org.hibernate.MappingException;
10-
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
1110
import org.hibernate.boot.registry.classloading.spi.ClassLoadingException;
1211
import org.hibernate.boot.spi.MetadataBuildingContext;
1312
import org.hibernate.resource.beans.spi.ManagedBean;
1413
import org.hibernate.type.ArrayType;
1514
import org.hibernate.type.BasicType;
1615
import org.hibernate.type.CollectionType;
16+
import org.hibernate.type.descriptor.java.JavaType;
1717
import org.hibernate.type.descriptor.java.spi.PrimitiveJavaType;
1818
import org.hibernate.usertype.UserCollectionType;
1919

20+
import static org.hibernate.mapping.MappingHelper.classForName;
21+
2022
/**
2123
* An array mapping has a primary key consisting of the key columns + index column.
2224
*
@@ -47,16 +49,14 @@ public Class<?> getElementClass() throws MappingException {
4749
if ( elementClassName == null ) {
4850
final org.hibernate.type.Type elementType = getElement().getType();
4951
if ( isPrimitiveArray() ) {
50-
return ( (PrimitiveJavaType<?>) ( (BasicType<?>) elementType ).getJavaTypeDescriptor() ).getPrimitiveClass();
52+
final JavaType<?> javaTypeDescriptor = ((BasicType<?>) elementType).getJavaTypeDescriptor();
53+
return ( (PrimitiveJavaType<?>) javaTypeDescriptor ).getPrimitiveClass();
5154
}
5255
return elementType.getReturnedClass();
5356
}
5457
else {
5558
try {
56-
return getMetadata().getMetadataBuildingOptions()
57-
.getServiceRegistry()
58-
.requireService( ClassLoaderService.class )
59-
.classForName( elementClassName );
59+
return classForName( elementClassName, getMetadata() );
6060
}
6161
catch (ClassLoadingException e) {
6262
throw new MappingException( e );

0 commit comments

Comments
 (0)