From 789d3c9314b294e70e380f3874b213fee6f80c00 Mon Sep 17 00:00:00 2001 From: Olivier Bourgain Date: Tue, 4 Mar 2025 16:29:24 +0100 Subject: [PATCH 1/2] HHH-19229 Faster checks in ReflectHelper --- .../internal/util/ReflectHelper.java | 31 +++++++++---------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/internal/util/ReflectHelper.java b/hibernate-core/src/main/java/org/hibernate/internal/util/ReflectHelper.java index 03791d0488ab..c78b7c6faab3 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/util/ReflectHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/util/ReflectHelper.java @@ -573,16 +573,15 @@ public static void verifyNoIsVariantExists( Method getMethod, 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 ) { - // No such method should throw the caught exception. So if we get here, there was - // such a method. - checkGetAndIsVariants( containerClass, propertyName, getMethod, isMethod ); + for ( Method declaredMethod : containerClass.getDeclaredMethods() ) { + if ( declaredMethod.getParameterCount() == 0 + && !Modifier.isStatic( declaredMethod.getModifiers() ) + && declaredMethod.getName().startsWith("is") + && declaredMethod.getName().regionMatches(0, stemName, 0, stemName.length() ) + && declaredMethod.getAnnotation( Transient.class ) == null ) { + checkGetAndIsVariants( containerClass, propertyName, getMethod, declaredMethod ); } } - catch (NoSuchMethodException ignore) { - } } @@ -613,17 +612,15 @@ public static void verifyNoGetVariantExists( Method isMethod, String stemName) { // verify that the Class does not also define a method with the same stem name with 'is' - try { - final Method getMethod = containerClass.getDeclaredMethod( "get" + stemName ); - // No such method should throw the caught exception. So if we get here, there was - // such a method. - if ( !Modifier.isStatic( getMethod.getModifiers() ) - && getMethod.getAnnotation( Transient.class ) == null ) { - checkGetAndIsVariants( containerClass, propertyName, getMethod, isMethod ); + for ( Method declaredMethod : containerClass.getDeclaredMethods() ) { + if ( declaredMethod.getParameterCount() == 0 + && !Modifier.isStatic( declaredMethod.getModifiers() ) + && declaredMethod.getName().startsWith("is") + && declaredMethod.getName().regionMatches(0, stemName, 0, stemName.length() ) + && declaredMethod.getAnnotation( Transient.class ) == null ) { + checkGetAndIsVariants( containerClass, propertyName, declaredMethod, isMethod ); } } - catch (NoSuchMethodException ignore) { - } } public static Method getterMethodOrNull(Class containerJavaType, String propertyName) { From 9c70df3d21790c18e420da01e05d179bdb6a37d6 Mon Sep 17 00:00:00 2001 From: Olivier Bourgain Date: Fri, 14 Mar 2025 07:22:50 +0100 Subject: [PATCH 2/2] Apply suggestions from code review Co-authored-by: Marco Belladelli --- .../java/org/hibernate/internal/util/ReflectHelper.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/internal/util/ReflectHelper.java b/hibernate-core/src/main/java/org/hibernate/internal/util/ReflectHelper.java index c78b7c6faab3..05cd1950ca89 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/util/ReflectHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/util/ReflectHelper.java @@ -576,8 +576,8 @@ public static void verifyNoIsVariantExists( for ( Method declaredMethod : containerClass.getDeclaredMethods() ) { if ( declaredMethod.getParameterCount() == 0 && !Modifier.isStatic( declaredMethod.getModifiers() ) - && declaredMethod.getName().startsWith("is") - && declaredMethod.getName().regionMatches(0, stemName, 0, stemName.length() ) + && declaredMethod.getName().startsWith( "is" ) + && declaredMethod.getName().regionMatches( 2, stemName, 0, stemName.length() ) && declaredMethod.getAnnotation( Transient.class ) == null ) { checkGetAndIsVariants( containerClass, propertyName, getMethod, declaredMethod ); } @@ -615,8 +615,8 @@ public static void verifyNoGetVariantExists( for ( Method declaredMethod : containerClass.getDeclaredMethods() ) { if ( declaredMethod.getParameterCount() == 0 && !Modifier.isStatic( declaredMethod.getModifiers() ) - && declaredMethod.getName().startsWith("is") - && declaredMethod.getName().regionMatches(0, stemName, 0, stemName.length() ) + && declaredMethod.getName().startsWith( "get" ) + && declaredMethod.getName().regionMatches( 3, stemName, 0, stemName.length() ) && declaredMethod.getAnnotation( Transient.class ) == null ) { checkGetAndIsVariants( containerClass, propertyName, declaredMethod, isMethod ); }