Skip to content

Commit cb01586

Browse files
committed
some cleanup to code in MetadataContext
1 parent ec7db94 commit cb01586

File tree

1 file changed

+80
-110
lines changed

1 file changed

+80
-110
lines changed

hibernate-core/src/main/java/org/hibernate/metamodel/internal/MetadataContext.java

Lines changed: 80 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
package org.hibernate.metamodel.internal;
66

77
import jakarta.persistence.metamodel.Attribute;
8-
import jakarta.persistence.metamodel.IdentifiableType;
98
import jakarta.persistence.metamodel.SingularAttribute;
109
import jakarta.persistence.metamodel.Type;
1110
import org.hibernate.AssertionFailure;
@@ -17,7 +16,6 @@
1716
import org.hibernate.internal.CoreLogging;
1817
import org.hibernate.internal.CoreMessageLogger;
1918
import org.hibernate.internal.util.collections.ArrayHelper;
20-
import org.hibernate.internal.util.collections.CollectionHelper;
2119
import org.hibernate.mapping.Component;
2220
import org.hibernate.mapping.MappedSuperclass;
2321
import org.hibernate.mapping.PersistentClass;
@@ -56,6 +54,7 @@
5654
import java.util.function.BiFunction;
5755

5856
import static java.util.Collections.unmodifiableMap;
57+
import static org.hibernate.internal.util.collections.CollectionHelper.mapOfSize;
5958
import static org.hibernate.metamodel.internal.InjectionHelper.injectField;
6059

6160
/**
@@ -158,17 +157,11 @@ public Set<EmbeddableDomainType<?>> getEmbeddableTypeSet() {
158157

159158
public Map<Class<?>, MappedSuperclassDomainType<?>> getMappedSuperclassTypeMap() {
160159
// we need to actually build this map...
161-
final Map<Class<?>, MappedSuperclassDomainType<?>> mappedSuperClassTypeMap = CollectionHelper.mapOfSize(
162-
mappedSuperclassByMappedSuperclassMapping.size()
163-
);
164-
165-
for ( MappedSuperclassDomainType<?> mappedSuperclassType : mappedSuperclassByMappedSuperclassMapping.values() ) {
166-
mappedSuperClassTypeMap.put(
167-
mappedSuperclassType.getJavaType(),
168-
mappedSuperclassType
169-
);
160+
final Map<Class<?>, MappedSuperclassDomainType<?>> mappedSuperClassTypeMap =
161+
mapOfSize( mappedSuperclassByMappedSuperclassMapping.size() );
162+
for ( var mappedSuperclassType : mappedSuperclassByMappedSuperclassMapping.values() ) {
163+
mappedSuperClassTypeMap.put( mappedSuperclassType.getJavaType(), mappedSuperclassType );
170164
}
171-
172165
return mappedSuperClassTypeMap;
173166
}
174167

@@ -263,12 +256,12 @@ public Map<String, IdentifiableDomainType<?>> getIdentifiableTypesByName() {
263256
Property property,
264257
IdentifiableDomainType<X> entityType,
265258
BiFunction<IdentifiableDomainType<X>, Property, PersistentAttribute<X, ?>> factoryFunction) {
266-
final Component component = property.getValue() instanceof Component comp ? comp : null;
267-
if ( component != null && component.isGeneric() ) {
259+
if ( property.getValue() instanceof Component component && component.isGeneric() ) {
268260
// This is an embeddable property that uses generics, we have to retrieve the generic
269261
// component previously registered and create the concrete attribute
270-
final Component genericComponent = runtimeModelCreationContext.getMetadata()
271-
.getGenericComponent( component.getComponentClass() );
262+
final Component genericComponent =
263+
runtimeModelCreationContext.getMetadata()
264+
.getGenericComponent( component.getComponentClass() );
272265
final Property genericProperty = property.copy();
273266
genericProperty.setValue( genericComponent );
274267
genericProperty.setGeneric( true );
@@ -382,8 +375,9 @@ else if ( safeMapping.isVersioned() && property == safeMapping.getVersion() ) {
382375

383376

384377
while ( ! embeddablesToProcess.isEmpty() ) {
385-
final ArrayList<EmbeddableDomainType<?>> processingEmbeddables = new ArrayList<>( embeddablesToProcess.size() );
386-
for ( List<EmbeddableDomainType<?>> embeddableDomainTypes : embeddablesToProcess.values() ) {
378+
final List<EmbeddableDomainType<?>> processingEmbeddables =
379+
new ArrayList<>( embeddablesToProcess.size() );
380+
for ( var embeddableDomainTypes : embeddablesToProcess.values() ) {
387381
processingEmbeddables.addAll( embeddableDomainTypes );
388382
}
389383

@@ -403,7 +397,6 @@ else if ( safeMapping.isVersioned() && property == safeMapping.getVersion() ) {
403397
// generic component embeddables used just for concrete type resolution
404398
if ( !component.isGeneric() && !( embeddable.getExpressibleJavaType() instanceof EntityJavaType<?> ) ) {
405399
embeddables.put( embeddable.getJavaType(), embeddable );
406-
407400
if ( staticMetamodelScanEnabled ) {
408401
populateStaticMetamodel( embeddable, processedMetamodelClasses );
409402
}
@@ -414,27 +407,24 @@ else if ( safeMapping.isVersioned() && property == safeMapping.getVersion() ) {
414407

415408
private static boolean isIdentifierProperty(Property property, MappedSuperclass mappedSuperclass) {
416409
final Component identifierMapper = mappedSuperclass.getIdentifierMapper();
417-
return identifierMapper != null && ArrayHelper.contains(
418-
identifierMapper.getPropertyNames(),
419-
property.getName()
420-
);
410+
return identifierMapper != null
411+
&& ArrayHelper.contains( identifierMapper.getPropertyNames(), property.getName() );
421412
}
422413

423414
private <T> void addAttribute(EmbeddableDomainType<T> embeddable, Property property, Component component) {
424415
final PersistentAttribute<T, ?> attribute =
425416
attributeFactory.buildAttribute( embeddable, property);
426417
if ( attribute != null ) {
427-
final Property superclassProperty = getMappedSuperclassProperty(
428-
property.getName(),
429-
component.getMappedSuperclass()
430-
);
418+
final Property superclassProperty =
419+
getMappedSuperclassProperty( property.getName(),
420+
component.getMappedSuperclass() );
431421
if ( superclassProperty != null && superclassProperty.isGeneric() ) {
432422
@SuppressWarnings("unchecked")
433-
final AttributeContainer<T> attributeContainer = (AttributeContainer<T>) embeddable;
423+
final var attributeContainer = (AttributeContainer<T>) embeddable;
434424
attributeContainer.getInFlightAccess().addConcreteGenericAttribute( attribute );
435425
}
436426
else {
437-
addAttribute(embeddable, attribute );
427+
addAttribute( embeddable, attribute );
438428
}
439429
}
440430
}
@@ -456,8 +446,9 @@ private <T> void addAttribute(ManagedDomainType<T> type, PersistentAttribute<T,
456446
@SuppressWarnings("unchecked")
457447
final AttributeContainer<T> container = (AttributeContainer<T>) type;
458448
final AttributeContainer.InFlightAccess<T> inFlightAccess = container.getInFlightAccess();
459-
final boolean virtual = attribute.getPersistentAttributeType() == Attribute.PersistentAttributeType.EMBEDDED
460-
&& attribute.getAttributeJavaType() instanceof EntityJavaType<?>;
449+
final boolean virtual =
450+
attribute.getPersistentAttributeType() == Attribute.PersistentAttributeType.EMBEDDED
451+
&& attribute.getAttributeJavaType() instanceof EntityJavaType<?>;
461452
if ( virtual ) {
462453
@SuppressWarnings("unchecked")
463454
final EmbeddableDomainType<T> embeddableDomainType =
@@ -487,24 +478,19 @@ private <T> void applyIdMetadata(PersistentClass persistentClass, IdentifiableDo
487478
@SuppressWarnings("unchecked")
488479
final AttributeContainer<T> attributeContainer = (AttributeContainer<T>) identifiableType;
489480
if ( declaredIdentifierProperty != null ) {
490-
final SingularPersistentAttribute<T, ?> idAttribute =
481+
final var idAttribute =
491482
(SingularPersistentAttribute<T, ?>)
492-
buildAttribute(
493-
declaredIdentifierProperty,
494-
identifiableType,
495-
attributeFactory::buildIdAttribute
496-
);
483+
buildAttribute( declaredIdentifierProperty, identifiableType,
484+
attributeFactory::buildIdAttribute );
497485
attributeContainer.getInFlightAccess().applyIdAttribute( idAttribute );
498486
}
499487
else {
500488
final Property superclassIdentifier = getMappedSuperclassIdentifier( persistentClass );
501489
if ( superclassIdentifier != null && superclassIdentifier.isGeneric() ) {
502490
// If the superclass identifier is generic we have to build the attribute to register the concrete type
503-
final SingularPersistentAttribute<T, ?> concreteIdentifier =
504-
attributeFactory.buildIdAttribute(
505-
identifiableType,
506-
persistentClass.getIdentifierProperty()
507-
);
491+
final var concreteIdentifier =
492+
attributeFactory.buildIdAttribute( identifiableType,
493+
persistentClass.getIdentifierProperty() );
508494
attributeContainer.getInFlightAccess().addConcreteGenericAttribute( concreteIdentifier );
509495
}
510496
}
@@ -542,7 +528,7 @@ private <T> void applyIdMetadata(PersistentClass persistentClass, IdentifiableDo
542528
final IdentifiableDomainType<?> idDomainType =
543529
identifiableTypesByName.get( compositeId.getOwner().getEntityName() );
544530
@SuppressWarnings("unchecked")
545-
final AbstractIdentifiableType<T> idType = (AbstractIdentifiableType<T>) idDomainType;
531+
final var idType = (AbstractIdentifiableType<T>) idDomainType;
546532
applyIdAttributes( identifiableType, idType, propertySpan, cidProperties, idClassType );
547533
}
548534
}
@@ -562,7 +548,7 @@ private <T> void applyIdAttributes(
562548
}
563549

564550
@SuppressWarnings("unchecked")
565-
final AttributeContainer<T> container = (AttributeContainer<T>) identifiableType;
551+
final var container = (AttributeContainer<T>) identifiableType;
566552
container.getInFlightAccess().applyNonAggregatedIdAttributes( idAttributes, idClassType);
567553
}
568554

@@ -579,54 +565,45 @@ private Property getMappedSuperclassIdentifier(PersistentClass persistentClass)
579565
}
580566

581567
private <Y> EmbeddableTypeImpl<Y> applyIdClassMetadata(Component idClassComponent) {
582-
final JavaType<Y> javaType =
583-
getTypeConfiguration().getJavaTypeRegistry()
584-
.resolveManagedTypeDescriptor( idClassComponent.getComponentClass() );
585-
586-
final MappedSuperclass mappedSuperclass = idClassComponent.getMappedSuperclass();
587-
final MappedSuperclassDomainType<? super Y> superType;
588-
if ( mappedSuperclass != null ) {
589-
//noinspection unchecked
590-
superType = (MappedSuperclassDomainType<? super Y>) locateMappedSuperclassType( mappedSuperclass );
591-
}
592-
else {
593-
superType = null;
594-
}
595-
596-
final EmbeddableTypeImpl<Y> embeddableType = new EmbeddableTypeImpl<>(
597-
javaType,
598-
superType,
599-
null,
600-
false,
601-
getJpaMetamodel()
602-
);
568+
final EmbeddableTypeImpl<Y> embeddableType =
569+
new EmbeddableTypeImpl<>(
570+
getTypeConfiguration().getJavaTypeRegistry()
571+
.resolveManagedTypeDescriptor( idClassComponent.getComponentClass() ),
572+
getMappedSuperclassDomainType( idClassComponent ),
573+
null,
574+
false,
575+
getJpaMetamodel()
576+
);
603577
registerEmbeddableType( embeddableType, idClassComponent );
604578
return embeddableType;
605579
}
606580

581+
@SuppressWarnings("unchecked")
582+
private <Y> MappedSuperclassDomainType<? super Y> getMappedSuperclassDomainType(Component idClassComponent) {
583+
final MappedSuperclass mappedSuperclass = idClassComponent.getMappedSuperclass();
584+
return mappedSuperclass == null ? null
585+
: (MappedSuperclassDomainType<? super Y>)
586+
locateMappedSuperclassType( mappedSuperclass );
587+
}
588+
607589
private <X> void applyIdMetadata(MappedSuperclass mappingType, MappedSuperclassDomainType<X> jpaMappingType) {
608590
@SuppressWarnings("unchecked")
609-
final AttributeContainer<X> attributeContainer = (AttributeContainer<X>) jpaMappingType;
591+
final var attributeContainer = (AttributeContainer<X>) jpaMappingType;
610592
if ( mappingType.hasIdentifierProperty() ) {
611593
final Property declaredIdentifierProperty = mappingType.getDeclaredIdentifierProperty();
612594
if ( declaredIdentifierProperty != null ) {
613-
final SingularPersistentAttribute<X, ?> attribute =
595+
final var attribute =
614596
(SingularPersistentAttribute<X, ?>)
615-
buildAttribute(
616-
declaredIdentifierProperty,
617-
jpaMappingType,
618-
attributeFactory::buildIdAttribute
619-
);
597+
buildAttribute( declaredIdentifierProperty, jpaMappingType,
598+
attributeFactory::buildIdAttribute );
620599
attributeContainer.getInFlightAccess().applyIdAttribute( attribute );
621600
}
622601
}
623602
//a MappedSuperclass can have no identifier if the id is set below in the hierarchy
624603
else if ( mappingType.getIdentifierMapper() != null ) {
625-
final Set<SingularPersistentAttribute<? super X, ?>> attributes =
626-
buildIdClassAttributes(
627-
jpaMappingType,
628-
mappingType.getIdentifierMapper().getProperties()
629-
);
604+
final var attributes =
605+
buildIdClassAttributes( jpaMappingType,
606+
mappingType.getIdentifierMapper().getProperties() );
630607
attributeContainer.getInFlightAccess().applyIdClassAttributes( attributes );
631608
}
632609
}
@@ -635,21 +612,19 @@ private <X> void applyVersionAttribute(PersistentClass persistentClass, EntityDo
635612
final Property declaredVersion = persistentClass.getDeclaredVersion();
636613
if ( declaredVersion != null ) {
637614
@SuppressWarnings("unchecked")
638-
final AttributeContainer<X> attributeContainer = (AttributeContainer<X>) jpaEntityType;
639-
attributeContainer.getInFlightAccess().applyVersionAttribute(
640-
attributeFactory.buildVersionAttribute( jpaEntityType, declaredVersion )
641-
);
615+
final var attributeContainer = (AttributeContainer<X>) jpaEntityType;
616+
attributeContainer.getInFlightAccess()
617+
.applyVersionAttribute( attributeFactory.buildVersionAttribute( jpaEntityType, declaredVersion ) );
642618
}
643619
}
644620

645621
private <X> void applyVersionAttribute(MappedSuperclass mappingType, MappedSuperclassDomainType<X> jpaMappingType) {
646622
final Property declaredVersion = mappingType.getDeclaredVersion();
647623
if ( declaredVersion != null ) {
648624
@SuppressWarnings("unchecked")
649-
final AttributeContainer<X> xAttributeContainer = (AttributeContainer<X>) jpaMappingType;
650-
xAttributeContainer.getInFlightAccess().applyVersionAttribute(
651-
attributeFactory.buildVersionAttribute( jpaMappingType, declaredVersion )
652-
);
625+
final var attributeContainer = (AttributeContainer<X>) jpaMappingType;
626+
attributeContainer.getInFlightAccess()
627+
.applyVersionAttribute( attributeFactory.buildVersionAttribute( jpaMappingType, declaredVersion ) );
653628
}
654629
}
655630

@@ -661,7 +636,7 @@ private <X> void applyGenericProperties(PersistentClass persistentClass, EntityD
661636
final Property property = persistentClass.getProperty( superclassProperty.getName() );
662637
final PersistentAttribute<X, ?> attribute = attributeFactory.buildAttribute( entityType, property );
663638
@SuppressWarnings("unchecked")
664-
final AttributeContainer<X> attributeContainer = (AttributeContainer<X>) entityType;
639+
final var attributeContainer = (AttributeContainer<X>) entityType;
665640
attributeContainer.getInFlightAccess().addConcreteGenericAttribute( attribute );
666641
}
667642
}
@@ -682,9 +657,9 @@ private MappedSuperclass getMappedSuperclass(PersistentClass persistentClass) {
682657

683658
private MappedSuperclass getMappedSuperclass(MappedSuperclass mappedSuperclass) {
684659
final MappedSuperclass superMappedSuperclass = mappedSuperclass.getSuperMappedSuperclass();
685-
return superMappedSuperclass != null
686-
? superMappedSuperclass
687-
: getMappedSuperclass( mappedSuperclass.getSuperPersistentClass() );
660+
return superMappedSuperclass == null
661+
? getMappedSuperclass( mappedSuperclass.getSuperPersistentClass() )
662+
: superMappedSuperclass;
688663
}
689664

690665
private Property getMappedSuperclassProperty(String propertyName, MappedSuperclass mappedSuperclass) {
@@ -731,7 +706,8 @@ private <X> void populateStaticMetamodel(ManagedDomainType<X> managedType, Set<S
731706
&& processedMetamodelClassName.add( metamodelClassName( managedType ) ) ) {
732707
final Class<?> metamodelClass = metamodelClass( managedType );
733708
if ( metamodelClass != null ) {
734-
populateMetamodelClass( managedType, metamodelClass );
709+
registerAttributes( metamodelClass, managedType );
710+
injectManagedType( managedType, metamodelClass );
735711
}
736712
// todo : this does not account for @MappedSuperclass, mainly
737713
// because this is not being tracked in our internal
@@ -743,11 +719,6 @@ private <X> void populateStaticMetamodel(ManagedDomainType<X> managedType, Set<S
743719
}
744720
}
745721

746-
private <X> void populateMetamodelClass(ManagedDomainType<X> managedType, Class<?> metamodelClass) {
747-
registerAttributes( metamodelClass, managedType );
748-
injectManagedType( managedType, metamodelClass );
749-
}
750-
751722
private static <X> void injectManagedType(ManagedDomainType<X> managedType, Class<?> metamodelClass) {
752723
try {
753724
injectField( metamodelClass, "class_", managedType, false );
@@ -788,17 +759,15 @@ private <X> void registerAttributes(Class<?> metamodelClass, ManagedDomainType<X
788759
registerAttribute( metamodelClass, attribute );
789760
}
790761

791-
if ( managedType instanceof IdentifiableType ) {
792-
final AbstractIdentifiableType<X> entityType = (AbstractIdentifiableType<X>) managedType;
793-
762+
if ( managedType instanceof AbstractIdentifiableType<X> entityType ) {
794763
// handle version
795764
if ( entityType.hasDeclaredVersionAttribute() ) {
796765
registerAttribute( metamodelClass, entityType.getDeclaredVersion() );
797766
}
798767

799768
// handle id-class mappings specially
800769
if ( entityType.hasIdClass() ) {
801-
final Set<SingularPersistentAttribute<? super X, ?>> attributes = entityType.getIdClassAttributesSafely();
770+
final var attributes = entityType.getIdClassAttributesSafely();
802771
if ( attributes != null ) {
803772
for ( SingularAttribute<? super X, ?> attribute : attributes ) {
804773
registerAttribute( metamodelClass, attribute );
@@ -833,7 +802,6 @@ private <X> void registerAttribute(Class<?> metamodelClass, Attribute<X, ?> attr
833802
}
834803
}
835804

836-
837805
public MappedSuperclassDomainType<?> locateMappedSuperclassType(MappedSuperclass mappedSuperclass) {
838806
return mappedSuperclassByMappedSuperclassMapping.get( mappedSuperclass );
839807
}
@@ -874,22 +842,24 @@ public <J> BasicDomainType<J> resolveBasicType(Class<J> javaType) {
874842
if ( domainType == null ) {
875843
// we cannot use getTypeConfiguration().standardBasicTypeForJavaType(javaType)
876844
// because that doesn't return the right thing for primitive types
877-
final JavaType<J> javaTypeDescriptor =
878-
getTypeConfiguration().getJavaTypeRegistry().resolveDescriptor( javaType );
879-
final JdbcType jdbcType =
880-
javaTypeDescriptor.getRecommendedJdbcType( typeConfiguration.getCurrentBaseSqlTypeIndicators() );
881-
final BasicDomainType<J> type =
882-
javaType.isPrimitive()
883-
? new PrimitiveBasicTypeImpl<>( javaTypeDescriptor, jdbcType, javaType )
884-
: new BasicTypeImpl<>( javaTypeDescriptor, jdbcType );
885-
basicDomainTypeMap.put( javaType, type );
886-
return type;
845+
basicDomainTypeMap.put( javaType, basicDomainType( javaType ) );
846+
return basicDomainType( javaType );
887847
}
888848
else {
889849
return domainType;
890850
}
891851
}
892852

853+
private <J> BasicDomainType<J> basicDomainType(Class<J> javaType) {
854+
final JavaType<J> javaTypeDescriptor =
855+
getTypeConfiguration().getJavaTypeRegistry().resolveDescriptor( javaType );
856+
final JdbcType jdbcType =
857+
javaTypeDescriptor.getRecommendedJdbcType( typeConfiguration.getCurrentBaseSqlTypeIndicators() );
858+
return javaType.isPrimitive()
859+
? new PrimitiveBasicTypeImpl<>( javaTypeDescriptor, jdbcType, javaType )
860+
: new BasicTypeImpl<>( javaTypeDescriptor, jdbcType );
861+
}
862+
893863
public <J> EmbeddableDomainType<J> locateEmbeddable(Class<J> embeddableClass, Component component) {
894864
//noinspection unchecked
895865
EmbeddableDomainType<J> domainType = (EmbeddableDomainType<J>) embeddables.get( embeddableClass );

0 commit comments

Comments
 (0)