Skip to content

Commit caaa45c

Browse files
author
Costin Leau
committed
+ fixed internal caching for LocalVariableTableParameterNameDiscoverer
1 parent 6389097 commit caaa45c

File tree

2 files changed

+23
-19
lines changed

2 files changed

+23
-19
lines changed

org.springframework.core/src/main/java/org/springframework/core/LocalVariableTableParameterNameDiscoverer.java

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -57,21 +57,19 @@ public class LocalVariableTableParameterNameDiscoverer implements ParameterNameD
5757
// the cache uses a nested index (value is a map) to keep the top level cache relatively small in size
5858
private final Map<Class<?>, Map<Member, String[]>> parameterNamesCache = new ConcurrentHashMap<Class<?>, Map<Member, String[]>>();
5959

60-
// marker object for members that have been visited yet no params have been discovered for it
61-
private static final String[] EMPTY_NAMES_ARRAY = new String[0];
6260
// marker object for classes that do not have any debug info
63-
private static final Map<Member, String[]> EMPTY_MAP = Collections.emptyMap();
61+
private static final Map<Member, String[]> NO_DEBUG_INFO_MAP = Collections.emptyMap();
6462

6563
public String[] getParameterNames(Method method) {
6664
Class<?> declaringClass = method.getDeclaringClass();
6765
Map<Member, String[]> map = parameterNamesCache.get(declaringClass);
6866
if (map == null) {
6967
// initialize cache
70-
map = cacheClass(declaringClass);
68+
map = inspectClass(declaringClass);
69+
parameterNamesCache.put(declaringClass, map);
7170
}
72-
if (map != EMPTY_MAP) {
73-
String[] paramNames = map.get(method);
74-
return (paramNames == EMPTY_NAMES_ARRAY ? null : paramNames);
71+
if (map != NO_DEBUG_INFO_MAP) {
72+
return map.get(method);
7573
}
7674
return null;
7775
}
@@ -82,20 +80,23 @@ public String[] getParameterNames(Constructor ctor) {
8280
Map<Member, String[]> map = parameterNamesCache.get(declaringClass);
8381
if (map == null) {
8482
// initialize cache
85-
map = cacheClass(declaringClass);
83+
map = inspectClass(declaringClass);
84+
parameterNamesCache.put(declaringClass, map);
8685
}
87-
String[] paramNames = map.get(ctor);
88-
return (paramNames == EMPTY_NAMES_ARRAY ? null : paramNames);
86+
if (map != NO_DEBUG_INFO_MAP) {
87+
return map.get(ctor);
88+
}
89+
90+
return null;
8991
}
9092

9193
/**
92-
* Caches the results of the introspected class. Exceptions will be logged and swallowed.
93-
*
94-
* @param declaringClass class about to be inspected
95-
* @return cached value
94+
* Inspects the target class. Exceptions will be logged and a maker map returned to indicate the lack of debug information.
95+
*
96+
* @param clazz
97+
* @return
9698
*/
97-
private Map<Member, String[]> cacheClass(Class<?> clazz) {
98-
99+
private Map<Member, String[]> inspectClass(Class<?> clazz) {
99100
InputStream is = clazz.getResourceAsStream(ClassUtils.getClassFileName(clazz));
100101
if (is == null) {
101102
// We couldn't load the class file, which is not fatal as it
@@ -104,7 +105,8 @@ private Map<Member, String[]> cacheClass(Class<?> clazz) {
104105
logger.debug("Cannot find '.class' file for class [" + clazz
105106
+ "] - unable to determine constructors/methods parameter names");
106107
}
107-
return Collections.emptyMap();
108+
109+
return NO_DEBUG_INFO_MAP;
108110
}
109111

110112
try {
@@ -125,7 +127,7 @@ private Map<Member, String[]> cacheClass(Class<?> clazz) {
125127
}
126128
}
127129

128-
return Collections.emptyMap();
130+
return NO_DEBUG_INFO_MAP;
129131
}
130132

131133
/**

org.springframework.core/src/test/java/org/springframework/core/LocalVariableTableParameterNameDiscovererTests.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,8 @@ public void testGenerifiedClass() throws Exception {
188188
m = clazz.getMethod("getDate", null);
189189
names = discoverer.getParameterNames(m);
190190
assertEquals(0, names.length);
191+
192+
//System.in.read();
191193
}
192194

193195
public void testMemUsage() throws Exception {
@@ -207,7 +209,7 @@ public void testMemUsage() throws Exception {
207209
names = discoverer.getParameterNames(m);
208210
assertNull(names);
209211

210-
//System.in.read()
212+
//System.in.read();
211213
}
212214

213215
public static void staticMethodNoLocalVars() {

0 commit comments

Comments
 (0)