Skip to content

Commit da049f4

Browse files
committed
ReflectivePropertyAccessor caches sorted methods per class
Issue: SPR-16882
1 parent 455d8ac commit da049f4

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

spring-expression/src/main/java/org/springframework/expression/spel/support/ReflectivePropertyAccessor.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ public class ReflectivePropertyAccessor implements PropertyAccessor {
8181

8282
private final Map<PropertyCacheKey, TypeDescriptor> typeDescriptorCache = new ConcurrentHashMap<>(64);
8383

84+
private final Map<Class<?>, Method[]> sortedMethodsCache = new ConcurrentHashMap<>(64);
85+
8486
@Nullable
8587
private volatile InvokerPair lastReadInvokerPair;
8688

@@ -403,7 +405,7 @@ protected Method findSetterForProperty(String propertyName, Class<?> clazz, bool
403405
private Method findMethodForProperty(String[] methodSuffixes, String prefix, Class<?> clazz,
404406
boolean mustBeStatic, int numberOfParams, Set<Class<?>> requiredReturnTypes) {
405407

406-
Method[] methods = getSortedClassMethods(clazz);
408+
Method[] methods = getSortedMethods(clazz);
407409
for (String methodSuffix : methodSuffixes) {
408410
for (Method method : methods) {
409411
if (isCandidateForProperty(method, clazz) && method.getName().equals(prefix + methodSuffix) &&
@@ -431,12 +433,14 @@ protected boolean isCandidateForProperty(Method method, Class<?> targetClass) {
431433
}
432434

433435
/**
434-
* Return class methods ordered with non bridge methods appearing higher.
436+
* Return class methods ordered with non-bridge methods appearing higher.
435437
*/
436-
private Method[] getSortedClassMethods(Class<?> clazz) {
437-
Method[] methods = clazz.getMethods();
438-
Arrays.sort(methods, (o1, o2) -> (o1.isBridge() == o2.isBridge() ? 0 : (o1.isBridge() ? 1 : -1)));
439-
return methods;
438+
private Method[] getSortedMethods(Class<?> clazz) {
439+
return this.sortedMethodsCache.computeIfAbsent(clazz, key -> {
440+
Method[] methods = key.getMethods();
441+
Arrays.sort(methods, (o1, o2) -> (o1.isBridge() == o2.isBridge() ? 0 : (o1.isBridge() ? 1 : -1)));
442+
return methods;
443+
});
440444
}
441445

442446
/**

0 commit comments

Comments
 (0)