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