13
13
import java .lang .reflect .Member ;
14
14
import java .lang .reflect .Method ;
15
15
import java .lang .reflect .Modifier ;
16
+ import java .util .HashMap ;
16
17
import java .util .Locale ;
18
+ import java .util .Map ;
17
19
import java .util .regex .Pattern ;
18
20
import javax .persistence .Transient ;
19
21
@@ -49,6 +51,9 @@ public final class ReflectHelper {
49
51
private static final Method OBJECT_EQUALS ;
50
52
private static final Method OBJECT_HASHCODE ;
51
53
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
+
52
57
static {
53
58
Method eq ;
54
59
Method hash ;
@@ -62,13 +67,20 @@ public final class ReflectHelper {
62
67
OBJECT_EQUALS = eq ;
63
68
OBJECT_HASHCODE = hash ;
64
69
}
65
-
66
70
/**
67
71
* Disallow instantiation of ReflectHelper.
68
72
*/
69
73
private ReflectHelper () {
70
74
}
71
75
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
+
72
84
/**
73
85
* Encapsulation of getting hold of a class's {@link Object#equals equals} method.
74
86
*
@@ -423,7 +435,7 @@ private static Field locateField(Class clazz, String propertyName) {
423
435
return null ;
424
436
}
425
437
426
- Field field = findField (propertyName , clazz . getDeclaredFields ( ));
438
+ Field field = findField (propertyName , getDeclaredFieldsOfClass ( clazz ));
427
439
if (field == null ) {
428
440
return locateField ( clazz .getSuperclass (), propertyName );
429
441
}
@@ -488,7 +500,7 @@ private static Method getGetterOrNull(Class[] interfaces, String propertyName) {
488
500
}
489
501
490
502
private static Method getGetterOrNull (Class containerClass , String propertyName ) {
491
- Method [] declaredMethods = containerClass . getDeclaredMethods ( );
503
+ Method [] declaredMethods = getDeclaredMethodsOfClass ( containerClass );
492
504
for ( Method method : declaredMethods ) {
493
505
// if the method has parameters, skip it
494
506
if ( method .getParameterCount () != 0 ) {
@@ -674,7 +686,7 @@ private static Method setterOrNull(Class[] interfaces, String propertyName, Clas
674
686
private static Method setterOrNull (Class theClass , String propertyName , Class propertyType ) {
675
687
Method potentialSetter = null ;
676
688
677
- for ( Method method : theClass . getDeclaredMethods ( ) ) {
689
+ for ( Method method : getDeclaredMethodsOfClass ( theClass ) ) {
678
690
final String methodName = method .getName ();
679
691
if ( method .getParameterCount () == 1 && methodName .startsWith ( "set" ) ) {
680
692
final String testOldMethod = methodName .substring ( 3 );
@@ -699,7 +711,7 @@ private static Method setterOrNull(Class theClass, String propertyName, Class pr
699
711
* as an abstract - but again, that is such an edge case...
700
712
*/
701
713
public static Method findGetterMethodForFieldAccess (Field field , String propertyName ) {
702
- for ( Method method : field .getDeclaringClass (). getDeclaredMethods ( ) ) {
714
+ for ( Method method : getDeclaredMethodsOfClass ( field .getDeclaringClass ()) ) {
703
715
// if the method has parameters, skip it
704
716
if ( method .getParameterCount () != 0 ) {
705
717
continue ;
0 commit comments