44 */
55package org .hibernate .property .access .internal ;
66
7+ import jakarta .persistence .AccessType ;
8+ import org .checkerframework .checker .nullness .qual .Nullable ;
9+ import org .hibernate .property .access .spi .EnhancedGetterFieldImpl ;
710import org .hibernate .property .access .spi .EnhancedSetterImpl ;
811import org .hibernate .property .access .spi .EnhancedSetterMethodImpl ;
912import org .hibernate .property .access .spi .Getter ;
1720import java .lang .reflect .Field ;
1821import java .lang .reflect .Method ;
1922
20- import jakarta .persistence .AccessType ;
21- import org .checkerframework .checker .nullness .qual .Nullable ;
22-
23+ import static org .hibernate .internal .util .ReflectHelper .findField ;
2324import static org .hibernate .internal .util .ReflectHelper .findSetterMethod ;
2425import static org .hibernate .internal .util .ReflectHelper .getterMethodOrNull ;
2526import static org .hibernate .property .access .internal .AccessStrategyHelper .fieldOrNull ;
@@ -41,10 +42,12 @@ public PropertyAccessEnhancedImpl(
4142 PropertyAccessStrategy strategy ,
4243 Class <?> containerJavaType ,
4344 String propertyName ,
44- @ Nullable AccessType getterAccessType ) {
45+ @ Nullable AccessType classAccessType ) {
4546 this .strategy = strategy ;
4647
47- final AccessType propertyAccessType = resolveAccessType ( getterAccessType , containerJavaType , propertyName );
48+ final AccessType propertyAccessType = classAccessType == null ?
49+ AccessStrategyHelper .getAccessType ( containerJavaType , propertyName ) :
50+ classAccessType ;
4851
4952 switch ( propertyAccessType ) {
5053 case FIELD : {
@@ -65,10 +68,8 @@ public PropertyAccessEnhancedImpl(
6568 "Could not locate getter for property named [" + containerJavaType .getName () + "#" + propertyName + "]"
6669 );
6770 }
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 () );
7273 break ;
7374 }
7475 default : {
@@ -79,12 +80,31 @@ public PropertyAccessEnhancedImpl(
7980 }
8081 }
8182
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+ }
86104 }
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 );
88108 }
89109
90110 @ Override
0 commit comments