Skip to content

Commit

Permalink
HHH-19140 Fix for issue
Browse files Browse the repository at this point in the history
  • Loading branch information
VladoKuruc committed Feb 25, 2025
1 parent cbd02a3 commit 9aed525
Showing 1 changed file with 6 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@
*/
package org.hibernate.property.access.internal;

import java.beans.Introspector;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Locale;

import org.hibernate.MappingException;
import org.hibernate.PropertyNotFoundException;
import org.hibernate.bytecode.enhance.spi.interceptor.BytecodeLazyAttributeInterceptor;
import org.hibernate.engine.spi.CompositeOwner;
Expand All @@ -31,6 +28,7 @@
import static org.hibernate.engine.internal.ManagedTypeHelper.isPersistentAttributeInterceptableType;
import static org.hibernate.internal.util.ReflectHelper.NO_PARAM_SIGNATURE;
import static org.hibernate.internal.util.ReflectHelper.findField;
import static org.hibernate.internal.util.ReflectHelper.getterMethodOrNull;
import static org.hibernate.internal.util.ReflectHelper.isRecord;

/**
Expand Down Expand Up @@ -82,92 +80,13 @@ public static AccessType getAccessType(Class<?> containerJavaType, String proper
&& field.isAnnotationPresent( Access.class )
&& !field.isAnnotationPresent( Transient.class )
&& !Modifier.isStatic( field.getModifiers() ) ) {
return AccessType.FIELD;
return getAccessTypeOrNull( field );
}

for ( Method method : containerClass.getDeclaredMethods() ) {
// if the method has parameters, skip it
if ( method.getParameterCount() != 0 ) {
continue;
}

// if the method is a "bridge", skip it
if ( method.isBridge() ) {
continue;
}

if ( method.isAnnotationPresent( Transient.class ) ) {
continue;
}

if ( Modifier.isStatic( method.getModifiers() ) ) {
continue;
}

final String methodName = method.getName();

// try "get"
if ( methodName.startsWith( "get" ) ) {
final String stemName = methodName.substring( 3 );
final String decapitalizedStemName = Introspector.decapitalize( stemName );
if ( stemName.equals( propertyName ) || decapitalizedStemName.equals( propertyName ) ) {
if ( method.isAnnotationPresent( Access.class ) ) {
return AccessType.PROPERTY;
}
else {
checkIsMethodVariant( containerClass, propertyName, method, stemName );
}
}
}

// if not "get", then try "is"
if ( methodName.startsWith( "is" ) ) {
final String stemName = methodName.substring( 2 );
String decapitalizedStemName = Introspector.decapitalize( stemName );
if ( stemName.equals( propertyName ) || decapitalizedStemName.equals( propertyName ) ) {
if ( method.isAnnotationPresent( Access.class ) ) {
return AccessType.PROPERTY;
}
}
}
}

return null;
}

private static void checkIsMethodVariant(
Class<?> containerClass,
String propertyName,
Method method,
String stemName) {
final Method isMethodVariant = findIsMethodVariant( containerClass, stemName );
if ( isMethodVariant == null ) {
return;
}

if ( !isMethodVariant.isAnnotationPresent( Access.class ) ) {
throw new MappingException(
String.format(
Locale.ROOT,
"Class '%s' declares both 'get' [%s] and 'is' [%s] variants of getter for property '%s'",
containerClass.getName(),
method.toString(),
isMethodVariant,
propertyName
)
);
}
}

public static @Nullable Method findIsMethodVariant(Class<?> containerClass, String stemName) {
// verify that the Class does not also define a method with the same stem name with 'is'
try {
final Method isMethod = containerClass.getDeclaredMethod( "is" + stemName );
if ( !Modifier.isStatic( isMethod.getModifiers() ) && isMethod.getAnnotation( Transient.class ) == null ) {
return isMethod;
}
}
catch (NoSuchMethodException ignore) {
Method getter = getterMethodOrNull( containerClass, propertyName );
if (getter != null
&& getter.isAnnotationPresent( Access.class )){
return AccessType.PROPERTY;
}

return null;
Expand Down

0 comments on commit 9aed525

Please sign in to comment.