Skip to content

Commit 9dbf9d0

Browse files
committed
minor cleanups around instantiators
1 parent c3a1f1d commit 9dbf9d0

25 files changed

+266
-398
lines changed

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

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public abstract class AbstractDynamicMapInstantiator implements Instantiator {
2121

2222
public AbstractDynamicMapInstantiator(String roleName) {
2323
if ( roleName == null ) {
24-
throw new IllegalArgumentException( "`roleName` passed to dynamic-map instantiator cannot be null" );
24+
throw new IllegalArgumentException( "Role name passed to dynamic map instantiator cannot be null" );
2525
}
2626
this.roleName = roleName;
2727
}
@@ -32,12 +32,9 @@ public String getRoleName() {
3232

3333
@Override
3434
public boolean isInstance(Object object) {
35-
if ( object instanceof Map<?,?> map ) {
36-
return isSameRole( (String) map.get( TYPE_KEY ) );
37-
}
38-
39-
// todo (6.0) : should this be an exception instead?
40-
return false;
35+
return object instanceof Map<?, ?> map
36+
&& isSameRole( (String) map.get( TYPE_KEY ) );
37+
// todo (6.0) : should this be an exception if there is no TYPE_KEY
4138
}
4239

4340
protected boolean isSameRole(String type) {
@@ -49,10 +46,8 @@ public boolean isSameClass(Object object) {
4946
return isInstance( object );
5047
}
5148

52-
@SuppressWarnings("rawtypes")
53-
protected Map generateDataMap() {
54-
final Map map = new HashMap();
55-
//noinspection unchecked
49+
protected Map<String,?> generateDataMap() {
50+
final Map<String,Object> map = new HashMap<>();
5651
map.put( TYPE_KEY, roleName );
5752
return map;
5853
}

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

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77

88
import org.hibernate.bytecode.enhance.spi.interceptor.LazyAttributeLoadingInterceptor;
9-
import org.hibernate.engine.spi.PersistentAttributeInterceptor;
109
import org.hibernate.mapping.PersistentClass;
1110
import org.hibernate.metamodel.spi.EntityInstantiator;
1211
import org.hibernate.tuple.entity.EntityMetamodel;
@@ -30,28 +29,25 @@ public AbstractEntityInstantiatorPojo(
3029
PersistentClass persistentClass,
3130
JavaType<?> javaType) {
3231
super( javaType.getJavaTypeClass() );
33-
3432
this.entityMetamodel = entityMetamodel;
3533
this.proxyInterface = persistentClass.getProxyInterface();
36-
3734
//TODO this PojoEntityInstantiator appears to not be reused ?!
38-
this.applyBytecodeInterception = isPersistentAttributeInterceptableType( persistentClass.getMappedClass() );
35+
this.applyBytecodeInterception =
36+
isPersistentAttributeInterceptableType( persistentClass.getMappedClass() );
3937
}
4038

4139
protected Object applyInterception(Object entity) {
42-
if ( !applyBytecodeInterception ) {
43-
return entity;
40+
if ( applyBytecodeInterception ) {
41+
asPersistentAttributeInterceptable( entity )
42+
.$$_hibernate_setInterceptor( new LazyAttributeLoadingInterceptor(
43+
entityMetamodel.getName(),
44+
null,
45+
entityMetamodel.getBytecodeEnhancementMetadata()
46+
.getLazyAttributesMetadata()
47+
.getLazyAttributeNames(),
48+
null
49+
) );
4450
}
45-
46-
PersistentAttributeInterceptor interceptor = new LazyAttributeLoadingInterceptor(
47-
entityMetamodel.getName(),
48-
null,
49-
entityMetamodel.getBytecodeEnhancementMetadata()
50-
.getLazyAttributesMetadata()
51-
.getLazyAttributeNames(),
52-
null
53-
);
54-
asPersistentAttributeInterceptable( entity ).$$_hibernate_setInterceptor( interceptor );
5551
return entity;
5652
}
5753

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
*/
55
package org.hibernate.metamodel.internal;
66

7-
import org.hibernate.internal.util.ReflectHelper;
87
import org.hibernate.metamodel.spi.Instantiator;
98

9+
import static org.hibernate.internal.util.ReflectHelper.isAbstractClass;
10+
1011
/**
1112
* Base support for POJO-based instantiation
1213
*
@@ -18,7 +19,7 @@ public abstract class AbstractPojoInstantiator implements Instantiator {
1819

1920
public AbstractPojoInstantiator(Class<?> mappedPojoClass) {
2021
this.mappedPojoClass = mappedPojoClass;
21-
this.isAbstract = ReflectHelper.isAbstractClass( mappedPojoClass );
22+
this.isAbstract = isAbstractClass( mappedPojoClass );
2223
}
2324

2425
public Class<?> getMappedPojoClass() {

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

Lines changed: 56 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -461,8 +461,7 @@ private static <X, Y> AttributeMetadata<X, Y> determineAttributeMetadata(
461461
propertyMapping,
462462
attributeContext.getOwnerType(),
463463
member,
464-
AttributeClassification.ANY,
465-
context
464+
AttributeClassification.ANY
466465
);
467466
}
468467
else if ( type instanceof EntityType ) {
@@ -471,8 +470,7 @@ else if ( type instanceof EntityType ) {
471470
propertyMapping,
472471
attributeContext.getOwnerType(),
473472
member,
474-
determineSingularAssociationClassification( member ),
475-
context
473+
determineSingularAssociationClassification( member )
476474
);
477475
}
478476
else if ( type instanceof CollectionType ) {
@@ -486,8 +484,7 @@ else if ( type instanceof CollectionType ) {
486484
member,
487485
collectionClassification( elementType, isManyToMany ),
488486
elementClassification( elementType, isManyToMany ),
489-
indexClassification( value ),
490-
context
487+
indexClassification( value )
491488
);
492489
}
493490
else if ( value instanceof OneToMany ) {
@@ -516,8 +513,7 @@ else if ( type instanceof ComponentType ) {
516513
propertyMapping,
517514
attributeContext.getOwnerType(),
518515
member,
519-
AttributeClassification.EMBEDDED,
520-
context
516+
AttributeClassification.EMBEDDED
521517
);
522518
}
523519
else {
@@ -527,8 +523,7 @@ else if ( type instanceof ComponentType ) {
527523
propertyMapping,
528524
attributeContext.getOwnerType(),
529525
member,
530-
AttributeClassification.BASIC,
531-
context
526+
AttributeClassification.BASIC
532527
);
533528
}
534529
throw new UnsupportedMappingException( "oops, we are missing something: " + propertyMapping );
@@ -557,9 +552,9 @@ else if ( elementType instanceof ComponentType ) {
557552
return AttributeClassification.EMBEDDED;
558553
}
559554
else if ( elementType instanceof EntityType ) {
560-
return isManyToMany ?
561-
AttributeClassification.MANY_TO_MANY :
562-
AttributeClassification.ONE_TO_MANY;
555+
return isManyToMany
556+
? AttributeClassification.MANY_TO_MANY
557+
: AttributeClassification.ONE_TO_MANY;
563558
}
564559
else {
565560
return AttributeClassification.BASIC;
@@ -569,9 +564,9 @@ else if ( elementType instanceof EntityType ) {
569564
private static AttributeClassification collectionClassification(
570565
org.hibernate.type.Type elementType, boolean isManyToMany) {
571566
if ( elementType instanceof EntityType ) {
572-
return isManyToMany ?
573-
AttributeClassification.MANY_TO_MANY :
574-
AttributeClassification.ONE_TO_MANY;
567+
return isManyToMany
568+
? AttributeClassification.MANY_TO_MANY
569+
: AttributeClassification.ONE_TO_MANY;
575570
}
576571
else {
577572
return AttributeClassification.ELEMENT_COLLECTION;
@@ -680,7 +675,7 @@ private static EmbeddableRepresentationStrategy ownerRepresentationStrategy(
680675
return ownerBootDescriptor.getBuildingContext()
681676
.getBootstrapContext()
682677
.getRepresentationStrategySelector()
683-
.resolveStrategy(ownerBootDescriptor, null,
678+
.resolveStrategy( ownerBootDescriptor, null,
684679
metadataContext.getRuntimeModelCreationContext() );
685680
}
686681
else {
@@ -728,66 +723,49 @@ private static Member resolveVirtualIdentifierMember( Property property, EntityP
728723
final ManagedDomainType<?> ownerType = attributeContext.getOwnerType();
729724
final Property property = attributeContext.getPropertyMapping();
730725
final Type.PersistenceType persistenceType = ownerType.getPersistenceType();
731-
if ( Type.PersistenceType.EMBEDDABLE == persistenceType ) {
732-
return embeddedMemberResolver.resolveMember( attributeContext, metadataContext );
733-
}
734-
else if ( Type.PersistenceType.MAPPED_SUPERCLASS == persistenceType ) {
735-
return resolveMappedSuperclassMember(
736-
property,
737-
(MappedSuperclassDomainType<?>) ownerType,
738-
metadataContext
739-
);
740-
}
741-
else if ( Type.PersistenceType.ENTITY == persistenceType ) {
742-
return resolveEntityMember( property, getDeclaringEntity( (AbstractIdentifiableType<?>) ownerType, metadataContext ) );
743-
}
744-
else {
745-
throw new IllegalArgumentException( "Unexpected owner type : " + persistenceType );
746-
}
726+
return switch ( persistenceType ) {
727+
case ENTITY ->
728+
resolveEntityMember( property,
729+
getDeclaringEntity( (AbstractIdentifiableType<?>) ownerType, metadataContext ) );
730+
case MAPPED_SUPERCLASS ->
731+
resolveMappedSuperclassMember( property, (MappedSuperclassDomainType<?>) ownerType, metadataContext );
732+
case EMBEDDABLE ->
733+
embeddedMemberResolver.resolveMember( attributeContext, metadataContext );
734+
default -> throw new IllegalArgumentException( "Unexpected owner type : " + persistenceType );
735+
};
747736
};
748737

749738
private static Member resolveEntityMember(Property property, EntityPersister declaringEntity) {
750739
final String propertyName = property.getName();
751740
final AttributeMapping attributeMapping = declaringEntity.findAttributeMapping( propertyName );
752-
if ( attributeMapping == null ) {
753-
// just like in #determineIdentifierJavaMember , this *should* indicate we have an IdClass mapping
754-
return resolveVirtualIdentifierMember( property, declaringEntity );
755-
}
756-
else {
757-
final Getter getter = getter( declaringEntity, property );
758-
return getter instanceof PropertyAccessMapImpl.GetterImpl
759-
? new MapMember( propertyName, property.getType().getReturnedClass() )
760-
: getter.getMember();
761-
}
741+
return attributeMapping == null
742+
// just like in #determineIdentifierJavaMember , this *should* indicate we have an IdClass mapping
743+
? resolveVirtualIdentifierMember( property, declaringEntity )
744+
: getter( declaringEntity, property, propertyName, property.getType().getReturnedClass() );
762745
}
763746

764747
private static Member resolveMappedSuperclassMember(
765748
Property property,
766749
MappedSuperclassDomainType<?> ownerType,
767-
MetadataContext metadataContext) {
768-
final EntityPersister declaringEntity = getDeclaringEntity( (AbstractIdentifiableType<?>) ownerType, metadataContext );
750+
MetadataContext context) {
751+
final EntityPersister declaringEntity =
752+
getDeclaringEntity( (AbstractIdentifiableType<?>) ownerType, context );
769753
if ( declaringEntity != null ) {
770754
return resolveEntityMember( property, declaringEntity );
771755
}
772756
else {
773757
final ManagedDomainType<?> subType = ownerType.getSubTypes().iterator().next();
774758
final Type.PersistenceType persistenceType = subType.getPersistenceType();
775-
if ( persistenceType == Type.PersistenceType.ENTITY ) {
776-
return resolveEntityMember( property, getDeclaringEntity( (AbstractIdentifiableType<?>) subType, metadataContext ) );
777-
}
778-
else if ( persistenceType == Type.PersistenceType.EMBEDDABLE ) {
779-
return resolveEmbeddedMember( property, (EmbeddableDomainType<?>) subType, metadataContext );
780-
}
781-
else if ( persistenceType == Type.PersistenceType.MAPPED_SUPERCLASS ) {
782-
return resolveMappedSuperclassMember(
783-
property,
784-
(MappedSuperclassDomainType<?>) subType,
785-
metadataContext
786-
);
787-
}
788-
else {
789-
throw new IllegalArgumentException( "Unexpected subtype: " + persistenceType );
790-
}
759+
return switch ( persistenceType ) {
760+
case ENTITY ->
761+
resolveEntityMember( property,
762+
getDeclaringEntity( (AbstractIdentifiableType<?>) subType, context ) );
763+
case MAPPED_SUPERCLASS ->
764+
resolveMappedSuperclassMember( property, (MappedSuperclassDomainType<?>) subType, context );
765+
case EMBEDDABLE ->
766+
resolveEmbeddedMember( property, (EmbeddableDomainType<?>) subType, context );
767+
default -> throw new IllegalArgumentException( "Unexpected PersistenceType: " + persistenceType );
768+
};
791769
}
792770
}
793771

@@ -797,18 +775,12 @@ else if ( persistenceType == Type.PersistenceType.MAPPED_SUPERCLASS ) {
797775
final EntityPersister declaringEntityMapping = getDeclaringEntity( identifiableType, metadataContext );
798776
final EntityIdentifierMapping identifierMapping = declaringEntityMapping.getIdentifierMapping();
799777
final Property propertyMapping = attributeContext.getPropertyMapping();
800-
if ( !propertyMapping.getName().equals( identifierMapping.getAttributeName() ) ) {
801-
// this *should* indicate processing part of an IdClass...
802-
return virtualIdentifierMemberResolver.resolveMember( attributeContext, metadataContext );
803-
}
778+
return !propertyMapping.getName().equals( identifierMapping.getAttributeName() )
779+
// this *should* indicate processing part of an IdClass...
780+
? virtualIdentifierMemberResolver.resolveMember( attributeContext, metadataContext )
781+
: getter( declaringEntityMapping, propertyMapping,
782+
identifierMapping.getAttributeName(), identifierMapping.getJavaType().getJavaTypeClass() );
804783

805-
final Getter getter = getter( declaringEntityMapping, propertyMapping );
806-
if ( getter instanceof PropertyAccessMapImpl.GetterImpl ) {
807-
return new MapMember( identifierMapping.getAttributeName(), identifierMapping.getJavaType().getJavaTypeClass() );
808-
}
809-
else {
810-
return getter.getMember();
811-
}
812784
};
813785

814786
private final MemberResolver versionMemberResolver = (attributeContext, metadataContext) -> {
@@ -824,20 +796,20 @@ else if ( persistenceType == Type.PersistenceType.MAPPED_SUPERCLASS ) {
824796
// this should never happen, but to be safe...
825797
throw new IllegalArgumentException( "Given property did not match declared version property" );
826798
}
827-
828-
final Getter getter = getter( entityPersister, attributeContext.getPropertyMapping() );
829-
if ( getter instanceof PropertyAccessMapImpl.GetterImpl ) {
830-
return new MapMember( versionPropertyName, versionMapping.getJavaType().getJavaTypeClass() );
831-
}
832-
else {
833-
return getter.getMember();
834-
}
799+
return getter( entityPersister, attributeContext.getPropertyMapping(),
800+
versionPropertyName, versionMapping.getJavaType().getJavaTypeClass() );
835801
};
836802

837-
private static Getter getter(EntityPersister declaringEntityMapping, Property propertyMapping) {
838-
return declaringEntityMapping.getRepresentationStrategy()
839-
.resolvePropertyAccess( propertyMapping )
840-
.getGetter();
803+
private static Member getter(EntityPersister persister, Property property, String name, Class<?> type) {
804+
final Getter getter = getter( persister, property );
805+
return getter instanceof PropertyAccessMapImpl.GetterImpl
806+
? new MapMember( name, type )
807+
: getter.getMember();
841808
}
842809

810+
private static Getter getter(EntityPersister persister, Property property) {
811+
return persister.getRepresentationStrategy()
812+
.resolvePropertyAccess( property )
813+
.getGetter();
814+
}
843815
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ protected BaseAttributeMetadata(
2828
Property propertyMapping,
2929
ManagedDomainType<X> ownerType,
3030
Member member,
31-
AttributeClassification attributeClassification,
32-
MetadataContext metadataContext) {
31+
AttributeClassification attributeClassification) {
3332
this.propertyMapping = propertyMapping;
3433
this.ownerType = ownerType;
3534
this.member = member;

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ public static boolean resolveIndex(String[] sortedComponentNames, String[] compo
2626
index[i] = newIndex;
2727
hasGaps = hasGaps || newIndex < 0;
2828
}
29-
3029
return hasGaps;
3130
}
3231
}

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,11 @@ public EmbeddableInstantiatorDynamicMap(
3131
@Override
3232
public Object instantiate(ValueAccess valuesAccess) {
3333
final Map<?,?> dataMap = generateDataMap();
34-
35-
Object[] values = valuesAccess == null ? null : valuesAccess.getValues();
34+
final Object[] values = valuesAccess == null ? null : valuesAccess.getValues();
3635
if ( values != null ) {
3736
final EmbeddableMappingType mappingType = runtimeDescriptorAccess.get();
3837
mappingType.setValues( dataMap, values );
3938
}
40-
4139
return dataMap;
4240
}
4341
}

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,9 @@ public static EmbeddableInstantiatorPojoIndirecting of(
3131
throw new IllegalArgumentException( "Can't determine field assignment for constructor: " + constructor );
3232
}
3333
final int[] index = new int[componentNames.length];
34-
if ( EmbeddableHelper.resolveIndex( propertyNames, componentNames, index ) ) {
35-
return new EmbeddableInstantiatorPojoIndirecting.EmbeddableInstantiatorPojoIndirectingWithGap( constructor, index );
36-
}
37-
else {
38-
return new EmbeddableInstantiatorPojoIndirecting( constructor, index );
39-
}
34+
return EmbeddableHelper.resolveIndex( propertyNames, componentNames, index )
35+
? new EmbeddableInstantiatorPojoIndirectingWithGap( constructor, index )
36+
: new EmbeddableInstantiatorPojoIndirecting( constructor, index );
4037
}
4138

4239
@Override

0 commit comments

Comments
 (0)