Skip to content

Commit

Permalink
minor cleanups around instantiators
Browse files Browse the repository at this point in the history
  • Loading branch information
gavinking committed Jan 11, 2025
1 parent c3a1f1d commit 9dbf9d0
Show file tree
Hide file tree
Showing 25 changed files with 266 additions and 398 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public abstract class AbstractDynamicMapInstantiator implements Instantiator {

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

@Override
public boolean isInstance(Object object) {
if ( object instanceof Map<?,?> map ) {
return isSameRole( (String) map.get( TYPE_KEY ) );
}

// todo (6.0) : should this be an exception instead?
return false;
return object instanceof Map<?, ?> map
&& isSameRole( (String) map.get( TYPE_KEY ) );
// todo (6.0) : should this be an exception if there is no TYPE_KEY
}

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

@SuppressWarnings("rawtypes")
protected Map generateDataMap() {
final Map map = new HashMap();
//noinspection unchecked
protected Map<String,?> generateDataMap() {
final Map<String,Object> map = new HashMap<>();
map.put( TYPE_KEY, roleName );
return map;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@


import org.hibernate.bytecode.enhance.spi.interceptor.LazyAttributeLoadingInterceptor;
import org.hibernate.engine.spi.PersistentAttributeInterceptor;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.metamodel.spi.EntityInstantiator;
import org.hibernate.tuple.entity.EntityMetamodel;
Expand All @@ -30,28 +29,25 @@ public AbstractEntityInstantiatorPojo(
PersistentClass persistentClass,
JavaType<?> javaType) {
super( javaType.getJavaTypeClass() );

this.entityMetamodel = entityMetamodel;
this.proxyInterface = persistentClass.getProxyInterface();

//TODO this PojoEntityInstantiator appears to not be reused ?!
this.applyBytecodeInterception = isPersistentAttributeInterceptableType( persistentClass.getMappedClass() );
this.applyBytecodeInterception =
isPersistentAttributeInterceptableType( persistentClass.getMappedClass() );
}

protected Object applyInterception(Object entity) {
if ( !applyBytecodeInterception ) {
return entity;
if ( applyBytecodeInterception ) {
asPersistentAttributeInterceptable( entity )
.$$_hibernate_setInterceptor( new LazyAttributeLoadingInterceptor(
entityMetamodel.getName(),
null,
entityMetamodel.getBytecodeEnhancementMetadata()
.getLazyAttributesMetadata()
.getLazyAttributeNames(),
null
) );
}

PersistentAttributeInterceptor interceptor = new LazyAttributeLoadingInterceptor(
entityMetamodel.getName(),
null,
entityMetamodel.getBytecodeEnhancementMetadata()
.getLazyAttributesMetadata()
.getLazyAttributeNames(),
null
);
asPersistentAttributeInterceptable( entity ).$$_hibernate_setInterceptor( interceptor );
return entity;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
*/
package org.hibernate.metamodel.internal;

import org.hibernate.internal.util.ReflectHelper;
import org.hibernate.metamodel.spi.Instantiator;

import static org.hibernate.internal.util.ReflectHelper.isAbstractClass;

/**
* Base support for POJO-based instantiation
*
Expand All @@ -18,7 +19,7 @@ public abstract class AbstractPojoInstantiator implements Instantiator {

public AbstractPojoInstantiator(Class<?> mappedPojoClass) {
this.mappedPojoClass = mappedPojoClass;
this.isAbstract = ReflectHelper.isAbstractClass( mappedPojoClass );
this.isAbstract = isAbstractClass( mappedPojoClass );
}

public Class<?> getMappedPojoClass() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -461,8 +461,7 @@ private static <X, Y> AttributeMetadata<X, Y> determineAttributeMetadata(
propertyMapping,
attributeContext.getOwnerType(),
member,
AttributeClassification.ANY,
context
AttributeClassification.ANY
);
}
else if ( type instanceof EntityType ) {
Expand All @@ -471,8 +470,7 @@ else if ( type instanceof EntityType ) {
propertyMapping,
attributeContext.getOwnerType(),
member,
determineSingularAssociationClassification( member ),
context
determineSingularAssociationClassification( member )
);
}
else if ( type instanceof CollectionType ) {
Expand All @@ -486,8 +484,7 @@ else if ( type instanceof CollectionType ) {
member,
collectionClassification( elementType, isManyToMany ),
elementClassification( elementType, isManyToMany ),
indexClassification( value ),
context
indexClassification( value )
);
}
else if ( value instanceof OneToMany ) {
Expand Down Expand Up @@ -516,8 +513,7 @@ else if ( type instanceof ComponentType ) {
propertyMapping,
attributeContext.getOwnerType(),
member,
AttributeClassification.EMBEDDED,
context
AttributeClassification.EMBEDDED
);
}
else {
Expand All @@ -527,8 +523,7 @@ else if ( type instanceof ComponentType ) {
propertyMapping,
attributeContext.getOwnerType(),
member,
AttributeClassification.BASIC,
context
AttributeClassification.BASIC
);
}
throw new UnsupportedMappingException( "oops, we are missing something: " + propertyMapping );
Expand Down Expand Up @@ -557,9 +552,9 @@ else if ( elementType instanceof ComponentType ) {
return AttributeClassification.EMBEDDED;
}
else if ( elementType instanceof EntityType ) {
return isManyToMany ?
AttributeClassification.MANY_TO_MANY :
AttributeClassification.ONE_TO_MANY;
return isManyToMany
? AttributeClassification.MANY_TO_MANY
: AttributeClassification.ONE_TO_MANY;
}
else {
return AttributeClassification.BASIC;
Expand All @@ -569,9 +564,9 @@ else if ( elementType instanceof EntityType ) {
private static AttributeClassification collectionClassification(
org.hibernate.type.Type elementType, boolean isManyToMany) {
if ( elementType instanceof EntityType ) {
return isManyToMany ?
AttributeClassification.MANY_TO_MANY :
AttributeClassification.ONE_TO_MANY;
return isManyToMany
? AttributeClassification.MANY_TO_MANY
: AttributeClassification.ONE_TO_MANY;
}
else {
return AttributeClassification.ELEMENT_COLLECTION;
Expand Down Expand Up @@ -680,7 +675,7 @@ private static EmbeddableRepresentationStrategy ownerRepresentationStrategy(
return ownerBootDescriptor.getBuildingContext()
.getBootstrapContext()
.getRepresentationStrategySelector()
.resolveStrategy(ownerBootDescriptor, null,
.resolveStrategy( ownerBootDescriptor, null,
metadataContext.getRuntimeModelCreationContext() );
}
else {
Expand Down Expand Up @@ -728,66 +723,49 @@ private static Member resolveVirtualIdentifierMember( Property property, EntityP
final ManagedDomainType<?> ownerType = attributeContext.getOwnerType();
final Property property = attributeContext.getPropertyMapping();
final Type.PersistenceType persistenceType = ownerType.getPersistenceType();
if ( Type.PersistenceType.EMBEDDABLE == persistenceType ) {
return embeddedMemberResolver.resolveMember( attributeContext, metadataContext );
}
else if ( Type.PersistenceType.MAPPED_SUPERCLASS == persistenceType ) {
return resolveMappedSuperclassMember(
property,
(MappedSuperclassDomainType<?>) ownerType,
metadataContext
);
}
else if ( Type.PersistenceType.ENTITY == persistenceType ) {
return resolveEntityMember( property, getDeclaringEntity( (AbstractIdentifiableType<?>) ownerType, metadataContext ) );
}
else {
throw new IllegalArgumentException( "Unexpected owner type : " + persistenceType );
}
return switch ( persistenceType ) {
case ENTITY ->
resolveEntityMember( property,
getDeclaringEntity( (AbstractIdentifiableType<?>) ownerType, metadataContext ) );
case MAPPED_SUPERCLASS ->
resolveMappedSuperclassMember( property, (MappedSuperclassDomainType<?>) ownerType, metadataContext );
case EMBEDDABLE ->
embeddedMemberResolver.resolveMember( attributeContext, metadataContext );
default -> throw new IllegalArgumentException( "Unexpected owner type : " + persistenceType );
};
};

private static Member resolveEntityMember(Property property, EntityPersister declaringEntity) {
final String propertyName = property.getName();
final AttributeMapping attributeMapping = declaringEntity.findAttributeMapping( propertyName );
if ( attributeMapping == null ) {
// just like in #determineIdentifierJavaMember , this *should* indicate we have an IdClass mapping
return resolveVirtualIdentifierMember( property, declaringEntity );
}
else {
final Getter getter = getter( declaringEntity, property );
return getter instanceof PropertyAccessMapImpl.GetterImpl
? new MapMember( propertyName, property.getType().getReturnedClass() )
: getter.getMember();
}
return attributeMapping == null
// just like in #determineIdentifierJavaMember , this *should* indicate we have an IdClass mapping
? resolveVirtualIdentifierMember( property, declaringEntity )
: getter( declaringEntity, property, propertyName, property.getType().getReturnedClass() );
}

private static Member resolveMappedSuperclassMember(
Property property,
MappedSuperclassDomainType<?> ownerType,
MetadataContext metadataContext) {
final EntityPersister declaringEntity = getDeclaringEntity( (AbstractIdentifiableType<?>) ownerType, metadataContext );
MetadataContext context) {
final EntityPersister declaringEntity =
getDeclaringEntity( (AbstractIdentifiableType<?>) ownerType, context );
if ( declaringEntity != null ) {
return resolveEntityMember( property, declaringEntity );
}
else {
final ManagedDomainType<?> subType = ownerType.getSubTypes().iterator().next();
final Type.PersistenceType persistenceType = subType.getPersistenceType();
if ( persistenceType == Type.PersistenceType.ENTITY ) {
return resolveEntityMember( property, getDeclaringEntity( (AbstractIdentifiableType<?>) subType, metadataContext ) );
}
else if ( persistenceType == Type.PersistenceType.EMBEDDABLE ) {
return resolveEmbeddedMember( property, (EmbeddableDomainType<?>) subType, metadataContext );
}
else if ( persistenceType == Type.PersistenceType.MAPPED_SUPERCLASS ) {
return resolveMappedSuperclassMember(
property,
(MappedSuperclassDomainType<?>) subType,
metadataContext
);
}
else {
throw new IllegalArgumentException( "Unexpected subtype: " + persistenceType );
}
return switch ( persistenceType ) {
case ENTITY ->
resolveEntityMember( property,
getDeclaringEntity( (AbstractIdentifiableType<?>) subType, context ) );
case MAPPED_SUPERCLASS ->
resolveMappedSuperclassMember( property, (MappedSuperclassDomainType<?>) subType, context );
case EMBEDDABLE ->
resolveEmbeddedMember( property, (EmbeddableDomainType<?>) subType, context );
default -> throw new IllegalArgumentException( "Unexpected PersistenceType: " + persistenceType );
};
}
}

Expand All @@ -797,18 +775,12 @@ else if ( persistenceType == Type.PersistenceType.MAPPED_SUPERCLASS ) {
final EntityPersister declaringEntityMapping = getDeclaringEntity( identifiableType, metadataContext );
final EntityIdentifierMapping identifierMapping = declaringEntityMapping.getIdentifierMapping();
final Property propertyMapping = attributeContext.getPropertyMapping();
if ( !propertyMapping.getName().equals( identifierMapping.getAttributeName() ) ) {
// this *should* indicate processing part of an IdClass...
return virtualIdentifierMemberResolver.resolveMember( attributeContext, metadataContext );
}
return !propertyMapping.getName().equals( identifierMapping.getAttributeName() )
// this *should* indicate processing part of an IdClass...
? virtualIdentifierMemberResolver.resolveMember( attributeContext, metadataContext )
: getter( declaringEntityMapping, propertyMapping,
identifierMapping.getAttributeName(), identifierMapping.getJavaType().getJavaTypeClass() );

final Getter getter = getter( declaringEntityMapping, propertyMapping );
if ( getter instanceof PropertyAccessMapImpl.GetterImpl ) {
return new MapMember( identifierMapping.getAttributeName(), identifierMapping.getJavaType().getJavaTypeClass() );
}
else {
return getter.getMember();
}
};

private final MemberResolver versionMemberResolver = (attributeContext, metadataContext) -> {
Expand All @@ -824,20 +796,20 @@ else if ( persistenceType == Type.PersistenceType.MAPPED_SUPERCLASS ) {
// this should never happen, but to be safe...
throw new IllegalArgumentException( "Given property did not match declared version property" );
}

final Getter getter = getter( entityPersister, attributeContext.getPropertyMapping() );
if ( getter instanceof PropertyAccessMapImpl.GetterImpl ) {
return new MapMember( versionPropertyName, versionMapping.getJavaType().getJavaTypeClass() );
}
else {
return getter.getMember();
}
return getter( entityPersister, attributeContext.getPropertyMapping(),
versionPropertyName, versionMapping.getJavaType().getJavaTypeClass() );
};

private static Getter getter(EntityPersister declaringEntityMapping, Property propertyMapping) {
return declaringEntityMapping.getRepresentationStrategy()
.resolvePropertyAccess( propertyMapping )
.getGetter();
private static Member getter(EntityPersister persister, Property property, String name, Class<?> type) {
final Getter getter = getter( persister, property );
return getter instanceof PropertyAccessMapImpl.GetterImpl
? new MapMember( name, type )
: getter.getMember();
}

private static Getter getter(EntityPersister persister, Property property) {
return persister.getRepresentationStrategy()
.resolvePropertyAccess( property )
.getGetter();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ protected BaseAttributeMetadata(
Property propertyMapping,
ManagedDomainType<X> ownerType,
Member member,
AttributeClassification attributeClassification,
MetadataContext metadataContext) {
AttributeClassification attributeClassification) {
this.propertyMapping = propertyMapping;
this.ownerType = ownerType;
this.member = member;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public static boolean resolveIndex(String[] sortedComponentNames, String[] compo
index[i] = newIndex;
hasGaps = hasGaps || newIndex < 0;
}

return hasGaps;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,11 @@ public EmbeddableInstantiatorDynamicMap(
@Override
public Object instantiate(ValueAccess valuesAccess) {
final Map<?,?> dataMap = generateDataMap();

Object[] values = valuesAccess == null ? null : valuesAccess.getValues();
final Object[] values = valuesAccess == null ? null : valuesAccess.getValues();
if ( values != null ) {
final EmbeddableMappingType mappingType = runtimeDescriptorAccess.get();
mappingType.setValues( dataMap, values );
}

return dataMap;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,9 @@ public static EmbeddableInstantiatorPojoIndirecting of(
throw new IllegalArgumentException( "Can't determine field assignment for constructor: " + constructor );
}
final int[] index = new int[componentNames.length];
if ( EmbeddableHelper.resolveIndex( propertyNames, componentNames, index ) ) {
return new EmbeddableInstantiatorPojoIndirecting.EmbeddableInstantiatorPojoIndirectingWithGap( constructor, index );
}
else {
return new EmbeddableInstantiatorPojoIndirecting( constructor, index );
}
return EmbeddableHelper.resolveIndex( propertyNames, componentNames, index )
? new EmbeddableInstantiatorPojoIndirectingWithGap( constructor, index )
: new EmbeddableInstantiatorPojoIndirecting( constructor, index );
}

@Override
Expand Down
Loading

0 comments on commit 9dbf9d0

Please sign in to comment.