Skip to content

Commit 56a780b

Browse files
committed
refactor duplicated code in SimpleValue/BasicValue
1 parent a283fc9 commit 56a780b

File tree

2 files changed

+31
-66
lines changed

2 files changed

+31
-66
lines changed

hibernate-core/src/main/java/org/hibernate/mapping/BasicValue.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ protected Resolution<?> buildResolution() {
420420
if ( typeParameters != null
421421
&& parseBoolean( typeParameters.getProperty(DynamicParameterizedType.IS_DYNAMIC) )
422422
&& typeParameters.get(DynamicParameterizedType.PARAMETER_TYPE) == null ) {
423-
createParameterImpl();
423+
getTypeParameters().put( DynamicParameterizedType.PARAMETER_TYPE, createParameterType() );
424424
}
425425
return buildResolution( typeParameters );
426426
}
@@ -1046,9 +1046,9 @@ private Properties getCustomTypeProperties() {
10461046

10471047
private UserType<?> getConfiguredUserTypeBean(Class<? extends UserType<?>> explicitCustomType, Properties properties) {
10481048
final UserType<?> typeInstance =
1049-
!getBuildingContext().getBuildingOptions().isAllowExtensionsInCdi()
1050-
? FallbackBeanInstanceProducer.INSTANCE.produceBeanInstance( explicitCustomType )
1051-
: getUserTypeBean( explicitCustomType, properties ).getBeanInstance();
1049+
getBuildingContext().getBuildingOptions().isAllowExtensionsInCdi()
1050+
? getUserTypeBean( explicitCustomType, properties ).getBeanInstance()
1051+
: FallbackBeanInstanceProducer.INSTANCE.produceBeanInstance( explicitCustomType );
10521052

10531053
if ( typeInstance instanceof TypeConfigurationAware configurationAware ) {
10541054
configurationAware.setTypeConfiguration( getTypeConfiguration() );
@@ -1057,7 +1057,7 @@ private UserType<?> getConfiguredUserTypeBean(Class<? extends UserType<?>> expli
10571057
if ( typeInstance instanceof DynamicParameterizedType ) {
10581058
if ( parseBoolean( properties.getProperty( DynamicParameterizedType.IS_DYNAMIC ) ) ) {
10591059
if ( properties.get( DynamicParameterizedType.PARAMETER_TYPE ) == null ) {
1060-
properties.put( DynamicParameterizedType.PARAMETER_TYPE, makeParameterImpl() );
1060+
properties.put( DynamicParameterizedType.PARAMETER_TYPE, createParameterType() );
10611061
}
10621062
}
10631063
}
@@ -1071,13 +1071,12 @@ private UserType<?> getConfiguredUserTypeBean(Class<? extends UserType<?>> expli
10711071

10721072
private <T> ManagedBean<T> getUserTypeBean(Class<T> explicitCustomType, Properties properties) {
10731073
final BeanInstanceProducer producer = getBuildingContext().getBootstrapContext().getCustomTypeProducer();
1074-
final ManagedBeanRegistry registry = getServiceRegistry().requireService( ManagedBeanRegistry.class );
10751074
if ( isNotEmpty( properties ) ) {
10761075
final String name = explicitCustomType.getName() + COUNTER++;
1077-
return registry.getBean( name, explicitCustomType, producer );
1076+
return getManagedBeanRegistry().getBean( name, explicitCustomType, producer );
10781077
}
10791078
else {
1080-
return registry.getBean( explicitCustomType, producer );
1079+
return getManagedBeanRegistry().getBean( explicitCustomType, producer );
10811080
}
10821081
}
10831082

hibernate-core/src/main/java/org/hibernate/mapping/SimpleValue.java

Lines changed: 24 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import org.hibernate.usertype.DynamicParameterizedType;
5555

5656
import jakarta.persistence.AttributeConverter;
57+
import org.hibernate.usertype.DynamicParameterizedType.ParameterType;
5758

5859
import static java.lang.Boolean.parseBoolean;
5960
import static org.hibernate.boot.model.convert.spi.ConverterDescriptor.TYPE_NAME_PREFIX;
@@ -914,54 +915,16 @@ public void setJpaAttributeConverterDescriptor(ConverterDescriptor descriptor) {
914915
this.attributeConverterDescriptor = descriptor;
915916
}
916917

917-
protected void createParameterImpl() {
918-
try {
919-
final String[] columnNames = new String[ columns.size() ];
920-
final Long[] columnLengths = new Long[ columns.size() ];
921-
922-
for ( int i = 0; i < columns.size(); i++ ) {
923-
final Selectable selectable = columns.get(i);
924-
if ( selectable instanceof Column column ) {
925-
columnNames[i] = column.getName();
926-
columnLengths[i] = column.getLength();
927-
}
928-
}
929-
930-
final MemberDetails attributeMember = (MemberDetails) typeParameters.get( DynamicParameterizedType.XPROPERTY );
931-
// todo : not sure this works for handling @MapKeyEnumerated
932-
final Annotation[] annotations = getAnnotations( attributeMember );
933-
typeParameters.put(
934-
DynamicParameterizedType.PARAMETER_TYPE,
935-
new ParameterTypeImpl(
936-
classLoaderService()
937-
.classForTypeName( typeParameters.getProperty(DynamicParameterizedType.RETURNED_CLASS) ),
938-
attributeMember != null ? attributeMember.getType() : null,
939-
annotations,
940-
table.getCatalog(),
941-
table.getSchema(),
942-
table.getName(),
943-
parseBoolean( typeParameters.getProperty(DynamicParameterizedType.IS_PRIMARY_KEY) ),
944-
columnNames,
945-
columnLengths
946-
)
947-
);
948-
}
949-
catch ( ClassLoadingException e ) {
950-
throw new MappingException( "Could not create DynamicParameterizedType for type: " + typeName, e );
951-
}
952-
}
953-
954918
private static final Annotation[] NO_ANNOTATIONS = new Annotation[0];
955919
private static Annotation[] getAnnotations(MemberDetails memberDetails) {
956-
final Collection<? extends Annotation> directAnnotationUsages = memberDetails == null
957-
? null
958-
: memberDetails.getDirectAnnotationUsages();
959-
return directAnnotationUsages == null
960-
? NO_ANNOTATIONS
920+
final Collection<? extends Annotation> directAnnotationUsages =
921+
memberDetails == null ? null
922+
: memberDetails.getDirectAnnotationUsages();
923+
return directAnnotationUsages == null ? NO_ANNOTATIONS
961924
: directAnnotationUsages.toArray( Annotation[]::new );
962925
}
963926

964-
public DynamicParameterizedType.ParameterType makeParameterImpl() {
927+
protected ParameterType createParameterType() {
965928
try {
966929
final String[] columnNames = new String[ columns.size() ];
967930
final Long[] columnLengths = new Long[ columns.size() ];
@@ -974,28 +937,31 @@ public DynamicParameterizedType.ParameterType makeParameterImpl() {
974937
}
975938
}
976939

977-
final MemberDetails attributeMember = (MemberDetails) typeParameters.get( DynamicParameterizedType.XPROPERTY );
978940
// todo : not sure this works for handling @MapKeyEnumerated
979-
final Annotation[] annotations = getAnnotations( attributeMember );
980-
return new ParameterTypeImpl(
981-
classLoaderService()
982-
.classForTypeName( typeParameters.getProperty(DynamicParameterizedType.RETURNED_CLASS) ),
983-
attributeMember != null ? attributeMember.getType() : null,
984-
annotations,
985-
table.getCatalog(),
986-
table.getSchema(),
987-
table.getName(),
988-
parseBoolean( typeParameters.getProperty(DynamicParameterizedType.IS_PRIMARY_KEY) ),
989-
columnNames,
990-
columnLengths
991-
);
941+
return createParameterType( columnNames, columnLengths );
992942
}
993943
catch ( ClassLoadingException e ) {
994944
throw new MappingException( "Could not create DynamicParameterizedType for type: " + typeName, e );
995945
}
996946
}
997947

998-
private static final class ParameterTypeImpl implements DynamicParameterizedType.ParameterType {
948+
private ParameterType createParameterType(String[] columnNames, Long[] columnLengths) {
949+
final MemberDetails attribute = (MemberDetails) typeParameters.get( DynamicParameterizedType.XPROPERTY );
950+
return new ParameterTypeImpl(
951+
classLoaderService()
952+
.classForTypeName( typeParameters.getProperty( DynamicParameterizedType.RETURNED_CLASS ) ),
953+
attribute != null ? attribute.getType() : null,
954+
getAnnotations( attribute ),
955+
table.getCatalog(),
956+
table.getSchema(),
957+
table.getName(),
958+
parseBoolean( typeParameters.getProperty( DynamicParameterizedType.IS_PRIMARY_KEY ) ),
959+
columnNames,
960+
columnLengths
961+
);
962+
}
963+
964+
private static final class ParameterTypeImpl implements ParameterType {
999965

1000966
private final Class<?> returnedClass;
1001967
private final java.lang.reflect.Type returnedJavaType;

0 commit comments

Comments
 (0)