Browse Source

Use constants when caching empty arrays

Reduce cache memory consumption by using a single constant for empty
arrays.
pull/917/head
Phillip Webb 9 years ago
parent
commit
e6b1f0a139
  1. 8
      spring-core/src/main/java/org/springframework/util/ReflectionUtils.java

8
spring-core/src/main/java/org/springframework/util/ReflectionUtils.java

@ -51,6 +51,10 @@ public abstract class ReflectionUtils { @@ -51,6 +51,10 @@ public abstract class ReflectionUtils {
*/
private static final String CGLIB_RENAMED_METHOD_PREFIX = "CGLIB$";
private static final Method[] NO_METHODS = {};
private static final Field[] NO_FIELDS = {};
/**
* Cache for {@link Class#getDeclaredMethods()} plus equivalent default methods
* from Java 8 based interfaces, allowing for fast iteration.
@ -617,7 +621,7 @@ public abstract class ReflectionUtils { @@ -617,7 +621,7 @@ public abstract class ReflectionUtils {
else {
result = declaredMethods;
}
declaredMethodsCache.put(clazz, result);
declaredMethodsCache.put(clazz, (result.length == 0 ? NO_METHODS : result));
}
return result;
}
@ -705,7 +709,7 @@ public abstract class ReflectionUtils { @@ -705,7 +709,7 @@ public abstract class ReflectionUtils {
Field[] result = declaredFieldsCache.get(clazz);
if (result == null) {
result = clazz.getDeclaredFields();
declaredFieldsCache.put(clazz, result);
declaredFieldsCache.put(clazz, (result.length == 0 ? NO_FIELDS : result));
}
return result;
}

Loading…
Cancel
Save