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

View File

@ -54,6 +54,7 @@
import org.hibernate.usertype.DynamicParameterizedType;
import jakarta.persistence.AttributeConverter;
import org.hibernate.usertype.DynamicParameterizedType.ParameterType;
import static java.lang.Boolean.parseBoolean;
import static org.hibernate.boot.model.convert.spi.ConverterDescriptor.TYPE_NAME_PREFIX;
@ -914,54 +915,16 @@ public void setJpaAttributeConverterDescriptor(ConverterDescriptor 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 Annotation[] getAnnotations(MemberDetails memberDetails) {
final Collection<? extends Annotation> directAnnotationUsages = memberDetails == null
? null
: memberDetails.getDirectAnnotationUsages();
return directAnnotationUsages == null
? NO_ANNOTATIONS
final Collection<? extends Annotation> directAnnotationUsages =
memberDetails == null ? null
: memberDetails.getDirectAnnotationUsages();
return directAnnotationUsages == null ? NO_ANNOTATIONS
: directAnnotationUsages.toArray( Annotation[]::new );
}
public DynamicParameterizedType.ParameterType makeParameterImpl() {
protected ParameterType createParameterType() {
try {
final String[] columnNames = new String[ 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
final Annotation[] annotations = getAnnotations( attributeMember );
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
);
return createParameterType( columnNames, columnLengths );
}
catch ( ClassLoadingException 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 java.lang.reflect.Type returnedJavaType;