Skip to content

Commit 9aed525

Browse files
committed
HHH-19140 Fix for issue
1 parent cbd02a3 commit 9aed525

File tree

1 file changed

+6
-87
lines changed

1 file changed

+6
-87
lines changed

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

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

7-
import java.beans.Introspector;
87
import java.lang.reflect.AnnotatedElement;
98
import java.lang.reflect.Field;
109
import java.lang.reflect.Method;
1110
import java.lang.reflect.Modifier;
12-
import java.util.Locale;
1311

14-
import org.hibernate.MappingException;
1512
import org.hibernate.PropertyNotFoundException;
1613
import org.hibernate.bytecode.enhance.spi.interceptor.BytecodeLazyAttributeInterceptor;
1714
import org.hibernate.engine.spi.CompositeOwner;
@@ -31,6 +28,7 @@
3128
import static org.hibernate.engine.internal.ManagedTypeHelper.isPersistentAttributeInterceptableType;
3229
import static org.hibernate.internal.util.ReflectHelper.NO_PARAM_SIGNATURE;
3330
import static org.hibernate.internal.util.ReflectHelper.findField;
31+
import static org.hibernate.internal.util.ReflectHelper.getterMethodOrNull;
3432
import static org.hibernate.internal.util.ReflectHelper.isRecord;
3533

3634
/**
@@ -82,92 +80,13 @@ public static AccessType getAccessType(Class<?> containerJavaType, String proper
8280
&& field.isAnnotationPresent( Access.class )
8381
&& !field.isAnnotationPresent( Transient.class )
8482
&& !Modifier.isStatic( field.getModifiers() ) ) {
85-
return AccessType.FIELD;
83+
return getAccessTypeOrNull( field );
8684
}
8785

88-
for ( Method method : containerClass.getDeclaredMethods() ) {
89-
// if the method has parameters, skip it
90-
if ( method.getParameterCount() != 0 ) {
91-
continue;
92-
}
93-
94-
// if the method is a "bridge", skip it
95-
if ( method.isBridge() ) {
96-
continue;
97-
}
98-
99-
if ( method.isAnnotationPresent( Transient.class ) ) {
100-
continue;
101-
}
102-
103-
if ( Modifier.isStatic( method.getModifiers() ) ) {
104-
continue;
105-
}
106-
107-
final String methodName = method.getName();
108-
109-
// try "get"
110-
if ( methodName.startsWith( "get" ) ) {
111-
final String stemName = methodName.substring( 3 );
112-
final String decapitalizedStemName = Introspector.decapitalize( stemName );
113-
if ( stemName.equals( propertyName ) || decapitalizedStemName.equals( propertyName ) ) {
114-
if ( method.isAnnotationPresent( Access.class ) ) {
115-
return AccessType.PROPERTY;
116-
}
117-
else {
118-
checkIsMethodVariant( containerClass, propertyName, method, stemName );
119-
}
120-
}
121-
}
122-
123-
// if not "get", then try "is"
124-
if ( methodName.startsWith( "is" ) ) {
125-
final String stemName = methodName.substring( 2 );
126-
String decapitalizedStemName = Introspector.decapitalize( stemName );
127-
if ( stemName.equals( propertyName ) || decapitalizedStemName.equals( propertyName ) ) {
128-
if ( method.isAnnotationPresent( Access.class ) ) {
129-
return AccessType.PROPERTY;
130-
}
131-
}
132-
}
133-
}
134-
135-
return null;
136-
}
137-
138-
private static void checkIsMethodVariant(
139-
Class<?> containerClass,
140-
String propertyName,
141-
Method method,
142-
String stemName) {
143-
final Method isMethodVariant = findIsMethodVariant( containerClass, stemName );
144-
if ( isMethodVariant == null ) {
145-
return;
146-
}
147-
148-
if ( !isMethodVariant.isAnnotationPresent( Access.class ) ) {
149-
throw new MappingException(
150-
String.format(
151-
Locale.ROOT,
152-
"Class '%s' declares both 'get' [%s] and 'is' [%s] variants of getter for property '%s'",
153-
containerClass.getName(),
154-
method.toString(),
155-
isMethodVariant,
156-
propertyName
157-
)
158-
);
159-
}
160-
}
161-
162-
public static @Nullable Method findIsMethodVariant(Class<?> containerClass, String stemName) {
163-
// verify that the Class does not also define a method with the same stem name with 'is'
164-
try {
165-
final Method isMethod = containerClass.getDeclaredMethod( "is" + stemName );
166-
if ( !Modifier.isStatic( isMethod.getModifiers() ) && isMethod.getAnnotation( Transient.class ) == null ) {
167-
return isMethod;
168-
}
169-
}
170-
catch (NoSuchMethodException ignore) {
86+
Method getter = getterMethodOrNull( containerClass, propertyName );
87+
if (getter != null
88+
&& getter.isAnnotationPresent( Access.class )){
89+
return AccessType.PROPERTY;
17190
}
17291

17392
return null;

0 commit comments

Comments
 (0)