Skip to content

Commit 7feea8b

Browse files
committed
minor cleanups in proxy and property packages
- deprecate obsolete class ProxyFactoryHelper - squash some warnings - lots of 'var' - etc
1 parent 96aa283 commit 7feea8b

25 files changed

+237
-219
lines changed

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@
3535
import org.hibernate.type.descriptor.java.JavaType;
3636
import org.hibernate.type.spi.CompositeTypeImplementor;
3737

38+
import static java.lang.reflect.Modifier.isFinal;
3839
import static org.hibernate.engine.internal.ManagedTypeHelper.isPersistentAttributeInterceptableType;
3940
import static org.hibernate.internal.util.ReflectHelper.getMethod;
4041
import static org.hibernate.metamodel.internal.PropertyAccessHelper.propertyAccessStrategy;
41-
import static org.hibernate.proxy.pojo.ProxyFactoryHelper.validateGetterSetterMethodProxyability;
4242

4343
/**
4444
* @author Steve Ebersole
@@ -375,4 +375,17 @@ else if ( mapsIdRepresentationStrategy != null ) {
375375
}
376376
}
377377
}
378+
379+
private static void validateGetterSetterMethodProxyability(String getterOrSetter, Method method ) {
380+
if ( method != null && isFinal( method.getModifiers() ) ) {
381+
throw new HibernateException(
382+
String.format(
383+
"%s methods of lazy classes cannot be final: %s#%s",
384+
getterOrSetter,
385+
method.getDeclaringClass().getName(),
386+
method.getName()
387+
)
388+
);
389+
}
390+
}
378391
}

hibernate-core/src/main/java/org/hibernate/property/access/internal/AbstractFieldSerialForm.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ protected AbstractFieldSerialForm(Class<?> declaringClass, String fieldName) {
3030

3131
protected Field resolveField() {
3232
try {
33-
final Field field = declaringClass.getDeclaredField( fieldName );
33+
final var field = declaringClass.getDeclaredField( fieldName );
3434
ReflectHelper.ensureAccessibility( field );
3535
return field;
3636
}

hibernate-core/src/main/java/org/hibernate/property/access/internal/AbstractSetterMethodSerialForm.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public Class<?> getArgumentType() {
5353

5454
protected Method resolveMethod() {
5555
try {
56-
final Method method = declaringClass.getDeclaredMethod( methodName, argumentType );
56+
final var method = declaringClass.getDeclaredMethod( methodName, argumentType );
5757
ReflectHelper.ensureAccessibility( method );
5858
return method;
5959
}

hibernate-core/src/main/java/org/hibernate/property/access/internal/AccessStrategyHelper.java

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
import java.lang.reflect.AnnotatedElement;
88
import java.lang.reflect.Field;
9-
import java.lang.reflect.Method;
10-
import java.lang.reflect.Modifier;
119

1210
import org.hibernate.PropertyNotFoundException;
1311
import org.hibernate.bytecode.enhance.spi.interceptor.BytecodeLazyAttributeInterceptor;
@@ -20,6 +18,7 @@
2018
import jakarta.persistence.Transient;
2119
import org.checkerframework.checker.nullness.qual.Nullable;
2220

21+
import static java.lang.reflect.Modifier.isStatic;
2322
import static org.hibernate.engine.internal.ManagedTypeHelper.asCompositeOwner;
2423
import static org.hibernate.engine.internal.ManagedTypeHelper.asCompositeTracker;
2524
import static org.hibernate.engine.internal.ManagedTypeHelper.asPersistentAttributeInterceptable;
@@ -48,14 +47,14 @@ public class AccessStrategyHelper {
4847
}
4948

5049
public static AccessType getAccessType(Class<?> containerJavaType, String propertyName) {
51-
final Field field = fieldOrNull( containerJavaType, propertyName );
52-
final AccessType explicitAccessType = getExplicitAccessType( containerJavaType, propertyName, field );
50+
final var field = fieldOrNull( containerJavaType, propertyName );
51+
final var explicitAccessType = getExplicitAccessType( containerJavaType, propertyName, field );
5352
if ( explicitAccessType != null ) {
5453
return explicitAccessType;
5554
}
5655

5756
// No @Access on property or field; check to see if containerJavaType has an explicit @Access
58-
AccessType classAccessType = getAccessTypeOrNull( containerJavaType );
57+
final var classAccessType = getAccessTypeOrNull( containerJavaType );
5958
if ( classAccessType != null ) {
6059
return classAccessType;
6160
}
@@ -78,38 +77,37 @@ public static AccessType getAccessType(Class<?> containerJavaType, String proper
7877
if ( field != null
7978
&& field.isAnnotationPresent( Access.class )
8079
&& !field.isAnnotationPresent( Transient.class )
81-
&& !Modifier.isStatic( field.getModifiers() ) ) {
80+
&& !isStatic( field.getModifiers() ) ) {
8281
return AccessType.FIELD;
8382
}
8483

85-
final Method getter = getterMethodOrNull( containerClass, propertyName );
86-
if ( getter != null && getter.isAnnotationPresent( Access.class ) ) {
87-
return AccessType.PROPERTY;
88-
}
89-
90-
return null;
84+
final var getter = getterMethodOrNull( containerClass, propertyName );
85+
return getter != null && getter.isAnnotationPresent( Access.class )
86+
? AccessType.PROPERTY
87+
: null;
9188
}
9289

9390
protected static @Nullable AccessType getAccessTypeOrNull(@Nullable AnnotatedElement element) {
9491
if ( element == null ) {
9592
return null;
9693
}
97-
Access elementAccess = element.getAnnotation( Access.class );
98-
return elementAccess == null ? null : elementAccess.value();
94+
else {
95+
final var elementAccess = element.getAnnotation( Access.class );
96+
return elementAccess == null ? null : elementAccess.value();
97+
}
9998
}
10099

101100
public static int determineEnhancementState(Class<?> containerClass, Class<?> attributeType) {
102-
return ( CompositeOwner.class.isAssignableFrom( containerClass ) ? AccessStrategyHelper.COMPOSITE_OWNER : 0 )
103-
| ( CompositeTracker.class.isAssignableFrom( attributeType ) ? AccessStrategyHelper.COMPOSITE_TRACKER_MASK : 0 )
104-
| ( isPersistentAttributeInterceptableType( containerClass ) ? AccessStrategyHelper.PERSISTENT_ATTRIBUTE_INTERCEPTABLE_MASK : 0 );
101+
return ( CompositeOwner.class.isAssignableFrom( containerClass ) ? COMPOSITE_OWNER : 0 )
102+
| ( CompositeTracker.class.isAssignableFrom( attributeType ) ? COMPOSITE_TRACKER_MASK : 0 )
103+
| ( isPersistentAttributeInterceptableType( containerClass ) ? PERSISTENT_ATTRIBUTE_INTERCEPTABLE_MASK : 0 );
105104
}
106105

107106
public static void handleEnhancedInjection(Object target, @Nullable Object value, int enhancementState, String propertyName) {
108107
// This sets the component relation for dirty tracking purposes
109108
if ( ( enhancementState & COMPOSITE_OWNER ) != 0
110-
&& ( ( enhancementState & COMPOSITE_TRACKER_MASK ) != 0
111-
&& value != null
112-
|| isCompositeTracker( value ) ) ) {
109+
&& ( ( enhancementState & COMPOSITE_TRACKER_MASK ) != 0 && value != null
110+
|| isCompositeTracker( value ) ) ) {
113111
asCompositeTracker( NullnessUtil.castNonNull(value) ).$$_hibernate_setOwner( propertyName, asCompositeOwner( target ) );
114112
}
115113

hibernate-core/src/main/java/org/hibernate/property/access/internal/PropertyAccessBasicImpl.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
*/
55
package org.hibernate.property.access.internal;
66

7-
import java.lang.reflect.Method;
8-
9-
import org.hibernate.internal.util.ReflectHelper;
107
import org.hibernate.property.access.spi.Getter;
118
import org.hibernate.property.access.spi.GetterMethodImpl;
129
import org.hibernate.property.access.spi.PropertyAccess;
@@ -17,6 +14,10 @@
1714

1815
import org.checkerframework.checker.nullness.qual.Nullable;
1916

17+
import static org.hibernate.internal.util.ReflectHelper.findGetterMethod;
18+
import static org.hibernate.internal.util.ReflectHelper.findSetterMethod;
19+
import static org.hibernate.internal.util.ReflectHelper.setterMethodOrNull;
20+
2021
/**
2122
* {@link PropertyAccess} for accessing the wrapped property via get/set pair, which may be nonpublic.
2223
*
@@ -37,12 +38,12 @@ public PropertyAccessBasicImpl(
3738
boolean setterRequired) {
3839
this.strategy = strategy;
3940

40-
final Method getterMethod = ReflectHelper.findGetterMethod( containerJavaType, propertyName );
41+
final var getterMethod = findGetterMethod( containerJavaType, propertyName );
4142
getter = new GetterMethodImpl( containerJavaType, propertyName, getterMethod );
4243

43-
final Method setterMethod = setterRequired
44-
? ReflectHelper.findSetterMethod( containerJavaType, propertyName, getterMethod.getReturnType() )
45-
: ReflectHelper.setterMethodOrNull( containerJavaType, propertyName, getterMethod.getReturnType() );
44+
final var setterMethod = setterRequired
45+
? findSetterMethod( containerJavaType, propertyName, getterMethod.getReturnType() )
46+
: setterMethodOrNull( containerJavaType, propertyName, getterMethod.getReturnType() );
4647
setter = setterMethod != null
4748
? new SetterMethodImpl( containerJavaType, propertyName, setterMethod )
4849
: null;

hibernate-core/src/main/java/org/hibernate/property/access/internal/PropertyAccessEnhancedImpl.java

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717
import org.hibernate.property.access.spi.PropertyAccessStrategy;
1818
import org.hibernate.property.access.spi.Setter;
1919

20-
import java.lang.reflect.Field;
2120
import java.lang.reflect.Method;
2221

2322
import static org.hibernate.internal.util.ReflectHelper.findField;
2423
import static org.hibernate.internal.util.ReflectHelper.findSetterMethod;
2524
import static org.hibernate.internal.util.ReflectHelper.getterMethodOrNull;
2625
import static org.hibernate.property.access.internal.AccessStrategyHelper.fieldOrNull;
26+
import static org.hibernate.property.access.internal.AccessStrategyHelper.getAccessType;
2727

2828
/**
2929
* A {@link PropertyAccess} for byte code enhanced entities. Enhanced setter methods ( if available ) are used for
@@ -45,14 +45,14 @@ public PropertyAccessEnhancedImpl(
4545
@Nullable AccessType classAccessType) {
4646
this.strategy = strategy;
4747

48-
final AccessType propertyAccessType =
48+
final var propertyAccessType =
4949
classAccessType == null
50-
? AccessStrategyHelper.getAccessType( containerJavaType, propertyName )
50+
? getAccessType( containerJavaType, propertyName )
5151
: classAccessType;
5252

5353
switch ( propertyAccessType ) {
5454
case FIELD: {
55-
final Field field = fieldOrNull( containerJavaType, propertyName );
55+
final var field = fieldOrNull( containerJavaType, propertyName );
5656
if ( field == null ) {
5757
throw new PropertyAccessBuildingException(
5858
"Could not locate field for property named [" + containerJavaType.getName() + "#" + propertyName + "]"
@@ -63,7 +63,7 @@ public PropertyAccessEnhancedImpl(
6363
break;
6464
}
6565
case PROPERTY: {
66-
final Method getterMethod = getterMethodOrNull( containerJavaType, propertyName );
66+
final var getterMethod = getterMethodOrNull( containerJavaType, propertyName );
6767
if ( getterMethod == null ) {
6868
throw new PropertyAccessBuildingException(
6969
"Could not locate getter for property named [" + containerJavaType.getName() + "#" + propertyName + "]"
@@ -83,29 +83,31 @@ public PropertyAccessEnhancedImpl(
8383

8484
private static Getter propertyGetter(@Nullable AccessType classAccessType, Class<?> containerJavaType, String propertyName, Method getterMethod) {
8585
if ( classAccessType != null ) {
86-
final AccessType explicitAccessType = AccessStrategyHelper.getAccessType( containerJavaType, propertyName );
86+
final var explicitAccessType = getAccessType( containerJavaType, propertyName );
8787
if ( explicitAccessType == AccessType.FIELD ) {
88-
// We need to default to FIELD unless we have an explicit AccessType to avoid unnecessary initializations
89-
final Field field = findField( containerJavaType, propertyName );
90-
return new EnhancedGetterFieldImpl( containerJavaType, propertyName, field, getterMethod );
88+
// We need to default to FIELD unless we have an explicit AccessType
89+
// to avoid unnecessary initializations
90+
return new EnhancedGetterFieldImpl( containerJavaType, propertyName,
91+
findField( containerJavaType, propertyName ), getterMethod );
9192
}
9293
}
93-
// when classAccessType is null know PROPERTY is the explicit access type
94+
// when classAccessType is null, know PROPERTY is the explicit access type
9495
return new GetterMethodImpl( containerJavaType, propertyName, getterMethod );
9596
}
9697

9798
private static Setter propertySetter(@Nullable AccessType classAccessType, Class<?> containerJavaType, String propertyName, Class<?> fieldType) {
9899
if ( classAccessType != null ) {
99-
final AccessType explicitAccessType = AccessStrategyHelper.getAccessType( containerJavaType, propertyName );
100+
final var explicitAccessType = getAccessType( containerJavaType, propertyName );
100101
if ( explicitAccessType == AccessType.FIELD ) {
101-
// We need to default to FIELD unless we have an explicit AccessType to avoid unnecessary initializations
102-
final Field field = findField( containerJavaType, propertyName );
103-
return new EnhancedSetterImpl( containerJavaType, propertyName, field );
102+
// We need to default to FIELD unless we have an explicit AccessType
103+
// to avoid unnecessary initializations
104+
return new EnhancedSetterImpl( containerJavaType, propertyName,
105+
findField( containerJavaType, propertyName ) );
104106
}
105107
}
106-
// when classAccessType is null know PROPERTY is the explicit access type
107-
final Method setterMethod = findSetterMethod( containerJavaType, propertyName, fieldType );
108-
return new EnhancedSetterMethodImpl( containerJavaType, propertyName, setterMethod );
108+
// when classAccessType is null, know PROPERTY is the explicit access type
109+
return new EnhancedSetterMethodImpl( containerJavaType, propertyName,
110+
findSetterMethod( containerJavaType, propertyName, fieldType ) );
109111
}
110112

111113
@Override

hibernate-core/src/main/java/org/hibernate/property/access/internal/PropertyAccessFieldImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44
*/
55
package org.hibernate.property.access.internal;
66

7-
import java.lang.reflect.Field;
87

9-
import org.hibernate.internal.util.ReflectHelper;
108
import org.hibernate.property.access.spi.Getter;
119
import org.hibernate.property.access.spi.GetterFieldImpl;
1210
import org.hibernate.property.access.spi.PropertyAccess;
1311
import org.hibernate.property.access.spi.PropertyAccessStrategy;
1412
import org.hibernate.property.access.spi.Setter;
1513
import org.hibernate.property.access.spi.SetterFieldImpl;
1614

15+
import static org.hibernate.internal.util.ReflectHelper.findField;
16+
1717
/**
1818
* @author Steve Ebersole
1919
* @author Gavin King
@@ -29,7 +29,7 @@ public PropertyAccessFieldImpl(
2929
final String propertyName) {
3030
this.strategy = strategy;
3131

32-
final Field field = ReflectHelper.findField( containerJavaType, propertyName );
32+
final var field = findField( containerJavaType, propertyName );
3333
getter = new GetterFieldImpl( containerJavaType, propertyName, field );
3434
setter = new SetterFieldImpl( containerJavaType, propertyName, field );
3535
}

hibernate-core/src/main/java/org/hibernate/property/access/internal/PropertyAccessGetterImpl.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
*/
55
package org.hibernate.property.access.internal;
66

7-
import jakarta.persistence.AccessType;
87
import org.checkerframework.checker.nullness.qual.Nullable;
98
import org.hibernate.property.access.spi.Getter;
109
import org.hibernate.property.access.spi.GetterFieldImpl;
@@ -18,6 +17,8 @@
1817
import java.lang.reflect.Method;
1918

2019
import static org.hibernate.internal.util.ReflectHelper.getterMethodOrNull;
20+
import static org.hibernate.property.access.internal.AccessStrategyHelper.fieldOrNull;
21+
import static org.hibernate.property.access.internal.AccessStrategyHelper.getAccessType;
2122

2223
/**
2324
* A {@link PropertyAccess} based on mix of getter method or field.
@@ -32,10 +33,10 @@ public class PropertyAccessGetterImpl implements PropertyAccess {
3233
public PropertyAccessGetterImpl(PropertyAccessStrategy strategy, Class<?> containerJavaType, String propertyName) {
3334
this.strategy = strategy;
3435

35-
final AccessType propertyAccessType = AccessStrategyHelper.getAccessType( containerJavaType, propertyName );
36+
final var propertyAccessType = getAccessType( containerJavaType, propertyName );
3637
switch ( propertyAccessType ) {
3738
case FIELD: {
38-
final Field field = AccessStrategyHelper.fieldOrNull( containerJavaType, propertyName );
39+
final var field = fieldOrNull( containerJavaType, propertyName );
3940
if ( field == null ) {
4041
throw new PropertyAccessBuildingException(
4142
"Could not locate field for property named [" + containerJavaType.getName() + "#" + propertyName + "]"
@@ -45,7 +46,7 @@ public PropertyAccessGetterImpl(PropertyAccessStrategy strategy, Class<?> contai
4546
break;
4647
}
4748
case PROPERTY: {
48-
final Method getterMethod = getterMethodOrNull( containerJavaType, propertyName );
49+
final var getterMethod = getterMethodOrNull( containerJavaType, propertyName );
4950
if ( getterMethod == null ) {
5051
throw new PropertyAccessBuildingException(
5152
"Could not locate getter for property named [" + containerJavaType.getName() + "#" + propertyName + "]"

hibernate-core/src/main/java/org/hibernate/property/access/internal/PropertyAccessMixedImpl.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@
1717
import org.hibernate.property.access.spi.SetterFieldImpl;
1818
import org.hibernate.property.access.spi.SetterMethodImpl;
1919

20-
import jakarta.persistence.AccessType;
2120
import org.checkerframework.checker.nullness.qual.PolyNull;
2221

2322
import static org.hibernate.internal.util.ReflectHelper.findSetterMethod;
2423
import static org.hibernate.internal.util.ReflectHelper.getterMethodOrNull;
24+
import static org.hibernate.property.access.internal.AccessStrategyHelper.fieldOrNull;
25+
import static org.hibernate.property.access.internal.AccessStrategyHelper.getAccessType;
2526

2627
/**
2728
* A {@link PropertyAccess} based on mix of getter/setter method and/or field.
@@ -37,10 +38,10 @@ public class PropertyAccessMixedImpl implements PropertyAccess {
3738
public PropertyAccessMixedImpl(PropertyAccessStrategy strategy, Class<?> containerJavaType, String propertyName) {
3839
this.strategy = strategy;
3940

40-
final AccessType propertyAccessType = AccessStrategyHelper.getAccessType( containerJavaType, propertyName );
41+
final var propertyAccessType = getAccessType( containerJavaType, propertyName );
4142
switch ( propertyAccessType ) {
4243
case FIELD: {
43-
final Field field = AccessStrategyHelper.fieldOrNull( containerJavaType, propertyName );
44+
final var field = fieldOrNull( containerJavaType, propertyName );
4445
if ( field == null ) {
4546
throw new PropertyAccessBuildingException(
4647
"Could not locate field for property named [" + containerJavaType.getName() + "#" + propertyName + "]"
@@ -51,13 +52,13 @@ public PropertyAccessMixedImpl(PropertyAccessStrategy strategy, Class<?> contain
5152
break;
5253
}
5354
case PROPERTY: {
54-
final Method getterMethod = getterMethodOrNull( containerJavaType, propertyName );
55+
final var getterMethod = getterMethodOrNull( containerJavaType, propertyName );
5556
if ( getterMethod == null ) {
5657
throw new PropertyAccessBuildingException(
5758
"Could not locate getter for property named [" + containerJavaType.getName() + "#" + propertyName + "]"
5859
);
5960
}
60-
final Method setterMethod = findSetterMethod( containerJavaType, propertyName, getterMethod.getReturnType() );
61+
final var setterMethod = findSetterMethod( containerJavaType, propertyName, getterMethod.getReturnType() );
6162

6263
getter = propertyGetter( containerJavaType, propertyName, getterMethod );
6364
setter = propertySetter( containerJavaType, propertyName, setterMethod );

hibernate-core/src/main/java/org/hibernate/property/access/spi/EnhancedSetterMethodImpl.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ public EnhancedSetterMethodImpl(Class<?> containerClass, String propertyName, Me
3838
@Override
3939
public void set(Object target, @Nullable Object value) {
4040
super.set( target, value );
41-
4241
handleEnhancedInjection( target, value, enhancementState, propertyName );
4342
}
4443

0 commit comments

Comments
 (0)