Skip to content

Commit

Permalink
HHH-18976 Get rid of dynamic array instantiations used only to resolv…
Browse files Browse the repository at this point in the history
…e type descriptors
  • Loading branch information
yrodiere committed Jan 14, 2025
1 parent 5a32bcb commit 62199d0
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ public class CollectionBatchLoaderArrayParam
private final SelectStatement sqlSelect;
private final JdbcOperationQuerySelect jdbcSelectOperation;

@AllowReflection
public CollectionBatchLoaderArrayParam(
int domainBatchSize,
LoadQueryInfluencers loadQueryInfluencers,
Expand All @@ -69,13 +68,12 @@ public CollectionBatchLoaderArrayParam(

final ForeignKeyDescriptor keyDescriptor = getLoadable().getKeyDescriptor();
final JdbcMapping jdbcMapping = keyDescriptor.getSingleJdbcMapping();
final Class<?> jdbcArrayClass = Array.newInstance( jdbcMapping.getJdbcJavaType().getJavaTypeClass(), 0 )
.getClass();
final Class<?> jdbcJavaTypeClass = jdbcMapping.getJdbcJavaType().getJavaTypeClass();
keyDomainType = getKeyType( keyDescriptor.getKeyPart() );

arrayJdbcMapping = MultiKeyLoadHelper.resolveArrayJdbcMapping(
jdbcMapping,
jdbcArrayClass,
jdbcJavaTypeClass,
getSessionFactory()
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ public class EntityBatchLoaderArrayParam<T>
* {@link EntityIdentifierMapping} is not available at that time. On first use, we know we
* have it available
*/
@AllowReflection
public EntityBatchLoaderArrayParam(
int domainBatchSize,
EntityMappingType entityDescriptor,
Expand All @@ -77,11 +76,10 @@ public EntityBatchLoaderArrayParam(
}

identifierMapping = (BasicEntityIdentifierMapping) getLoadable().getIdentifierMapping();
final Class<?> arrayClass =
Array.newInstance( identifierMapping.getJavaType().getJavaTypeClass(), 0 ).getClass();
final Class<?> idClass = identifierMapping.getJavaType().getJavaTypeClass();
arrayJdbcMapping = MultiKeyLoadHelper.resolveArrayJdbcMapping(
identifierMapping.getJdbcMapping(),
arrayClass,
idClass,
sessionFactory
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ public MultiIdEntityLoaderArrayParam(
EntityMappingType entityDescriptor,
SessionFactoryImplementor sessionFactory) {
super( entityDescriptor, sessionFactory );
final Class<?> idArrayClass = idArray.getClass();
final Class<?> idClass = idArray.getClass().getComponentType();
arrayJdbcMapping = resolveArrayJdbcMapping(
getIdentifierMapping().getJdbcMapping(),
idArrayClass,
idClass,
getSessionFactory()
);
jdbcParameter = new JdbcParameterImpl( arrayJdbcMapping );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,20 @@ public static boolean supportsSqlArrayType(Dialect dialect) {

public static JdbcMapping resolveArrayJdbcMapping(
JdbcMapping keyMapping,
Class<?> arrayClass,
Class<?> elementClass,
SessionFactoryImplementor sessionFactory) {
BasicType<?> arrayBasicType = sessionFactory.getTypeConfiguration().getBasicTypeRegistry()
.getRegisteredType( arrayClass );
.getRegisteredArrayType( elementClass );
if ( arrayBasicType != null ) {
return arrayBasicType;
}

final TypeConfiguration typeConfiguration = sessionFactory.getTypeConfiguration();
final JavaTypeRegistry javaTypeRegistry = typeConfiguration.getJavaTypeRegistry();

final JavaType<Object> rawArrayJavaType = javaTypeRegistry.resolveDescriptor( arrayClass );
if ( !(rawArrayJavaType instanceof BasicPluralJavaType<?> arrayJavaType) ) {
throw new IllegalArgumentException( "Expecting BasicPluralJavaType for array class `" + arrayClass.getName() + "`, but got `" + rawArrayJavaType + "`" );
final JavaType<?> rawArrayJavaType = javaTypeRegistry.resolveArrayDescriptor( elementClass );
if ( !(rawArrayJavaType instanceof BasicPluralJavaType<?> arrayJavaType ) ) {
throw new IllegalArgumentException( "Expecting BasicPluralJavaType for array class `" + elementClass.getTypeName() + "[]`, but got `" + rawArrayJavaType + "`" );
}

//noinspection unchecked,rawtypes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,14 @@
*/
public class MultiNaturalIdLoaderArrayParam<E> implements MultiNaturalIdLoader<E>, SqlArrayMultiKeyLoader {
private final EntityMappingType entityDescriptor;
private final Class<?> keyArrayClass;
private final Class<?> keyClass;

public MultiNaturalIdLoaderArrayParam(EntityMappingType entityDescriptor) {
assert entityDescriptor.getNaturalIdMapping() instanceof SimpleNaturalIdMapping;

this.entityDescriptor = entityDescriptor;

final Class<?> keyClass = entityDescriptor.getNaturalIdMapping().getJavaType().getJavaTypeClass();
this.keyArrayClass = LoaderHelper.createTypedArray( keyClass, 0 ).getClass();
this.keyClass = entityDescriptor.getNaturalIdMapping().getJavaType().getJavaTypeClass();
}

@Override
Expand Down Expand Up @@ -77,7 +76,7 @@ public <K> List<E> multiLoad(K[] naturalIds, MultiNaturalIdLoadOptions loadOptio

final JdbcMapping arrayJdbcMapping = MultiKeyLoadHelper.resolveArrayJdbcMapping(
getNaturalIdMapping().getSingleJdbcMapping(),
keyArrayClass,
keyClass,
sessionFactory
);
final JdbcParameter jdbcParameter = new JdbcParameterImpl( arrayJdbcMapping );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ public <J> BasicType<J> getRegisteredType(Class<J> javaType) {
return getRegisteredType( javaType.getTypeName() );
}

public BasicType<?> getRegisteredArrayType(java.lang.reflect.Type javaElementType) {
return getRegisteredType( javaElementType.getTypeName() + "[]" );
}

public <J> BasicType<J> resolve(BasicTypeReference<J> basicTypeReference) {
return getRegisteredType( basicTypeReference.getName() );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class JavaTypeRegistry implements JavaTypeBaseline.BaselineTarget, Serial
private static final Logger log = Logger.getLogger( JavaTypeRegistry.class );

private final TypeConfiguration typeConfiguration;
private final ConcurrentHashMap<Type, JavaType<?>> descriptorsByType = new ConcurrentHashMap<>();
private final ConcurrentHashMap<String, JavaType<?>> descriptorsByTypeName = new ConcurrentHashMap<>();

public JavaTypeRegistry(TypeConfiguration typeConfiguration) {
this.typeConfiguration = typeConfiguration;
Expand All @@ -56,7 +56,7 @@ public void addBaselineDescriptor(JavaType<?> descriptor) {
@Override
public void addBaselineDescriptor(Type describedJavaType, JavaType<?> descriptor) {
performInjections( descriptor );
descriptorsByType.put( describedJavaType, descriptor );
descriptorsByTypeName.put( describedJavaType.getTypeName(), descriptor );
}

private void performInjections(JavaType<?> descriptor) {
Expand All @@ -71,15 +71,15 @@ private void performInjections(JavaType<?> descriptor) {
// descriptor access

public void forEachDescriptor(Consumer<JavaType<?>> consumer) {
descriptorsByType.values().forEach( consumer );
descriptorsByTypeName.values().forEach( consumer );
}

public <T> JavaType<T> getDescriptor(Type javaType) {
return resolveDescriptor( javaType );
}

public void addDescriptor(JavaType<?> descriptor) {
JavaType<?> old = descriptorsByType.put( descriptor.getJavaType(), descriptor );
JavaType<?> old = descriptorsByTypeName.put( descriptor.getJavaType().getTypeName(), descriptor );
if ( old != null ) {
log.debugf(
"JavaTypeRegistry entry replaced : %s -> %s (was %s)",
Expand All @@ -93,74 +93,84 @@ public void addDescriptor(JavaType<?> descriptor) {

public <J> JavaType<J> findDescriptor(Type javaType) {
//noinspection unchecked
return (JavaType<J>) descriptorsByType.get( javaType );
return (JavaType<J>) descriptorsByTypeName.get( javaType.getTypeName() );
}

public <J> JavaType<J> resolveDescriptor(Type javaType, Supplier<JavaType<J>> creator) {
final JavaType<?> cached = descriptorsByType.get( javaType );
return resolveDescriptor( javaType.getTypeName(), creator );
}

private <J> JavaType<J> resolveDescriptor(String javaTypeName, Supplier<JavaType<J>> creator) {
final JavaType<?> cached = descriptorsByTypeName.get( javaTypeName );
if ( cached != null ) {
//noinspection unchecked
return (JavaType<J>) cached;
}

final JavaType<J> created = creator.get();
descriptorsByType.put( javaType, created );
descriptorsByTypeName.put( javaTypeName, created );
return created;
}

public <J> JavaType<J> resolveDescriptor(Type javaType) {
return resolveDescriptor( javaType, (elementJavaType, typeConfiguration) -> {
final MutabilityPlan<J> determinedPlan = RegistryHelper.INSTANCE.determineMutabilityPlan(
elementJavaType,
typeConfiguration
);
if ( determinedPlan != null ) {
return determinedPlan;
}
return resolveDescriptor( javaType, JavaTypeRegistry::createMutabilityPlan );
}

private static <J> MutabilityPlan<?> createMutabilityPlan(Type elementJavaType, TypeConfiguration typeConfiguration) {
final MutabilityPlan<J> determinedPlan = RegistryHelper.INSTANCE.determineMutabilityPlan(
elementJavaType,
typeConfiguration
);
if ( determinedPlan != null ) {
return determinedPlan;
}

return MutableMutabilityPlan.INSTANCE;
}

return MutableMutabilityPlan.INSTANCE;
} );
public JavaType<?> resolveArrayDescriptor(Class<?> elementJavaType) {
return resolveDescriptor( elementJavaType.getTypeName() + "[]",
() -> createArrayTypeDescriptor( elementJavaType, JavaTypeRegistry::createMutabilityPlan) );
}

public <J> JavaType<J> resolveDescriptor(
Type javaType,
BiFunction<Type, TypeConfiguration, MutabilityPlan<?>> mutabilityPlanCreator) {
return resolveDescriptor(
javaType,
javaType.getTypeName(),
() -> {
if ( javaType instanceof ParameterizedType parameterizedType ) {
final JavaType<J> rawType = findDescriptor( parameterizedType.getRawType() );
if ( rawType != null ) {
return rawType.createJavaType( parameterizedType, typeConfiguration );
}
}
final Type elementJavaType;
JavaType<J> elementTypeDescriptor;
if ( javaType instanceof Class<?> && ( (Class<?>) javaType ).isArray() ) {
elementJavaType = ( (Class<?>) javaType ).getComponentType();
elementTypeDescriptor = findDescriptor( elementJavaType );
}
else {
elementJavaType = javaType;
elementTypeDescriptor = null;
}
if ( elementTypeDescriptor == null ) {
//noinspection unchecked
elementTypeDescriptor = RegistryHelper.INSTANCE.createTypeDescriptor(
elementJavaType,
() -> (MutabilityPlan<J>) mutabilityPlanCreator.apply( elementJavaType, typeConfiguration ),
typeConfiguration
);
}
if ( javaType != elementJavaType ) {
else if ( javaType instanceof Class<?> javaClass && javaClass.isArray() ) {
//noinspection unchecked
return (JavaType<J>) new ArrayJavaType<>( elementTypeDescriptor );
return (JavaType<J>) createArrayTypeDescriptor( javaClass.getComponentType(), mutabilityPlanCreator );
}
return elementTypeDescriptor;
return createTypeDescriptor( javaType, mutabilityPlanCreator );
}
);
}

private <J> JavaType<J[]> createArrayTypeDescriptor(Class<J> elementJavaType, BiFunction<Type, TypeConfiguration, MutabilityPlan<?>> mutabilityPlanCreator) {
JavaType<J> elementTypeDescriptor = findDescriptor( elementJavaType );
if ( elementTypeDescriptor == null ) {
elementTypeDescriptor = createTypeDescriptor( elementJavaType, mutabilityPlanCreator );
}
return new ArrayJavaType<>( elementTypeDescriptor );
}

private <J> JavaType<J> createTypeDescriptor(Type javaType, BiFunction<Type, TypeConfiguration, MutabilityPlan<?>> mutabilityPlanCreator) {
//noinspection unchecked
return RegistryHelper.INSTANCE.createTypeDescriptor(
javaType,
() -> (MutabilityPlan<J>) mutabilityPlanCreator.apply( javaType, typeConfiguration ),
typeConfiguration
);
}

public <J> JavaType<J> resolveManagedTypeDescriptor(Type javaType) {
return resolveManagedTypeDescriptor( javaType, false );
}
Expand Down

0 comments on commit 62199d0

Please sign in to comment.