66 */
77package org .hibernate .property .access .internal ;
88
9+ import jakarta .persistence .AccessType ;
10+ import org .checkerframework .checker .nullness .qual .Nullable ;
11+ import org .hibernate .property .access .spi .EnhancedGetterFieldImpl ;
912import org .hibernate .property .access .spi .EnhancedSetterImpl ;
1013import org .hibernate .property .access .spi .EnhancedSetterMethodImpl ;
1114import org .hibernate .property .access .spi .Getter ;
1922import java .lang .reflect .Field ;
2023import java .lang .reflect .Method ;
2124
22- import jakarta .persistence .AccessType ;
23- import org .checkerframework .checker .nullness .qual .Nullable ;
24-
25+ import static org .hibernate .internal .util .ReflectHelper .findField ;
2526import static org .hibernate .internal .util .ReflectHelper .findSetterMethod ;
2627import static org .hibernate .internal .util .ReflectHelper .getterMethodOrNull ;
2728import static org .hibernate .property .access .internal .AccessStrategyHelper .fieldOrNull ;
@@ -43,10 +44,12 @@ public PropertyAccessEnhancedImpl(
4344 PropertyAccessStrategy strategy ,
4445 Class <?> containerJavaType ,
4546 String propertyName ,
46- @ Nullable AccessType getterAccessType ) {
47+ @ Nullable AccessType classAccessType ) {
4748 this .strategy = strategy ;
4849
49- final AccessType propertyAccessType = resolveAccessType ( getterAccessType , containerJavaType , propertyName );
50+ final AccessType propertyAccessType = classAccessType == null ?
51+ AccessStrategyHelper .getAccessType ( containerJavaType , propertyName ) :
52+ classAccessType ;
5053
5154 switch ( propertyAccessType ) {
5255 case FIELD : {
@@ -67,10 +70,8 @@ public PropertyAccessEnhancedImpl(
6770 "Could not locate getter for property named [" + containerJavaType .getName () + "#" + propertyName + "]"
6871 );
6972 }
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 () );
7475 break ;
7576 }
7677 default : {
@@ -81,12 +82,31 @@ public PropertyAccessEnhancedImpl(
8182 }
8283 }
8384
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+ }
88106 }
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 );
90110 }
91111
92112 @ Override
0 commit comments