Skip to content

Commit e6b1f0a

Browse files
committed
Use constants when caching empty arrays
Reduce cache memory consumption by using a single constant for empty arrays.
1 parent 760bc71 commit e6b1f0a

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ public abstract class ReflectionUtils {
5151
*/
5252
private static final String CGLIB_RENAMED_METHOD_PREFIX = "CGLIB$";
5353

54+
private static final Method[] NO_METHODS = {};
55+
56+
private static final Field[] NO_FIELDS = {};
57+
5458
/**
5559
* Cache for {@link Class#getDeclaredMethods()} plus equivalent default methods
5660
* from Java 8 based interfaces, allowing for fast iteration.
@@ -617,7 +621,7 @@ private static Method[] getDeclaredMethods(Class<?> clazz) {
617621
else {
618622
result = declaredMethods;
619623
}
620-
declaredMethodsCache.put(clazz, result);
624+
declaredMethodsCache.put(clazz, (result.length == 0 ? NO_METHODS : result));
621625
}
622626
return result;
623627
}
@@ -705,7 +709,7 @@ private static Field[] getDeclaredFields(Class<?> clazz) {
705709
Field[] result = declaredFieldsCache.get(clazz);
706710
if (result == null) {
707711
result = clazz.getDeclaredFields();
708-
declaredFieldsCache.put(clazz, result);
712+
declaredFieldsCache.put(clazz, (result.length == 0 ? NO_FIELDS : result));
709713
}
710714
return result;
711715
}

0 commit comments

Comments
 (0)