Skip to content

Commit da47e57

Browse files
committed
make AttributeContainer extends ManagedDomainType
in order to resolve some unchecked casts in in MetadataContext also use lots of 'var' in MetadataContext
1 parent 9321a3e commit da47e57

File tree

2 files changed

+79
-79
lines changed

2 files changed

+79
-79
lines changed

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

Lines changed: 77 additions & 78 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.SingularAttribute;
98
import jakarta.persistence.metamodel.Type;
109
import org.hibernate.AssertionFailure;
1110
import org.hibernate.Internal;
@@ -265,12 +264,12 @@ public Map<String, IdentifiableDomainType<?>> getIdentifiableTypesByName() {
265264
final Property genericProperty = property.copy();
266265
genericProperty.setValue( genericComponent );
267266
genericProperty.setGeneric( true );
268-
final PersistentAttribute<X, ?> attribute = factoryFunction.apply( entityType, genericProperty );
267+
final var attribute = factoryFunction.apply( entityType, genericProperty );
269268
if ( !property.isGeneric() ) {
270-
final PersistentAttribute<X, ?> concreteAttribute = factoryFunction.apply( entityType, property );
269+
final var concreteAttribute = factoryFunction.apply( entityType, property );
271270
if ( concreteAttribute != null ) {
272-
@SuppressWarnings("unchecked")
273-
final AttributeContainer<X> attributeContainer = (AttributeContainer<X>) entityType;
271+
final var managedType = (ManagedDomainType<X>) entityType;
272+
final var attributeContainer = (AttributeContainer<X>) managedType;
274273
attributeContainer.getInFlightAccess().addConcreteGenericAttribute( concreteAttribute );
275274
}
276275
}
@@ -383,7 +382,7 @@ else if ( safeMapping.isVersioned() && property == safeMapping.getVersion() ) {
383382

384383
embeddablesToProcess.clear();
385384

386-
for ( EmbeddableDomainType<?> embeddable : processingEmbeddables ) {
385+
for ( var embeddable : processingEmbeddables ) {
387386
final Component component = componentByEmbeddable.get( embeddable );
388387
for ( Property property : component.getProperties() ) {
389388
if ( !component.isPolymorphic()
@@ -395,7 +394,8 @@ else if ( safeMapping.isVersioned() && property == safeMapping.getVersion() ) {
395394
( ( AttributeContainer<?>) embeddable ).getInFlightAccess().finishUp();
396395
// Do not process embeddables for entity types i.e. id-classes or
397396
// generic component embeddables used just for concrete type resolution
398-
if ( !component.isGeneric() && !( embeddable.getExpressibleJavaType() instanceof EntityJavaType<?> ) ) {
397+
if ( !component.isGeneric()
398+
&& !( embeddable.getExpressibleJavaType() instanceof EntityJavaType<?> ) ) {
399399
embeddables.put( embeddable.getJavaType(), embeddable );
400400
if ( staticMetamodelScanEnabled ) {
401401
populateStaticMetamodel( embeddable, processedMetamodelClasses );
@@ -419,8 +419,8 @@ private <T> void addAttribute(EmbeddableDomainType<T> embeddable, Property prope
419419
getMappedSuperclassProperty( property.getName(),
420420
component.getMappedSuperclass() );
421421
if ( superclassProperty != null && superclassProperty.isGeneric() ) {
422-
@SuppressWarnings("unchecked")
423-
final var attributeContainer = (AttributeContainer<T>) embeddable;
422+
final var managedType = (ManagedDomainType<T>) embeddable;
423+
final var attributeContainer = (AttributeContainer<T>) managedType;
424424
attributeContainer.getInFlightAccess().addConcreteGenericAttribute( attribute );
425425
}
426426
else {
@@ -430,29 +430,26 @@ private <T> void addAttribute(EmbeddableDomainType<T> embeddable, Property prope
430430
}
431431

432432
private <T> void buildAttribute(Property property, IdentifiableDomainType<T> jpaType) {
433-
final PersistentAttribute<T, ?> attribute =
434-
buildAttribute( property, jpaType, attributeFactory::buildAttribute );
433+
final var attribute = buildAttribute( property, jpaType, attributeFactory::buildAttribute );
435434
if ( attribute != null ) {
436435
addAttribute( jpaType, attribute );
437436
if ( property.isNaturalIdentifier() ) {
438-
@SuppressWarnings("unchecked")
439-
final AttributeContainer<T> attributeContainer = (AttributeContainer<T>) jpaType;
437+
final var managedType = (ManagedDomainType<T>) jpaType;
438+
final var attributeContainer = (AttributeContainer<T>) managedType;
440439
attributeContainer.getInFlightAccess().applyNaturalIdAttribute( attribute );
441440
}
442441
}
443442
}
444443

445444
private <T> void addAttribute(ManagedDomainType<T> type, PersistentAttribute<T, ?> attribute) {
446-
@SuppressWarnings("unchecked")
447-
final AttributeContainer<T> container = (AttributeContainer<T>) type;
448-
final AttributeContainer.InFlightAccess<T> inFlightAccess = container.getInFlightAccess();
445+
final var container = (AttributeContainer<T>) type;
446+
final var inFlightAccess = container.getInFlightAccess();
449447
final boolean virtual =
450448
attribute.getPersistentAttributeType() == Attribute.PersistentAttributeType.EMBEDDED
451449
&& attribute.getAttributeJavaType() instanceof EntityJavaType<?>;
452450
if ( virtual ) {
453451
@SuppressWarnings("unchecked")
454-
final EmbeddableDomainType<T> embeddableDomainType =
455-
(EmbeddableDomainType<T>) attribute.getValueGraphType();
452+
final var embeddableDomainType = (EmbeddableDomainType<T>) attribute.getValueGraphType();
456453
final Component component = componentByEmbeddable.get( embeddableDomainType );
457454
for ( Property property : component.getProperties() ) {
458455
final PersistentAttribute<T, ?> subAttribute =
@@ -474,9 +471,9 @@ private <T> void addAttribute(ManagedDomainType<T> type, PersistentAttribute<T,
474471

475472
private <T> void applyIdMetadata(PersistentClass persistentClass, IdentifiableDomainType<T> identifiableType) {
476473
if ( persistentClass.hasIdentifierProperty() ) {
474+
final var managedType = (ManagedDomainType<T>) identifiableType;
475+
final var attributeContainer = (AttributeContainer<T>) managedType;
477476
final Property declaredIdentifierProperty = persistentClass.getDeclaredIdentifierProperty();
478-
@SuppressWarnings("unchecked")
479-
final AttributeContainer<T> attributeContainer = (AttributeContainer<T>) identifiableType;
480477
if ( declaredIdentifierProperty != null ) {
481478
final var idAttribute =
482479
(SingularPersistentAttribute<T, ?>)
@@ -509,13 +506,10 @@ private <T> void applyIdMetadata(PersistentClass persistentClass, IdentifiableDo
509506
if ( identifierMapper != null ) {
510507
cidProperties = identifierMapper.getProperties();
511508
propertySpan = identifierMapper.getPropertySpan();
512-
if ( identifierMapper.getComponentClassName() == null ) {
513-
// support for no id-class, especially for dynamic models
514-
idClassType = null;
515-
}
516-
else {
517-
idClassType = applyIdClassMetadata( (Component) persistentClass.getIdentifier() );
518-
}
509+
idClassType =
510+
identifierMapper.getComponentClassName() == null
511+
? null // support for no id-class, especially for dynamic models
512+
: applyIdClassMetadata( (Component) persistentClass.getIdentifier() );
519513
}
520514
else {
521515
cidProperties = compositeId.getProperties();
@@ -525,8 +519,7 @@ private <T> void applyIdMetadata(PersistentClass persistentClass, IdentifiableDo
525519

526520
assert compositeId.isEmbedded();
527521

528-
final IdentifiableDomainType<?> idDomainType =
529-
identifiableTypesByName.get( compositeId.getOwner().getEntityName() );
522+
final var idDomainType = identifiableTypesByName.get( compositeId.getOwner().getEntityName() );
530523
@SuppressWarnings("unchecked")
531524
final var idType = (AbstractIdentifiableType<T>) idDomainType;
532525
applyIdAttributes( identifiableType, idType, propertySpan, cidProperties, idClassType );
@@ -539,17 +532,29 @@ private <T> void applyIdAttributes(
539532
int propertySpan,
540533
List<Property> cidProperties,
541534
EmbeddableTypeImpl<?> idClassType) {
542-
Set<SingularPersistentAttribute<? super T, ?>> idAttributes = idType.getIdClassAttributesSafely();
543-
if ( idAttributes == null ) {
544-
idAttributes = new HashSet<>( propertySpan );
535+
final var idAttributes = idClassAttributes( idType, propertySpan, cidProperties );
536+
final var managedType = (ManagedDomainType<T>) identifiableType;
537+
final var container = (AttributeContainer<T>) managedType;
538+
container.getInFlightAccess().applyNonAggregatedIdAttributes( idAttributes, idClassType);
539+
}
540+
541+
private <T> Set<SingularPersistentAttribute<? super T, ?>> idClassAttributes(
542+
AbstractIdentifiableType<T> idType,
543+
int propertySpan,
544+
List<Property> cidProperties) {
545+
final var idAttributes = idType.getIdClassAttributesSafely();
546+
if ( idAttributes != null ) {
547+
return idAttributes;
548+
}
549+
else {
550+
final Set<SingularPersistentAttribute<? super T, ?>> result = new HashSet<>( propertySpan );
545551
for ( Property cidSubproperty : cidProperties ) {
546-
idAttributes.add( attributeFactory.buildIdAttribute( idType, cidSubproperty ) );
552+
final SingularPersistentAttribute<T, ?> idAttribute =
553+
attributeFactory.buildIdAttribute( idType, cidSubproperty );
554+
result.add( idAttribute );
547555
}
556+
return result;
548557
}
549-
550-
@SuppressWarnings("unchecked")
551-
final var container = (AttributeContainer<T>) identifiableType;
552-
container.getInFlightAccess().applyNonAggregatedIdAttributes( idAttributes, idClassType);
553558
}
554559

555560
private Property getMappedSuperclassIdentifier(PersistentClass persistentClass) {
@@ -565,10 +570,9 @@ private Property getMappedSuperclassIdentifier(PersistentClass persistentClass)
565570
}
566571

567572
private <Y> EmbeddableTypeImpl<Y> applyIdClassMetadata(Component idClassComponent) {
568-
final EmbeddableTypeImpl<Y> embeddableType =
569-
new EmbeddableTypeImpl<>(
570-
getTypeConfiguration().getJavaTypeRegistry()
571-
.resolveManagedTypeDescriptor( idClassComponent.getComponentClass() ),
573+
final var embeddableType =
574+
new EmbeddableTypeImpl<Y>(
575+
getJavaTypeRegistry().resolveManagedTypeDescriptor( idClassComponent.getComponentClass() ),
572576
getMappedSuperclassDomainType( idClassComponent ),
573577
null,
574578
false,
@@ -587,8 +591,8 @@ private <Y> MappedSuperclassDomainType<? super Y> getMappedSuperclassDomainType(
587591
}
588592

589593
private <X> void applyIdMetadata(MappedSuperclass mappingType, MappedSuperclassDomainType<X> jpaMappingType) {
590-
@SuppressWarnings("unchecked")
591-
final var attributeContainer = (AttributeContainer<X>) jpaMappingType;
594+
final var managedType = (ManagedDomainType<X>) jpaMappingType;
595+
final var attributeContainer = (AttributeContainer<X>) managedType;
592596
if ( mappingType.hasIdentifierProperty() ) {
593597
final Property declaredIdentifierProperty = mappingType.getDeclaredIdentifierProperty();
594598
if ( declaredIdentifierProperty != null ) {
@@ -611,8 +615,8 @@ else if ( mappingType.getIdentifierMapper() != null ) {
611615
private <X> void applyVersionAttribute(PersistentClass persistentClass, EntityDomainType<X> jpaEntityType) {
612616
final Property declaredVersion = persistentClass.getDeclaredVersion();
613617
if ( declaredVersion != null ) {
614-
@SuppressWarnings("unchecked")
615-
final var attributeContainer = (AttributeContainer<X>) jpaEntityType;
618+
final var managedType = (ManagedDomainType<X>) jpaEntityType;
619+
final var attributeContainer = (AttributeContainer<X>) managedType;
616620
attributeContainer.getInFlightAccess()
617621
.applyVersionAttribute( attributeFactory.buildVersionAttribute( jpaEntityType, declaredVersion ) );
618622
}
@@ -621,22 +625,22 @@ private <X> void applyVersionAttribute(PersistentClass persistentClass, EntityDo
621625
private <X> void applyVersionAttribute(MappedSuperclass mappingType, MappedSuperclassDomainType<X> jpaMappingType) {
622626
final Property declaredVersion = mappingType.getDeclaredVersion();
623627
if ( declaredVersion != null ) {
624-
@SuppressWarnings("unchecked")
625-
final var attributeContainer = (AttributeContainer<X>) jpaMappingType;
628+
final var managedType = (ManagedDomainType<X>) jpaMappingType;
629+
final var attributeContainer = (AttributeContainer<X>) managedType;
626630
attributeContainer.getInFlightAccess()
627631
.applyVersionAttribute( attributeFactory.buildVersionAttribute( jpaMappingType, declaredVersion ) );
628632
}
629633
}
630634

631635
private <X> void applyGenericProperties(PersistentClass persistentClass, EntityDomainType<X> entityType) {
632-
MappedSuperclass mappedSuperclass = getMappedSuperclass( persistentClass );
636+
var mappedSuperclass = getMappedSuperclass( persistentClass );
633637
while ( mappedSuperclass != null ) {
634638
for ( Property superclassProperty : mappedSuperclass.getDeclaredProperties() ) {
635639
if ( superclassProperty.isGeneric() ) {
636640
final Property property = persistentClass.getProperty( superclassProperty.getName() );
637641
final PersistentAttribute<X, ?> attribute = attributeFactory.buildAttribute( entityType, property );
638-
@SuppressWarnings("unchecked")
639-
final var attributeContainer = (AttributeContainer<X>) entityType;
642+
final var managedType = (ManagedDomainType<X>) entityType;
643+
final var attributeContainer = (AttributeContainer<X>) managedType;
640644
attributeContainer.getInFlightAccess().addConcreteGenericAttribute( attribute );
641645
}
642646
}
@@ -646,7 +650,7 @@ private <X> void applyGenericProperties(PersistentClass persistentClass, EntityD
646650

647651
private MappedSuperclass getMappedSuperclass(PersistentClass persistentClass) {
648652
while ( persistentClass != null ) {
649-
final MappedSuperclass mappedSuperclass = persistentClass.getSuperMappedSuperclass();
653+
final var mappedSuperclass = persistentClass.getSuperMappedSuperclass();
650654
if ( mappedSuperclass != null ) {
651655
return mappedSuperclass;
652656
}
@@ -656,7 +660,7 @@ private MappedSuperclass getMappedSuperclass(PersistentClass persistentClass) {
656660
}
657661

658662
private MappedSuperclass getMappedSuperclass(MappedSuperclass mappedSuperclass) {
659-
final MappedSuperclass superMappedSuperclass = mappedSuperclass.getSuperMappedSuperclass();
663+
final var superMappedSuperclass = mappedSuperclass.getSuperMappedSuperclass();
660664
return superMappedSuperclass == null
661665
? getMappedSuperclass( mappedSuperclass.getSuperPersistentClass() )
662666
: superMappedSuperclass;
@@ -710,7 +714,7 @@ private <X> void populateStaticMetamodel(ManagedDomainType<X> managedType, Set<S
710714
// todo : this does not account for @MappedSuperclass, mainly
711715
// because this is not being tracked in our internal
712716
// metamodel as populated from the annotations properly
713-
final ManagedDomainType<? super X> superType = managedType.getSuperType();
717+
final var superType = managedType.getSuperType();
714718
if ( superType != null ) {
715719
populateStaticMetamodel( superType, processedMetamodelClassName );
716720
}
@@ -753,7 +757,7 @@ public Class<?> metamodelClass(ManagedDomainType<?> managedDomainType) {
753757

754758
private <X> void registerAttributes(Class<?> metamodelClass, ManagedDomainType<X> managedType) {
755759
// push the attributes on to the metamodel class...
756-
for ( Attribute<X, ?> attribute : managedType.getDeclaredAttributes() ) {
760+
for ( var attribute : managedType.getDeclaredAttributes() ) {
757761
registerAttribute( metamodelClass, attribute );
758762
}
759763

@@ -767,7 +771,7 @@ private <X> void registerAttributes(Class<?> metamodelClass, ManagedDomainType<X
767771
if ( entityType.hasIdClass() ) {
768772
final var attributes = entityType.getIdClassAttributesSafely();
769773
if ( attributes != null ) {
770-
for ( SingularAttribute<? super X, ?> attribute : attributes ) {
774+
for ( var attribute : attributes ) {
771775
registerAttribute( metamodelClass, attribute );
772776
}
773777
}
@@ -789,13 +793,12 @@ private <X> void registerAttribute(Class<?> metamodelClass, Attribute<X, ?> attr
789793
final boolean allowNonDeclaredFieldReference =
790794
attribute.getPersistentAttributeType() == Attribute.PersistentAttributeType.EMBEDDED
791795
|| attribute.getDeclaringType().getPersistenceType() == Type.PersistenceType.EMBEDDABLE;
792-
793796
injectField( metamodelClass, name, attribute, allowNonDeclaredFieldReference );
794797
}
795798
catch (NoSuchFieldException e) {
796799
log.unableToLocateStaticMetamodelField( metamodelClass.getName(), name );
797800
// throw new AssertionFailure(
798-
// "Unable to locate static metamodel field : " + metamodelClass.getName() + '#' + name
801+
// "Unable to locate static metamodel field: " + metamodelClass.getName() + '#' + name
799802
// );
800803
}
801804
}
@@ -836,7 +839,7 @@ public Set<MappedSuperclass> getUnusedMappedSuperclasses() {
836839

837840
public <J> BasicDomainType<J> resolveBasicType(Class<J> javaType) {
838841
@SuppressWarnings("unchecked")
839-
final BasicDomainType<J> domainType = (BasicDomainType<J>) basicDomainTypeMap.get( javaType );
842+
final var domainType = (BasicDomainType<J>) basicDomainTypeMap.get( javaType );
840843
if ( domainType == null ) {
841844
// we cannot use getTypeConfiguration().standardBasicTypeForJavaType(javaType)
842845
// because that doesn't return the right thing for primitive types
@@ -849,59 +852,55 @@ public <J> BasicDomainType<J> resolveBasicType(Class<J> javaType) {
849852
}
850853

851854
private <J> BasicDomainType<J> basicDomainType(Class<J> javaType) {
852-
final JavaType<J> javaTypeDescriptor =
853-
getTypeConfiguration().getJavaTypeRegistry().resolveDescriptor( javaType );
855+
final JavaType<J> javaTypeDescriptor = getJavaTypeRegistry().resolveDescriptor( javaType );
854856
final JdbcType jdbcType =
855857
javaTypeDescriptor.getRecommendedJdbcType( typeConfiguration.getCurrentBaseSqlTypeIndicators() );
856858
return javaType.isPrimitive()
857859
? new PrimitiveBasicTypeImpl<>( javaTypeDescriptor, jdbcType, javaType )
858860
: new BasicTypeImpl<>( javaTypeDescriptor, jdbcType );
859861
}
860862

863+
@SuppressWarnings("unchecked")
861864
public <J> EmbeddableDomainType<J> locateEmbeddable(Class<J> embeddableClass, Component component) {
862-
//noinspection unchecked
863-
EmbeddableDomainType<J> domainType = (EmbeddableDomainType<J>) embeddables.get( embeddableClass );
864-
if ( domainType == null ) {
865-
final List<EmbeddableDomainType<?>> embeddableDomainTypes = embeddablesToProcess.get( embeddableClass );
865+
final var domainType = (EmbeddableDomainType<J>) embeddables.get( embeddableClass );
866+
if ( domainType != null ) {
867+
return domainType;
868+
}
869+
else {
870+
final var embeddableDomainTypes = embeddablesToProcess.get( embeddableClass );
866871
if ( embeddableDomainTypes != null ) {
867-
for ( EmbeddableDomainType<?> embeddableDomainType : embeddableDomainTypes ) {
872+
for ( var embeddableDomainType : embeddableDomainTypes ) {
868873
final Component cachedComponent = componentByEmbeddable.get( embeddableDomainType );
869874
if ( cachedComponent.isSame( component ) ) {
870-
//noinspection unchecked
871-
domainType = (EmbeddableDomainType<J>) embeddableDomainType;
872-
break;
875+
return (EmbeddableDomainType<J>) embeddableDomainType;
873876
}
874877
else if ( cachedComponent.getComponentClass().equals( component.getComponentClass() ) ) {
875878
final int cachedComponentPropertySpan = cachedComponent.getPropertySpan();
876879
if ( cachedComponentPropertySpan != component.getPropertySpan() ) {
877-
throw new MappingException(
878-
"Encountered multiple component mappings for the same java class "
880+
throw new MappingException( "Encountered multiple component mappings for the same java class "
879881
+ embeddableClass.getName() +
880882
" with different property mappings. Every property mapping combination should have its own java class" );
881883
}
882884
else {
883885
for ( int i = 0; i < cachedComponentPropertySpan; i++ ) {
884886
if ( !cachedComponent.getProperty( i ).getName()
885887
.equals( component.getProperty( i ).getName() ) ) {
886-
throw new MappingException(
887-
"Encountered multiple component mappings for the same java class "
888+
throw new MappingException( "Encountered multiple component mappings for the same java class "
888889
+ embeddableClass.getName() +
889890
" with different property mappings. Every property mapping combination should have its own java class" );
890891
}
891892
}
892893
}
893-
//noinspection unchecked
894-
domainType = (EmbeddableDomainType<J>) embeddableDomainType;
895-
break;
894+
return (EmbeddableDomainType<J>) embeddableDomainType;
896895
}
897896
else {
898897
throw new MappingException( "Encountered multiple component mappings for the same java class "
899-
+ embeddableClass.getName() +
900-
" with different property mappings. Every property mapping combination should have its own java class" );
898+
+ embeddableClass.getName() +
899+
" with different property mappings. Every property mapping combination should have its own java class" );
901900
}
902901
}
903902
}
903+
return null;
904904
}
905-
return domainType;
906905
}
907906
}

0 commit comments

Comments
 (0)