Skip to content

Commit 8c319d7

Browse files
committed
cache declared fields and methods of class instances
1 parent 8145598 commit 8c319d7

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

hibernate-core/src/main/java/org/hibernate/internal/util/ReflectHelper.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
import java.lang.reflect.Member;
1414
import java.lang.reflect.Method;
1515
import java.lang.reflect.Modifier;
16+
import java.util.HashMap;
1617
import java.util.Locale;
18+
import java.util.Map;
1719
import java.util.regex.Pattern;
1820
import javax.persistence.Transient;
1921

@@ -49,6 +51,9 @@ public final class ReflectHelper {
4951
private static final Method OBJECT_EQUALS;
5052
private static final Method OBJECT_HASHCODE;
5153

54+
private static final Map<Class<?>, Method[]> METHODS_BY_CLASS = new HashMap<>();
55+
private static final Map<Class<?>, Field[]> FIELDS_BY_CLASS = new HashMap<>();
56+
5257
static {
5358
Method eq;
5459
Method hash;
@@ -62,13 +67,20 @@ public final class ReflectHelper {
6267
OBJECT_EQUALS = eq;
6368
OBJECT_HASHCODE = hash;
6469
}
65-
6670
/**
6771
* Disallow instantiation of ReflectHelper.
6872
*/
6973
private ReflectHelper() {
7074
}
7175

76+
private static Method[] getDeclaredMethodsOfClass(Class<?> clazz) {
77+
return METHODS_BY_CLASS.computeIfAbsent(clazz, name -> clazz.getDeclaredMethods());
78+
}
79+
80+
private static Field[] getDeclaredFieldsOfClass(Class<?> clazz) {
81+
return FIELDS_BY_CLASS.computeIfAbsent(clazz, name -> clazz.getDeclaredFields());
82+
}
83+
7284
/**
7385
* Encapsulation of getting hold of a class's {@link Object#equals equals} method.
7486
*
@@ -423,7 +435,7 @@ private static Field locateField(Class clazz, String propertyName) {
423435
return null;
424436
}
425437

426-
Field field = findField(propertyName, clazz.getDeclaredFields());
438+
Field field = findField(propertyName, getDeclaredFieldsOfClass(clazz));
427439
if (field == null) {
428440
return locateField( clazz.getSuperclass(), propertyName );
429441
}
@@ -488,7 +500,7 @@ private static Method getGetterOrNull(Class[] interfaces, String propertyName) {
488500
}
489501

490502
private static Method getGetterOrNull(Class containerClass, String propertyName) {
491-
Method[] declaredMethods = containerClass.getDeclaredMethods();
503+
Method[] declaredMethods = getDeclaredMethodsOfClass(containerClass);
492504
for ( Method method : declaredMethods) {
493505
// if the method has parameters, skip it
494506
if ( method.getParameterCount() != 0 ) {
@@ -674,7 +686,7 @@ private static Method setterOrNull(Class[] interfaces, String propertyName, Clas
674686
private static Method setterOrNull(Class theClass, String propertyName, Class propertyType) {
675687
Method potentialSetter = null;
676688

677-
for ( Method method : theClass.getDeclaredMethods() ) {
689+
for ( Method method : getDeclaredMethodsOfClass(theClass) ) {
678690
final String methodName = method.getName();
679691
if ( method.getParameterCount() == 1 && methodName.startsWith( "set" ) ) {
680692
final String testOldMethod = methodName.substring( 3 );
@@ -699,7 +711,7 @@ private static Method setterOrNull(Class theClass, String propertyName, Class pr
699711
* as an abstract - but again, that is such an edge case...
700712
*/
701713
public static Method findGetterMethodForFieldAccess(Field field, String propertyName) {
702-
for ( Method method : field.getDeclaringClass().getDeclaredMethods() ) {
714+
for ( Method method : getDeclaredMethodsOfClass(field.getDeclaringClass()) ) {
703715
// if the method has parameters, skip it
704716
if ( method.getParameterCount() != 0 ) {
705717
continue;

0 commit comments

Comments
 (0)