Skip to content

Commit 9221349

Browse files
committed
clean up code duplication in RepresentationStrategy stuff
1 parent 1a6fb91 commit 9221349

File tree

3 files changed

+101
-135
lines changed

3 files changed

+101
-135
lines changed

hibernate-core/src/main/java/org/hibernate/metamodel/internal/EmbeddableRepresentationStrategyPojo.java

Lines changed: 3 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,22 @@
1717
import org.hibernate.bytecode.spi.BytecodeProvider;
1818
import org.hibernate.bytecode.spi.ProxyFactoryFactory;
1919
import org.hibernate.bytecode.spi.ReflectionOptimizer;
20-
import org.hibernate.mapping.Backref;
2120
import org.hibernate.mapping.Component;
22-
import org.hibernate.mapping.IndexBackref;
2321
import org.hibernate.mapping.Property;
2422
import org.hibernate.metamodel.RepresentationMode;
2523
import org.hibernate.metamodel.mapping.EmbeddableMappingType;
2624
import org.hibernate.metamodel.spi.EmbeddableInstantiator;
2725
import org.hibernate.metamodel.spi.EmbeddableRepresentationStrategy;
2826
import org.hibernate.metamodel.spi.RuntimeModelCreationContext;
29-
import org.hibernate.property.access.internal.PropertyAccessStrategyBackRefImpl;
30-
import org.hibernate.property.access.internal.PropertyAccessStrategyIndexBackRefImpl;
31-
import org.hibernate.property.access.spi.BuiltInPropertyAccessStrategies;
3227
import org.hibernate.property.access.spi.PropertyAccess;
33-
import org.hibernate.property.access.spi.PropertyAccessStrategy;
3428
import org.hibernate.type.descriptor.java.JavaType;
3529
import org.hibernate.type.descriptor.java.spi.JavaTypeRegistry;
3630
import org.hibernate.type.internal.CompositeUserTypeJavaTypeWrapper;
3731
import org.hibernate.usertype.CompositeUserType;
3832

3933
import static org.hibernate.internal.util.NullnessUtil.castNonNull;
4034
import static org.hibernate.internal.util.ReflectHelper.isAbstractClass;
41-
import static org.hibernate.internal.util.StringHelper.isNotEmpty;
35+
import static org.hibernate.metamodel.internal.PropertyAccessHelper.propertyAccessStrategy;
4236

4337
/**
4438
* @author Steve Ebersole
@@ -180,38 +174,7 @@ private PropertyAccess buildPropertyAccess(
180174
Property bootAttributeDescriptor,
181175
Class<?> embeddableClass,
182176
boolean requireSetters) {
183-
PropertyAccessStrategy strategy = bootAttributeDescriptor.getPropertyAccessStrategy( embeddableClass );
184-
185-
if ( strategy == null ) {
186-
final String propertyAccessorName = bootAttributeDescriptor.getPropertyAccessorName();
187-
if ( isNotEmpty( propertyAccessorName ) ) {
188-
189-
// handle explicitly specified attribute accessor
190-
strategy = strategySelector.resolveStrategy(
191-
PropertyAccessStrategy.class,
192-
propertyAccessorName
193-
);
194-
}
195-
else {
196-
if ( bootAttributeDescriptor instanceof Backref backref ) {
197-
strategy = new PropertyAccessStrategyBackRefImpl(
198-
backref.getCollectionRole(),
199-
backref.getEntityName()
200-
);
201-
}
202-
else if ( bootAttributeDescriptor instanceof IndexBackref indexBackref ) {
203-
strategy = new PropertyAccessStrategyIndexBackRefImpl(
204-
indexBackref.getCollectionRole(),
205-
indexBackref.getEntityName()
206-
);
207-
}
208-
else {
209-
// for now...
210-
strategy = BuiltInPropertyAccessStrategies.MIXED.getStrategy();
211-
}
212-
}
213-
}
214-
177+
final var strategy = propertyAccessStrategy( bootAttributeDescriptor, embeddableClass, strategySelector );
215178
if ( strategy == null ) {
216179
throw new HibernateException(
217180
String.format(
@@ -222,12 +185,7 @@ else if ( bootAttributeDescriptor instanceof IndexBackref indexBackref ) {
222185
)
223186
);
224187
}
225-
226-
return strategy.buildPropertyAccess(
227-
embeddableClass,
228-
bootAttributeDescriptor.getName(),
229-
requireSetters
230-
);
188+
return strategy.buildPropertyAccess( embeddableClass, bootAttributeDescriptor.getName(), requireSetters );
231189
}
232190

233191
private static ReflectionOptimizer buildReflectionOptimizer(

hibernate-core/src/main/java/org/hibernate/metamodel/internal/EntityRepresentationStrategyPojoStandard.java

Lines changed: 45 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import java.lang.reflect.Method;
88
import java.util.LinkedHashMap;
9+
import java.util.LinkedHashSet;
910
import java.util.Locale;
1011
import java.util.Map;
1112
import java.util.Set;
@@ -17,10 +18,7 @@
1718
import org.hibernate.bytecode.spi.ReflectionOptimizer;
1819
import org.hibernate.internal.CoreLogging;
1920
import org.hibernate.internal.CoreMessageLogger;
20-
import org.hibernate.mapping.Backref;
2121
import org.hibernate.mapping.Component;
22-
import org.hibernate.mapping.IndexBackref;
23-
import org.hibernate.mapping.KeyValue;
2422
import org.hibernate.mapping.PersistentClass;
2523
import org.hibernate.mapping.Property;
2624
import org.hibernate.mapping.Subclass;
@@ -29,11 +27,7 @@
2927
import org.hibernate.metamodel.spi.EntityRepresentationStrategy;
3028
import org.hibernate.metamodel.spi.RuntimeModelCreationContext;
3129
import org.hibernate.persister.entity.EntityPersister;
32-
import org.hibernate.property.access.internal.PropertyAccessStrategyBackRefImpl;
33-
import org.hibernate.property.access.internal.PropertyAccessStrategyIndexBackRefImpl;
34-
import org.hibernate.property.access.spi.BuiltInPropertyAccessStrategies;
3530
import org.hibernate.property.access.spi.PropertyAccess;
36-
import org.hibernate.property.access.spi.PropertyAccessStrategy;
3731
import org.hibernate.proxy.HibernateProxy;
3832
import org.hibernate.proxy.ProxyFactory;
3933
import org.hibernate.tuple.entity.EntityMetamodel;
@@ -44,7 +38,7 @@
4438

4539
import static org.hibernate.engine.internal.ManagedTypeHelper.isPersistentAttributeInterceptableType;
4640
import static org.hibernate.internal.util.ReflectHelper.getMethod;
47-
import static org.hibernate.internal.util.StringHelper.isNotEmpty;
41+
import static org.hibernate.metamodel.internal.PropertyAccessHelper.propertyAccessStrategy;
4842
import static org.hibernate.proxy.pojo.ProxyFactoryHelper.validateGetterSetterMethodProxyability;
4943

5044
/**
@@ -88,33 +82,19 @@ public EntityRepresentationStrategyPojoStandard(
8882
identifierPropertyName = null;
8983
identifierPropertyAccess = null;
9084

91-
final KeyValue bootDescriptorIdentifier = bootDescriptor.getIdentifier();
92-
93-
if ( bootDescriptorIdentifier instanceof Component descriptorIdentifierComponent ) {
85+
if ( bootDescriptor.getIdentifier() instanceof Component descriptorIdentifierComponent ) {
9486
final Component identifierMapper = bootDescriptor.getIdentifierMapper();
95-
if ( identifierMapper != null ) {
96-
mapsIdRepresentationStrategy = new EmbeddableRepresentationStrategyPojo(
97-
identifierMapper,
98-
() -> ( ( CompositeTypeImplementor) bootDescriptor.getIdentifierMapper().getType() )
99-
.getMappingModelPart().getEmbeddableTypeDescriptor(),
100-
// we currently do not support custom instantiators for identifiers
101-
null,
102-
null,
103-
creationContext
104-
);
105-
}
106-
else {
107-
mapsIdRepresentationStrategy = new EmbeddableRepresentationStrategyPojo(
108-
descriptorIdentifierComponent,
109-
// TODO: something wrong here: bootDescriptor.getIdentifierMapper() was null!
110-
() -> ( ( CompositeTypeImplementor) bootDescriptor.getIdentifierMapper().getType() )
111-
.getMappingModelPart().getEmbeddableTypeDescriptor(),
112-
// we currently do not support custom instantiators for identifiers
113-
null,
114-
null,
115-
creationContext
116-
);
117-
}
87+
mapsIdRepresentationStrategy = new EmbeddableRepresentationStrategyPojo(
88+
identifierMapper == null ? descriptorIdentifierComponent : identifierMapper,
89+
() -> {
90+
final var type = (CompositeTypeImplementor) bootDescriptor.getIdentifierMapper().getType();
91+
return type.getMappingModelPart().getEmbeddableTypeDescriptor();
92+
},
93+
// we currently do not support custom instantiators for identifiers
94+
null,
95+
null,
96+
creationContext
97+
);
11898
}
11999
else {
120100
mapsIdRepresentationStrategy = null;
@@ -146,36 +126,35 @@ public EntityRepresentationStrategyPojoStandard(
146126
this.instantiator = determineInstantiator( bootDescriptor, runtimeDescriptor.getEntityMetamodel() );
147127
}
148128

149-
@SuppressWarnings("removal")
150129
private ProxyFactory resolveProxyFactory(
151130
PersistentClass bootDescriptor,
152131
EntityPersister entityPersister,
153-
JavaType<?> proxyJtd,
132+
JavaType<?> proxyJavaType,
154133
BytecodeProvider bytecodeProvider,
155134
RuntimeModelCreationContext creationContext) {
156-
final EntityMetamodel entityMetamodel = entityPersister.getEntityMetamodel();
157-
final boolean enhancedForLazyLoading =
158-
entityPersister.getBytecodeEnhancementMetadata()
159-
.isEnhancedForLazyLoading();
160-
161135
// todo : `@ConcreteProxy` handling
162-
if ( enhancedForLazyLoading
136+
if ( entityPersister.getBytecodeEnhancementMetadata().isEnhancedForLazyLoading()
163137
&& bootDescriptor.getRootClass() == bootDescriptor
164138
&& !bootDescriptor.hasSubclasses() ) {
165-
// the entity is bytecode enhanced for lazy loading and is not part of an inheritance hierarchy,
139+
// the entity is bytecode enhanced for lazy loading
140+
// and is not part of an inheritance hierarchy,
166141
// so no need for a ProxyFactory
167142
return null;
168143
}
169-
170-
if ( proxyJtd != null && entityMetamodel.isLazy() ) {
171-
final ProxyFactory proxyFactory = createProxyFactory( bootDescriptor, bytecodeProvider, creationContext );
172-
if ( proxyFactory == null ) {
173-
entityMetamodel.setLazy( false );
144+
else {
145+
final EntityMetamodel entityMetamodel = entityPersister.getEntityMetamodel();
146+
if ( proxyJavaType != null && entityMetamodel.isLazy() ) {
147+
final ProxyFactory proxyFactory =
148+
createProxyFactory( bootDescriptor, bytecodeProvider, creationContext );
149+
if ( proxyFactory == null ) {
150+
entityMetamodel.setLazy( false );
151+
}
152+
return proxyFactory;
153+
}
154+
else {
155+
return null;
174156
}
175-
return proxyFactory;
176157
}
177-
178-
return null;
179158
}
180159

181160
private Map<String, PropertyAccess> buildPropertyAccessMap(PersistentClass bootDescriptor) {
@@ -209,7 +188,7 @@ private ProxyFactory createProxyFactory(
209188
// that the most general @Proxy declared interface at the top of a class
210189
// hierarchy will be used first when a HibernateProxy decides what it
211190
// should implement.
212-
final Set<Class<?>> proxyInterfaces = new java.util.LinkedHashSet<>();
191+
final Set<Class<?>> proxyInterfaces = new LinkedHashSet<>();
213192

214193
final Class<?> mappedClass = mappedJtd.getJavaTypeClass();
215194
final Class<?> proxyInterface = proxyJtd != null ? proxyJtd.getJavaTypeClass() : null;
@@ -306,32 +285,8 @@ private ReflectionOptimizer resolveReflectionOptimizer(BytecodeProvider bytecode
306285
}
307286

308287
private PropertyAccess makePropertyAccess(Property bootAttributeDescriptor) {
309-
PropertyAccessStrategy strategy = bootAttributeDescriptor.getPropertyAccessStrategy( mappedJtd.getJavaTypeClass() );
310-
311-
if ( strategy == null ) {
312-
final String propertyAccessorName = bootAttributeDescriptor.getPropertyAccessorName();
313-
if ( isNotEmpty( propertyAccessorName ) ) {
314-
// handle explicitly specified attribute accessor
315-
strategy = strategySelector.resolveStrategy( PropertyAccessStrategy.class, propertyAccessorName );
316-
}
317-
else {
318-
if ( bootAttributeDescriptor instanceof Backref backref ) {
319-
strategy = new PropertyAccessStrategyBackRefImpl( backref.getCollectionRole(), backref
320-
.getEntityName() );
321-
}
322-
else if ( bootAttributeDescriptor instanceof IndexBackref indexBackref ) {
323-
strategy = new PropertyAccessStrategyIndexBackRefImpl(
324-
indexBackref.getCollectionRole(),
325-
indexBackref.getEntityName()
326-
);
327-
}
328-
else {
329-
// for now...
330-
strategy = BuiltInPropertyAccessStrategies.MIXED.getStrategy();
331-
}
332-
}
333-
}
334-
288+
final Class<?> mappedClass = mappedJtd.getJavaTypeClass();
289+
final var strategy = propertyAccessStrategy( bootAttributeDescriptor, mappedClass, strategySelector );
335290
if ( strategy == null ) {
336291
throw new HibernateException(
337292
String.format(
@@ -342,8 +297,7 @@ else if ( bootAttributeDescriptor instanceof IndexBackref indexBackref ) {
342297
)
343298
);
344299
}
345-
346-
return strategy.buildPropertyAccess( mappedJtd.getJavaTypeClass(), bootAttributeDescriptor.getName(), true );
300+
return strategy.buildPropertyAccess( mappedClass, bootAttributeDescriptor.getName(), true );
347301
}
348302

349303
@Override
@@ -386,16 +340,17 @@ public PropertyAccess resolvePropertyAccess(Property bootAttributeDescriptor) {
386340
if ( bootAttributeDescriptor.getName().equals( identifierPropertyName ) ) {
387341
return identifierPropertyAccess;
388342
}
389-
390-
final PropertyAccess propertyAccess = propertyAccessMap.get( bootAttributeDescriptor.getName() );
391-
if ( propertyAccess != null ) {
392-
return propertyAccess;
393-
}
394-
395-
if ( mapsIdRepresentationStrategy != null ) {
396-
return mapsIdRepresentationStrategy.resolvePropertyAccess( bootAttributeDescriptor );
343+
else {
344+
final PropertyAccess propertyAccess = propertyAccessMap.get( bootAttributeDescriptor.getName() );
345+
if ( propertyAccess != null ) {
346+
return propertyAccess;
347+
}
348+
else if ( mapsIdRepresentationStrategy != null ) {
349+
return mapsIdRepresentationStrategy.resolvePropertyAccess( bootAttributeDescriptor );
350+
}
351+
else {
352+
return null;
353+
}
397354
}
398-
399-
return null;
400355
}
401356
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.metamodel.internal;
6+
7+
import org.hibernate.boot.registry.selector.spi.StrategySelector;
8+
import org.hibernate.mapping.Backref;
9+
import org.hibernate.mapping.IndexBackref;
10+
import org.hibernate.mapping.Property;
11+
import org.hibernate.property.access.internal.PropertyAccessStrategyBackRefImpl;
12+
import org.hibernate.property.access.internal.PropertyAccessStrategyIndexBackRefImpl;
13+
import org.hibernate.property.access.spi.BuiltInPropertyAccessStrategies;
14+
import org.hibernate.property.access.spi.PropertyAccessStrategy;
15+
16+
import static org.hibernate.internal.util.StringHelper.isNotEmpty;
17+
18+
class PropertyAccessHelper {
19+
static PropertyAccessStrategy propertyAccessStrategy(
20+
Property bootAttributeDescriptor,
21+
Class<?> mappedClass,
22+
StrategySelector strategySelector) {
23+
final PropertyAccessStrategy strategy = bootAttributeDescriptor.getPropertyAccessStrategy( mappedClass );
24+
if ( strategy != null ) {
25+
return strategy;
26+
}
27+
else {
28+
final String propertyAccessorName = bootAttributeDescriptor.getPropertyAccessorName();
29+
if ( isNotEmpty( propertyAccessorName ) ) {
30+
// handle explicitly specified attribute accessor
31+
return strategySelector.resolveStrategy( PropertyAccessStrategy.class, propertyAccessorName );
32+
}
33+
else {
34+
if ( bootAttributeDescriptor instanceof Backref backref ) {
35+
return new PropertyAccessStrategyBackRefImpl(
36+
backref.getCollectionRole(),
37+
backref.getEntityName()
38+
);
39+
}
40+
else if ( bootAttributeDescriptor instanceof IndexBackref indexBackref ) {
41+
return new PropertyAccessStrategyIndexBackRefImpl(
42+
indexBackref.getCollectionRole(),
43+
indexBackref.getEntityName()
44+
);
45+
}
46+
else {
47+
// for now...
48+
return BuiltInPropertyAccessStrategies.MIXED.getStrategy();
49+
}
50+
}
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)