Skip to content

Commit 699d3f1

Browse files
committed
Simplify LocalVariableTableParameterNameDiscoverer implementation
This commit refactors the internals of LocalVariableTableParameterNameDiscoverer to use java.lang.reflect.Executable in order to simplify the implementation.
1 parent 309b328 commit 699d3f1

File tree

1 file changed

+30
-31
lines changed

1 file changed

+30
-31
lines changed

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

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,7 +19,7 @@
1919
import java.io.IOException;
2020
import java.io.InputStream;
2121
import java.lang.reflect.Constructor;
22-
import java.lang.reflect.Member;
22+
import java.lang.reflect.Executable;
2323
import java.lang.reflect.Method;
2424
import java.util.Collections;
2525
import java.util.Map;
@@ -51,47 +51,46 @@
5151
* @author Costin Leau
5252
* @author Juergen Hoeller
5353
* @author Chris Beams
54+
* @author Sam Brannen
5455
* @since 2.0
5556
*/
5657
public class LocalVariableTableParameterNameDiscoverer implements ParameterNameDiscoverer {
5758

5859
private static final Log logger = LogFactory.getLog(LocalVariableTableParameterNameDiscoverer.class);
5960

6061
// marker object for classes that do not have any debug info
61-
private static final Map<Member, String[]> NO_DEBUG_INFO_MAP = Collections.emptyMap();
62+
private static final Map<Executable, String[]> NO_DEBUG_INFO_MAP = Collections.emptyMap();
6263

6364
// the cache uses a nested index (value is a map) to keep the top level cache relatively small in size
64-
private final Map<Class<?>, Map<Member, String[]>> parameterNamesCache = new ConcurrentHashMap<>(32);
65+
private final Map<Class<?>, Map<Executable, String[]>> parameterNamesCache = new ConcurrentHashMap<>(32);
6566

6667

6768
@Override
6869
@Nullable
6970
public String[] getParameterNames(Method method) {
7071
Method originalMethod = BridgeMethodResolver.findBridgedMethod(method);
71-
Class<?> declaringClass = originalMethod.getDeclaringClass();
72-
Map<Member, String[]> map = this.parameterNamesCache.computeIfAbsent(declaringClass, this::inspectClass);
73-
if (map != NO_DEBUG_INFO_MAP) {
74-
return map.get(originalMethod);
75-
}
76-
return null;
72+
return doGetParameterNames(originalMethod);
7773
}
7874

7975
@Override
8076
@Nullable
8177
public String[] getParameterNames(Constructor<?> ctor) {
82-
Class<?> declaringClass = ctor.getDeclaringClass();
83-
Map<Member, String[]> map = this.parameterNamesCache.computeIfAbsent(declaringClass, this::inspectClass);
84-
if (map != NO_DEBUG_INFO_MAP) {
85-
return map.get(ctor);
86-
}
87-
return null;
78+
return doGetParameterNames(ctor);
79+
}
80+
81+
@Nullable
82+
private String[] doGetParameterNames(Executable executable) {
83+
Class<?> declaringClass = executable.getDeclaringClass();
84+
Map<Executable, String[]> map = this.parameterNamesCache.computeIfAbsent(declaringClass, this::inspectClass);
85+
return (map != NO_DEBUG_INFO_MAP ? map.get(executable) : null);
8886
}
8987

9088
/**
91-
* Inspects the target class. Exceptions will be logged and a maker map returned
92-
* to indicate the lack of debug information.
89+
* Inspects the target class.
90+
* <p>Exceptions will be logged, and a marker map returned to indicate the
91+
* lack of debug information.
9392
*/
94-
private Map<Member, String[]> inspectClass(Class<?> clazz) {
93+
private Map<Executable, String[]> inspectClass(Class<?> clazz) {
9594
InputStream is = clazz.getResourceAsStream(ClassUtils.getClassFileName(clazz));
9695
if (is == null) {
9796
// We couldn't load the class file, which is not fatal as it
@@ -104,7 +103,7 @@ private Map<Member, String[]> inspectClass(Class<?> clazz) {
104103
}
105104
try {
106105
ClassReader classReader = new ClassReader(is);
107-
Map<Member, String[]> map = new ConcurrentHashMap<>(32);
106+
Map<Executable, String[]> map = new ConcurrentHashMap<>(32);
108107
classReader.accept(new ParameterNameDiscoveringVisitor(clazz, map), 0);
109108
return map;
110109
}
@@ -134,29 +133,29 @@ private Map<Member, String[]> inspectClass(Class<?> clazz) {
134133

135134

136135
/**
137-
* Helper class that inspects all methods (constructor included) and then
138-
* attempts to find the parameter names for that member.
136+
* Helper class that inspects all methods and constructors and then
137+
* attempts to find the parameter names for the given {@link Executable}.
139138
*/
140139
private static class ParameterNameDiscoveringVisitor extends ClassVisitor {
141140

142141
private static final String STATIC_CLASS_INIT = "<clinit>";
143142

144143
private final Class<?> clazz;
145144

146-
private final Map<Member, String[]> memberMap;
145+
private final Map<Executable, String[]> executableMap;
147146

148-
public ParameterNameDiscoveringVisitor(Class<?> clazz, Map<Member, String[]> memberMap) {
147+
public ParameterNameDiscoveringVisitor(Class<?> clazz, Map<Executable, String[]> executableMap) {
149148
super(SpringAsmInfo.ASM_VERSION);
150149
this.clazz = clazz;
151-
this.memberMap = memberMap;
150+
this.executableMap = executableMap;
152151
}
153152

154153
@Override
155154
@Nullable
156155
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
157156
// exclude synthetic + bridged && static class initialization
158157
if (!isSyntheticOrBridged(access) && !STATIC_CLASS_INIT.equals(name)) {
159-
return new LocalVariableTableVisitor(this.clazz, this.memberMap, name, desc, isStatic(access));
158+
return new LocalVariableTableVisitor(this.clazz, this.executableMap, name, desc, isStatic(access));
160159
}
161160
return null;
162161
}
@@ -177,7 +176,7 @@ private static class LocalVariableTableVisitor extends MethodVisitor {
177176

178177
private final Class<?> clazz;
179178

180-
private final Map<Member, String[]> memberMap;
179+
private final Map<Executable, String[]> executableMap;
181180

182181
private final String name;
183182

@@ -195,10 +194,10 @@ private static class LocalVariableTableVisitor extends MethodVisitor {
195194
*/
196195
private final int[] lvtSlotIndex;
197196

198-
public LocalVariableTableVisitor(Class<?> clazz, Map<Member, String[]> map, String name, String desc, boolean isStatic) {
197+
public LocalVariableTableVisitor(Class<?> clazz, Map<Executable, String[]> map, String name, String desc, boolean isStatic) {
199198
super(SpringAsmInfo.ASM_VERSION);
200199
this.clazz = clazz;
201-
this.memberMap = map;
200+
this.executableMap = map;
202201
this.name = name;
203202
this.args = Type.getArgumentTypes(desc);
204203
this.parameterNames = new String[this.args.length];
@@ -223,11 +222,11 @@ public void visitEnd() {
223222
// which doesn't use any local variables.
224223
// This means that hasLvtInfo could be false for that kind of methods
225224
// even if the class has local variable info.
226-
this.memberMap.put(resolveMember(), this.parameterNames);
225+
this.executableMap.put(resolveExecutable(), this.parameterNames);
227226
}
228227
}
229228

230-
private Member resolveMember() {
229+
private Executable resolveExecutable() {
231230
ClassLoader loader = this.clazz.getClassLoader();
232231
Class<?>[] argTypes = new Class<?>[this.args.length];
233232
for (int i = 0; i < this.args.length; i++) {

0 commit comments

Comments
 (0)