4
4
*/
5
5
package org .hibernate .property .access .internal ;
6
6
7
+ import jakarta .persistence .AccessType ;
8
+ import org .checkerframework .checker .nullness .qual .Nullable ;
9
+ import org .hibernate .property .access .spi .EnhancedGetterFieldImpl ;
7
10
import org .hibernate .property .access .spi .EnhancedSetterImpl ;
8
11
import org .hibernate .property .access .spi .EnhancedSetterMethodImpl ;
9
12
import org .hibernate .property .access .spi .Getter ;
17
20
import java .lang .reflect .Field ;
18
21
import java .lang .reflect .Method ;
19
22
20
- import jakarta .persistence .AccessType ;
21
- import org .checkerframework .checker .nullness .qual .Nullable ;
22
-
23
+ import static org .hibernate .internal .util .ReflectHelper .findField ;
23
24
import static org .hibernate .internal .util .ReflectHelper .findSetterMethod ;
24
25
import static org .hibernate .internal .util .ReflectHelper .getterMethodOrNull ;
25
26
import static org .hibernate .property .access .internal .AccessStrategyHelper .fieldOrNull ;
@@ -41,10 +42,12 @@ public PropertyAccessEnhancedImpl(
41
42
PropertyAccessStrategy strategy ,
42
43
Class <?> containerJavaType ,
43
44
String propertyName ,
44
- @ Nullable AccessType getterAccessType ) {
45
+ @ Nullable AccessType classAccessType ) {
45
46
this .strategy = strategy ;
46
47
47
- final AccessType propertyAccessType = resolveAccessType ( getterAccessType , containerJavaType , propertyName );
48
+ final AccessType propertyAccessType = classAccessType == null ?
49
+ AccessStrategyHelper .getAccessType ( containerJavaType , propertyName ) :
50
+ classAccessType ;
48
51
49
52
switch ( propertyAccessType ) {
50
53
case FIELD : {
@@ -65,10 +68,8 @@ public PropertyAccessEnhancedImpl(
65
68
"Could not locate getter for property named [" + containerJavaType .getName () + "#" + propertyName + "]"
66
69
);
67
70
}
68
- final Method setterMethod = findSetterMethod ( containerJavaType , propertyName , getterMethod .getReturnType () );
69
-
70
- this .getter = new GetterMethodImpl ( containerJavaType , propertyName , getterMethod );
71
- this .setter = new EnhancedSetterMethodImpl ( containerJavaType , propertyName , setterMethod );
71
+ this .getter = propertyGetter ( classAccessType , containerJavaType , propertyName , getterMethod );
72
+ this .setter = propertySetter ( classAccessType , containerJavaType , propertyName , getterMethod .getReturnType () );
72
73
break ;
73
74
}
74
75
default : {
@@ -79,12 +80,31 @@ public PropertyAccessEnhancedImpl(
79
80
}
80
81
}
81
82
82
- private static AccessType resolveAccessType (@ Nullable AccessType getterAccessType , Class <?> containerJavaType , String propertyName ) {
83
- if ( getterAccessType != null ) {
84
- // this should indicate FIELD access
85
- return getterAccessType ;
83
+ private static Getter propertyGetter (@ Nullable AccessType classAccessType , Class <?> containerJavaType , String propertyName , Method getterMethod ) {
84
+ if ( classAccessType != null ) {
85
+ final AccessType explicitAccessType = AccessStrategyHelper .getAccessType ( containerJavaType , propertyName );
86
+ if ( explicitAccessType == AccessType .FIELD ) {
87
+ // We need to default to FIELD unless we have an explicit AccessType to avoid unnecessary initializations
88
+ final Field field = findField ( containerJavaType , propertyName );
89
+ return new EnhancedGetterFieldImpl ( containerJavaType , propertyName , field , getterMethod );
90
+ }
91
+ }
92
+ // when classAccessType is null know PROPERTY is the explicit access type
93
+ return new GetterMethodImpl ( containerJavaType , propertyName , getterMethod );
94
+ }
95
+
96
+ private static Setter propertySetter (@ Nullable AccessType classAccessType , Class <?> containerJavaType , String propertyName , Class <?> fieldType ) {
97
+ if ( classAccessType != null ) {
98
+ final AccessType explicitAccessType = AccessStrategyHelper .getAccessType ( containerJavaType , propertyName );
99
+ if ( explicitAccessType == AccessType .FIELD ) {
100
+ // We need to default to FIELD unless we have an explicit AccessType to avoid unnecessary initializations
101
+ final Field field = findField ( containerJavaType , propertyName );
102
+ return new EnhancedSetterImpl ( containerJavaType , propertyName , field );
103
+ }
86
104
}
87
- return AccessStrategyHelper .getAccessType ( containerJavaType , propertyName );
105
+ // when classAccessType is null know PROPERTY is the explicit access type
106
+ final Method setterMethod = findSetterMethod ( containerJavaType , propertyName , fieldType );
107
+ return new EnhancedSetterMethodImpl ( containerJavaType , propertyName , setterMethod );
88
108
}
89
109
90
110
@ Override
0 commit comments