ReflectivePropertyAccessor caches sorted methods per class

Issue: SPR-16882
This commit is contained in:
Juergen Hoeller
2018-06-11 16:47:52 +02:00
parent 357ca21675
commit 4a6e9a5557

View File

@@ -83,7 +83,10 @@ public class ReflectivePropertyAccessor implements PropertyAccessor {
private final Map<PropertyCacheKey, TypeDescriptor> typeDescriptorCache =
new ConcurrentHashMap<PropertyCacheKey, TypeDescriptor>(64);
private InvokerPair lastReadInvokerPair;
private final Map<Class<?>, Method[]> sortedMethodsCache =
new ConcurrentHashMap<Class<?>, Method[]>(64);
private volatile InvokerPair lastReadInvokerPair;
/**
@@ -277,6 +280,7 @@ public class ReflectivePropertyAccessor implements PropertyAccessor {
throw new AccessException("Type conversion failure", evaluationException);
}
}
PropertyCacheKey cacheKey = new PropertyCacheKey(type, name, target instanceof Class);
Member cachedMember = this.writerCache.get(cacheKey);
@@ -400,7 +404,7 @@ public class ReflectivePropertyAccessor implements PropertyAccessor {
private Method findMethodForProperty(String[] methodSuffixes, String prefix, Class<?> clazz,
boolean mustBeStatic, int numberOfParams, Set<Class<?>> requiredReturnTypes) {
Method[] methods = getSortedClassMethods(clazz);
Method[] methods = getSortedMethods(clazz);
for (String methodSuffix : methodSuffixes) {
for (Method method : methods) {
if (isCandidateForProperty(method, clazz) && method.getName().equals(prefix + methodSuffix) &&
@@ -428,16 +432,20 @@ 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) {
Method[] methods = clazz.getMethods();
Arrays.sort(methods, new Comparator<Method>() {
@Override
public int compare(Method o1, Method o2) {
return (o1.isBridge() == o2.isBridge()) ? 0 : (o1.isBridge() ? 1 : -1);
}
});
private Method[] getSortedMethods(Class<?> clazz) {
Method[] methods = this.sortedMethodsCache.get(clazz);
if (methods == null) {
methods = clazz.getMethods();
Arrays.sort(methods, new Comparator<Method>() {
@Override
public int compare(Method o1, Method o2) {
return (o1.isBridge() == o2.isBridge()) ? 0 : (o1.isBridge() ? 1 : -1);
}
});
this.sortedMethodsCache.put(clazz, methods);
}
return methods;
}