refactor duplicated code in SimpleValue/BasicValue

This commit is contained in:
Gavin King 2024-12-30 23:32:31 +01:00
parent a283fc982b
commit 56a780b9e1
2 changed files with 31 additions and 66 deletions

View File

@ -420,7 +420,7 @@ protected Resolution<?> buildResolution() {
if ( typeParameters != null if ( typeParameters != null
&& parseBoolean( typeParameters.getProperty(DynamicParameterizedType.IS_DYNAMIC) ) && parseBoolean( typeParameters.getProperty(DynamicParameterizedType.IS_DYNAMIC) )
&& typeParameters.get(DynamicParameterizedType.PARAMETER_TYPE) == null ) { && typeParameters.get(DynamicParameterizedType.PARAMETER_TYPE) == null ) {
createParameterImpl(); getTypeParameters().put( DynamicParameterizedType.PARAMETER_TYPE, createParameterType() );
} }
return buildResolution( typeParameters ); return buildResolution( typeParameters );
} }
@ -1046,9 +1046,9 @@ private Properties getCustomTypeProperties() {
private UserType<?> getConfiguredUserTypeBean(Class<? extends UserType<?>> explicitCustomType, Properties properties) { private UserType<?> getConfiguredUserTypeBean(Class<? extends UserType<?>> explicitCustomType, Properties properties) {
final UserType<?> typeInstance = final UserType<?> typeInstance =
!getBuildingContext().getBuildingOptions().isAllowExtensionsInCdi() getBuildingContext().getBuildingOptions().isAllowExtensionsInCdi()
? FallbackBeanInstanceProducer.INSTANCE.produceBeanInstance( explicitCustomType ) ? getUserTypeBean( explicitCustomType, properties ).getBeanInstance()
: getUserTypeBean( explicitCustomType, properties ).getBeanInstance(); : FallbackBeanInstanceProducer.INSTANCE.produceBeanInstance( explicitCustomType );
if ( typeInstance instanceof TypeConfigurationAware configurationAware ) { if ( typeInstance instanceof TypeConfigurationAware configurationAware ) {
configurationAware.setTypeConfiguration( getTypeConfiguration() ); configurationAware.setTypeConfiguration( getTypeConfiguration() );
@ -1057,7 +1057,7 @@ private UserType<?> getConfiguredUserTypeBean(Class<? extends UserType<?>> expli
if ( typeInstance instanceof DynamicParameterizedType ) { if ( typeInstance instanceof DynamicParameterizedType ) {
if ( parseBoolean( properties.getProperty( DynamicParameterizedType.IS_DYNAMIC ) ) ) { if ( parseBoolean( properties.getProperty( DynamicParameterizedType.IS_DYNAMIC ) ) ) {
if ( properties.get( DynamicParameterizedType.PARAMETER_TYPE ) == null ) { if ( properties.get( DynamicParameterizedType.PARAMETER_TYPE ) == null ) {
properties.put( DynamicParameterizedType.PARAMETER_TYPE, makeParameterImpl() ); properties.put( DynamicParameterizedType.PARAMETER_TYPE, createParameterType() );
} }
} }
} }
@ -1071,13 +1071,12 @@ private UserType<?> getConfiguredUserTypeBean(Class<? extends UserType<?>> expli
private <T> ManagedBean<T> getUserTypeBean(Class<T> explicitCustomType, Properties properties) { private <T> ManagedBean<T> getUserTypeBean(Class<T> explicitCustomType, Properties properties) {
final BeanInstanceProducer producer = getBuildingContext().getBootstrapContext().getCustomTypeProducer(); final BeanInstanceProducer producer = getBuildingContext().getBootstrapContext().getCustomTypeProducer();
final ManagedBeanRegistry registry = getServiceRegistry().requireService( ManagedBeanRegistry.class );
if ( isNotEmpty( properties ) ) { if ( isNotEmpty( properties ) ) {
final String name = explicitCustomType.getName() + COUNTER++; final String name = explicitCustomType.getName() + COUNTER++;
return registry.getBean( name, explicitCustomType, producer ); return getManagedBeanRegistry().getBean( name, explicitCustomType, producer );
} }
else { else {
return registry.getBean( explicitCustomType, producer ); return getManagedBeanRegistry().getBean( explicitCustomType, producer );
} }
} }

View File

@ -54,6 +54,7 @@
import org.hibernate.usertype.DynamicParameterizedType; import org.hibernate.usertype.DynamicParameterizedType;
import jakarta.persistence.AttributeConverter; import jakarta.persistence.AttributeConverter;
import org.hibernate.usertype.DynamicParameterizedType.ParameterType;
import static java.lang.Boolean.parseBoolean; import static java.lang.Boolean.parseBoolean;
import static org.hibernate.boot.model.convert.spi.ConverterDescriptor.TYPE_NAME_PREFIX; import static org.hibernate.boot.model.convert.spi.ConverterDescriptor.TYPE_NAME_PREFIX;
@ -914,54 +915,16 @@ public void setJpaAttributeConverterDescriptor(ConverterDescriptor descriptor) {
this.attributeConverterDescriptor = descriptor; this.attributeConverterDescriptor = descriptor;
} }
protected void createParameterImpl() {
try {
final String[] columnNames = new String[ columns.size() ];
final Long[] columnLengths = new Long[ columns.size() ];
for ( int i = 0; i < columns.size(); i++ ) {
final Selectable selectable = columns.get(i);
if ( selectable instanceof Column column ) {
columnNames[i] = column.getName();
columnLengths[i] = column.getLength();
}
}
final MemberDetails attributeMember = (MemberDetails) typeParameters.get( DynamicParameterizedType.XPROPERTY );
// todo : not sure this works for handling @MapKeyEnumerated
final Annotation[] annotations = getAnnotations( attributeMember );
typeParameters.put(
DynamicParameterizedType.PARAMETER_TYPE,
new ParameterTypeImpl(
classLoaderService()
.classForTypeName( typeParameters.getProperty(DynamicParameterizedType.RETURNED_CLASS) ),
attributeMember != null ? attributeMember.getType() : null,
annotations,
table.getCatalog(),
table.getSchema(),
table.getName(),
parseBoolean( typeParameters.getProperty(DynamicParameterizedType.IS_PRIMARY_KEY) ),
columnNames,
columnLengths
)
);
}
catch ( ClassLoadingException e ) {
throw new MappingException( "Could not create DynamicParameterizedType for type: " + typeName, e );
}
}
private static final Annotation[] NO_ANNOTATIONS = new Annotation[0]; private static final Annotation[] NO_ANNOTATIONS = new Annotation[0];
private static Annotation[] getAnnotations(MemberDetails memberDetails) { private static Annotation[] getAnnotations(MemberDetails memberDetails) {
final Collection<? extends Annotation> directAnnotationUsages = memberDetails == null final Collection<? extends Annotation> directAnnotationUsages =
? null memberDetails == null ? null
: memberDetails.getDirectAnnotationUsages(); : memberDetails.getDirectAnnotationUsages();
return directAnnotationUsages == null return directAnnotationUsages == null ? NO_ANNOTATIONS
? NO_ANNOTATIONS
: directAnnotationUsages.toArray( Annotation[]::new ); : directAnnotationUsages.toArray( Annotation[]::new );
} }
public DynamicParameterizedType.ParameterType makeParameterImpl() { protected ParameterType createParameterType() {
try { try {
final String[] columnNames = new String[ columns.size() ]; final String[] columnNames = new String[ columns.size() ];
final Long[] columnLengths = new Long[ columns.size() ]; final Long[] columnLengths = new Long[ columns.size() ];
@ -974,28 +937,31 @@ public DynamicParameterizedType.ParameterType makeParameterImpl() {
} }
} }
final MemberDetails attributeMember = (MemberDetails) typeParameters.get( DynamicParameterizedType.XPROPERTY );
// todo : not sure this works for handling @MapKeyEnumerated // todo : not sure this works for handling @MapKeyEnumerated
final Annotation[] annotations = getAnnotations( attributeMember ); return createParameterType( columnNames, columnLengths );
return new ParameterTypeImpl(
classLoaderService()
.classForTypeName( typeParameters.getProperty(DynamicParameterizedType.RETURNED_CLASS) ),
attributeMember != null ? attributeMember.getType() : null,
annotations,
table.getCatalog(),
table.getSchema(),
table.getName(),
parseBoolean( typeParameters.getProperty(DynamicParameterizedType.IS_PRIMARY_KEY) ),
columnNames,
columnLengths
);
} }
catch ( ClassLoadingException e ) { catch ( ClassLoadingException e ) {
throw new MappingException( "Could not create DynamicParameterizedType for type: " + typeName, e ); throw new MappingException( "Could not create DynamicParameterizedType for type: " + typeName, e );
} }
} }
private static final class ParameterTypeImpl implements DynamicParameterizedType.ParameterType { private ParameterType createParameterType(String[] columnNames, Long[] columnLengths) {
final MemberDetails attribute = (MemberDetails) typeParameters.get( DynamicParameterizedType.XPROPERTY );
return new ParameterTypeImpl(
classLoaderService()
.classForTypeName( typeParameters.getProperty( DynamicParameterizedType.RETURNED_CLASS ) ),
attribute != null ? attribute.getType() : null,
getAnnotations( attribute ),
table.getCatalog(),
table.getSchema(),
table.getName(),
parseBoolean( typeParameters.getProperty( DynamicParameterizedType.IS_PRIMARY_KEY ) ),
columnNames,
columnLengths
);
}
private static final class ParameterTypeImpl implements ParameterType {
private final Class<?> returnedClass; private final Class<?> returnedClass;
private final java.lang.reflect.Type returnedJavaType; private final java.lang.reflect.Type returnedJavaType;