Browse Source

ReflectivePropertyAccessor caches sorted methods per class

Issue: SPR-16882
pull/1884/head
Juergen Hoeller 8 years ago
parent
commit
da049f480b
  1. 16
      spring-expression/src/main/java/org/springframework/expression/spel/support/ReflectivePropertyAccessor.java

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

@ -81,6 +81,8 @@ public class ReflectivePropertyAccessor implements PropertyAccessor {
private final Map<PropertyCacheKey, TypeDescriptor> typeDescriptorCache = new ConcurrentHashMap<>(64); private final Map<PropertyCacheKey, TypeDescriptor> typeDescriptorCache = new ConcurrentHashMap<>(64);
private final Map<Class<?>, Method[]> sortedMethodsCache = new ConcurrentHashMap<>(64);
@Nullable @Nullable
private volatile InvokerPair lastReadInvokerPair; private volatile InvokerPair lastReadInvokerPair;
@ -403,7 +405,7 @@ public class ReflectivePropertyAccessor implements PropertyAccessor {
private Method findMethodForProperty(String[] methodSuffixes, String prefix, Class<?> clazz, private Method findMethodForProperty(String[] methodSuffixes, String prefix, Class<?> clazz,
boolean mustBeStatic, int numberOfParams, Set<Class<?>> requiredReturnTypes) { boolean mustBeStatic, int numberOfParams, Set<Class<?>> requiredReturnTypes) {
Method[] methods = getSortedClassMethods(clazz); Method[] methods = getSortedMethods(clazz);
for (String methodSuffix : methodSuffixes) { for (String methodSuffix : methodSuffixes) {
for (Method method : methods) { for (Method method : methods) {
if (isCandidateForProperty(method, clazz) && method.getName().equals(prefix + methodSuffix) && if (isCandidateForProperty(method, clazz) && method.getName().equals(prefix + methodSuffix) &&
@ -431,12 +433,14 @@ public class ReflectivePropertyAccessor implements PropertyAccessor {
} }
/** /**
* Return class methods ordered with non bridge methods appearing higher. * Return class methods ordered with non-bridge methods appearing higher.
*/ */
private Method[] getSortedClassMethods(Class<?> clazz) { private Method[] getSortedMethods(Class<?> clazz) {
Method[] methods = clazz.getMethods(); return this.sortedMethodsCache.computeIfAbsent(clazz, key -> {
Arrays.sort(methods, (o1, o2) -> (o1.isBridge() == o2.isBridge() ? 0 : (o1.isBridge() ? 1 : -1))); Method[] methods = key.getMethods();
return methods; Arrays.sort(methods, (o1, o2) -> (o1.isBridge() == o2.isBridge() ? 0 : (o1.isBridge() ? 1 : -1)));
return methods;
});
} }
/** /**

Loading…
Cancel
Save