Skip to content

Commit eb56902

Browse files
committed
code cleanups around MappingHelper
1 parent c8b0db9 commit eb56902

File tree

5 files changed

+67
-98
lines changed

5 files changed

+67
-98
lines changed

hibernate-core/src/main/java/org/hibernate/boot/model/TypeDefinition.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,9 @@ private static <T> BasicValue.Resolution<T> createResolution(
145145

146146
// support for AttributeConverter would be nice too
147147
if ( isKnownType ) {
148-
final T typeInstance = instantiateType( bootstrapContext.getServiceRegistry(),
149-
context.getBuildingOptions(), name, typeImplementorClass, instanceProducer );
148+
final T typeInstance =
149+
instantiateType( bootstrapContext.getServiceRegistry(), context.getBuildingOptions(),
150+
name, typeImplementorClass, instanceProducer );
150151

151152
if ( typeInstance instanceof TypeConfigurationAware configurationAware ) {
152153
configurationAware.setTypeConfiguration( typeConfiguration );

hibernate-core/src/main/java/org/hibernate/boot/model/internal/CollectionBinder.java

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,7 @@
110110
import org.hibernate.models.spi.SourceModelBuildingContext;
111111
import org.hibernate.models.spi.TypeDetails;
112112
import org.hibernate.resource.beans.spi.ManagedBean;
113-
import org.hibernate.resource.beans.spi.ManagedBeanRegistry;
114113
import org.hibernate.usertype.CompositeUserType;
115-
import org.hibernate.usertype.ParameterizedType;
116114
import org.hibernate.usertype.UserCollectionType;
117115

118116

@@ -174,8 +172,7 @@
174172
import static org.hibernate.internal.util.StringHelper.qualify;
175173
import static org.hibernate.internal.util.collections.CollectionHelper.isEmpty;
176174
import static org.hibernate.internal.util.collections.CollectionHelper.isNotEmpty;
177-
import static org.hibernate.mapping.MappingHelper.createLocalUserCollectionTypeBean;
178-
import static org.hibernate.mapping.MappingHelper.throwIgnoredCollectionTypeParameters;
175+
import static org.hibernate.mapping.MappingHelper.createCustomTypeBean;
179176

180177
/**
181178
* Base class for stateful binders responsible for producing mapping model objects of type {@link Collection}.
@@ -889,7 +886,7 @@ private static CollectionBinder createBinderFromTypeRegistration(
889886
MetadataBuildingContext buildingContext) {
890887
return createBinder(
891888
property,
892-
() -> createCustomType(
889+
() -> createCustomTypeBean(
893890
property.getDeclaringType().getName() + "#" + property.getName(),
894891
typeRegistration.getImplementation(),
895892
typeRegistration.getParameters(),
@@ -900,37 +897,6 @@ private static CollectionBinder createBinderFromTypeRegistration(
900897
);
901898
}
902899

903-
private static ManagedBean<? extends UserCollectionType> createCustomType(
904-
String role,
905-
Class<? extends UserCollectionType> implementation,
906-
Map<String, ?> parameters,
907-
MetadataBuildingContext buildingContext) {
908-
final boolean hasParameters = isNotEmpty( parameters );
909-
if ( !buildingContext.getBuildingOptions().isAllowExtensionsInCdi() ) {
910-
// if deferred container access is enabled, we locally create the user-type
911-
return createLocalUserCollectionTypeBean( role, implementation, hasParameters, parameters );
912-
}
913-
914-
final ManagedBean<? extends UserCollectionType> managedBean =
915-
buildingContext.getBuildingOptions().getServiceRegistry()
916-
.requireService( ManagedBeanRegistry.class )
917-
.getBean( implementation );
918-
919-
if ( hasParameters ) {
920-
if ( ParameterizedType.class.isAssignableFrom( managedBean.getBeanClass() ) ) {
921-
// create a copy of the parameters and create a bean wrapper to delay injecting
922-
// the parameters, thereby delaying the need to resolve the instance from the
923-
// wrapped bean
924-
final Properties copy = new Properties();
925-
copy.putAll( parameters );
926-
return new DelayedParameterizedTypeBean<>( managedBean, copy );
927-
}
928-
throwIgnoredCollectionTypeParameters( role, implementation );
929-
}
930-
931-
return managedBean;
932-
}
933-
934900
private static CollectionBinder createBinderFromProperty(MemberDetails property, MetadataBuildingContext context) {
935901
final CollectionClassification classification = determineCollectionClassification( property, context );
936902
return createBinder( property, null, classification, context );
@@ -955,7 +921,7 @@ private static ManagedBean<? extends UserCollectionType> resolveCustomType(
955921
MemberDetails property,
956922
CollectionType typeAnnotation,
957923
MetadataBuildingContext context) {
958-
return createCustomType(
924+
return createCustomTypeBean(
959925
property.getDeclaringType().getName() + "." + property.getName(),
960926
typeAnnotation.type(),
961927
PropertiesHelper.map( extractParameters( typeAnnotation ) ),

hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/ModelBinder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1327,7 +1327,7 @@ private void bindCollectionMetadata(MappingDocument mappingDocument, PluralAttri
13271327

13281328
// bind the collection type info
13291329
String typeName = source.getTypeInformation().getName();
1330-
Map<Object,Object> typeParameters = new HashMap<>();
1330+
final Map<String,String> typeParameters = new HashMap<>();
13311331
if ( typeName != null ) {
13321332
// see if there is a corresponding type-def
13331333
final TypeDefinition typeDef = mappingDocument.getMetadataCollector().getTypeDefinition( typeName );

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -760,23 +760,19 @@ public void setTypeName(String typeName) {
760760
this.typeName = typeName;
761761
}
762762

763+
@Deprecated(since = "7.0", forRemoval = true)
763764
public Properties getTypeParameters() {
764765
return typeParameters;
765766
}
766767

768+
@Deprecated(since = "7.0", forRemoval = true)
767769
public void setTypeParameters(Properties parameterMap) {
768770
this.typeParameters = parameterMap;
769771
}
770772

771-
@SuppressWarnings("rawtypes")
772-
public void setTypeParameters(java.util.Map typeParameters) {
773-
if ( typeParameters instanceof Properties properties ) {
774-
this.typeParameters = properties;
775-
}
776-
else {
777-
this.typeParameters = new Properties();
778-
this.typeParameters.putAll( typeParameters );
779-
}
773+
public void setTypeParameters(java.util.Map<String,String> typeParameters) {
774+
this.typeParameters = new Properties();
775+
this.typeParameters.putAll( typeParameters );
780776
}
781777

782778
@Override

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

Lines changed: 55 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
1616
import org.hibernate.boot.spi.MetadataBuildingContext;
1717
import org.hibernate.boot.spi.MetadataImplementor;
18-
import org.hibernate.internal.util.collections.CollectionHelper;
1918
import org.hibernate.metamodel.spi.ImplicitDiscriminatorStrategy;
2019
import org.hibernate.resource.beans.internal.FallbackBeanInstanceProducer;
2120
import org.hibernate.resource.beans.spi.ManagedBean;
@@ -33,7 +32,8 @@
3332
import org.hibernate.usertype.ParameterizedType;
3433
import org.hibernate.usertype.UserCollectionType;
3534

36-
import static org.hibernate.metamodel.mapping.MappingModelCreationLogging.MAPPING_MODEL_CREATION_MESSAGE_LOGGER;
35+
import static org.hibernate.internal.util.PropertiesHelper.map;
36+
import static org.hibernate.internal.util.collections.CollectionHelper.isNotEmpty;
3737

3838
/**
3939
* @author Steve Ebersole
@@ -51,47 +51,59 @@ public static CollectionType customCollection(
5151
String role,
5252
String propertyRef,
5353
MetadataImplementor metadata) {
54-
final Class<? extends UserCollectionType> userCollectionTypeClass =
55-
metadata.getMetadataBuildingOptions().getServiceRegistry()
56-
.requireService( ClassLoaderService.class )
57-
.classForName( typeName );
58-
59-
final boolean hasParameters = CollectionHelper.isNotEmpty( typeParameters );
60-
final ManagedBean<? extends UserCollectionType> userTypeBean;
61-
62-
if ( !metadata.getMetadataBuildingOptions().isAllowExtensionsInCdi() ) {
63-
//noinspection unchecked,rawtypes
64-
userTypeBean = createLocalUserCollectionTypeBean(
65-
role,
66-
userCollectionTypeClass,
67-
hasParameters,
68-
(Map) typeParameters
69-
);
70-
}
71-
else {
72-
final ManagedBean<? extends UserCollectionType> userCollectionTypeBean =
73-
getManagedBeanRegistry( metadata ).getBean( userCollectionTypeClass );
74-
75-
if ( hasParameters ) {
76-
if ( ParameterizedType.class.isAssignableFrom( userCollectionTypeBean.getBeanClass() ) ) {
77-
// create a copy of the parameters and create a bean wrapper to delay injecting
78-
// the parameters, thereby delaying the need to resolve the instance from the
79-
// wrapped bean
80-
final Properties copy = new Properties();
81-
copy.putAll( typeParameters );
82-
userTypeBean = new DelayedParameterizedTypeBean<>( userCollectionTypeBean, copy );
83-
}
84-
else {
85-
throwIgnoredCollectionTypeParameters( role, userCollectionTypeClass );
86-
userTypeBean = userCollectionTypeBean;
87-
}
54+
final ManagedBean<? extends UserCollectionType> userTypeBean =
55+
createUserTypeBean( role, classForName( typeName, metadata ), map( typeParameters ), metadata );
56+
return new CustomCollectionType( userTypeBean, role, propertyRef );
57+
}
58+
59+
private static ManagedBean<? extends UserCollectionType> createUserTypeBean(
60+
String role,
61+
Class<? extends UserCollectionType> userCollectionTypeClass,
62+
Map<String, ?> parameters,
63+
MetadataImplementor metadata) {
64+
return metadata.getMetadataBuildingOptions().isAllowExtensionsInCdi()
65+
? createSharedUserTypeBean( role, userCollectionTypeClass, parameters, metadata )
66+
: createLocalUserTypeBean( role, userCollectionTypeClass, parameters );
67+
}
68+
69+
public static ManagedBean<? extends UserCollectionType> createCustomTypeBean(
70+
String role,
71+
Class<? extends UserCollectionType> implementation,
72+
Map<String, ?> parameters,
73+
MetadataBuildingContext context) {
74+
// if deferred container access is enabled, we locally create the user-type
75+
return context.getBuildingOptions().isAllowExtensionsInCdi()
76+
? createSharedUserTypeBean( role, implementation, parameters, context.getMetadataCollector() )
77+
: createLocalUserTypeBean( role, implementation, parameters );
78+
}
79+
80+
private static ManagedBean<? extends UserCollectionType> createSharedUserTypeBean(
81+
String role,
82+
Class<? extends UserCollectionType> userCollectionTypeClass,
83+
Map<String, ?> parameters,
84+
MetadataImplementor metadata) {
85+
final ManagedBean<? extends UserCollectionType> managedBean =
86+
getManagedBeanRegistry( metadata ).getBean( userCollectionTypeClass );
87+
if ( isNotEmpty( parameters ) ) {
88+
if ( ParameterizedType.class.isAssignableFrom( managedBean.getBeanClass() ) ) {
89+
// create a copy of the parameters and create a bean wrapper to delay injecting
90+
// the parameters, thereby delaying the need to resolve the instance from the
91+
// wrapped bean
92+
final Properties copy = new Properties();
93+
copy.putAll( parameters );
94+
return new DelayedParameterizedTypeBean<>( managedBean, copy );
8895
}
8996
else {
90-
userTypeBean = userCollectionTypeBean;
97+
throwIgnoredCollectionTypeParameters( role, userCollectionTypeClass );
9198
}
9299
}
100+
return managedBean;
101+
}
93102

94-
return new CustomCollectionType( userTypeBean, role, propertyRef );
103+
private static Class<UserCollectionType> classForName(String typeName, MetadataImplementor metadata) {
104+
return metadata.getMetadataBuildingOptions().getServiceRegistry()
105+
.requireService( ClassLoaderService.class )
106+
.classForName( typeName );
95107
}
96108

97109
private static ManagedBeanRegistry getManagedBeanRegistry(MetadataImplementor metadata) {
@@ -109,11 +121,8 @@ public static void injectParameters(Object type, Properties parameters) {
109121
parameterizedType.setParameterValues( parameters == null ? EMPTY_PROPERTIES : parameters );
110122
}
111123
else if ( parameters != null && !parameters.isEmpty() ) {
112-
// TODO: should this throw?
113-
MAPPING_MODEL_CREATION_MESSAGE_LOGGER.debugf(
114-
"UserCollectionType impl does not implement ParameterizedType but parameters were present : `%s`",
115-
type.getClass().getName()
116-
);
124+
throw new MappingException( "'UserType' implementation '" + type.getClass().getName()
125+
+ "' does not implement 'ParameterizedType' but parameters were provided" );
117126
}
118127
}
119128

@@ -201,16 +210,14 @@ public static OneToOneType oneToOne(
201210
);
202211
}
203212

204-
public static ManagedBean<UserCollectionType> createLocalUserCollectionTypeBean(
213+
private static ManagedBean<UserCollectionType> createLocalUserTypeBean(
205214
String role,
206215
Class<? extends UserCollectionType> implementation,
207-
boolean hasParameters,
208216
Map<String, ?> parameters) {
209217
final UserCollectionType userCollectionType =
210218
FallbackBeanInstanceProducer.INSTANCE.produceBeanInstance( implementation );
211-
212-
if ( hasParameters ) {
213-
// `CollectionType declared parameters - inject them
219+
if ( isNotEmpty( parameters ) ) {
220+
// CollectionType declared parameters - inject them
214221
if ( userCollectionType instanceof ParameterizedType parameterizedType ) {
215222
final Properties properties = new Properties();
216223
properties.putAll( parameters );
@@ -220,7 +227,6 @@ public static ManagedBean<UserCollectionType> createLocalUserCollectionTypeBean(
220227
throwIgnoredCollectionTypeParameters( role, implementation );
221228
}
222229
}
223-
224230
return new ProvidedInstanceManagedBeanImpl<>( userCollectionType );
225231
}
226232

0 commit comments

Comments
 (0)