1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.mule.config.spring.util;
21
22
23
24 import java.io.IOException;
25 import java.lang.reflect.Constructor;
26 import java.lang.reflect.Member;
27 import java.lang.reflect.Method;
28 import java.lang.reflect.Modifier;
29 import java.lang.reflect.Proxy;
30 import java.util.HashMap;
31 import java.util.Map;
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 public class ParamReader extends ClassReader
49 {
50 private String methodName;
51 private Map<String, MethodInfo> methods = new HashMap<String, MethodInfo>();
52 private Class[] paramTypes;
53
54
55
56
57
58
59
60
61 public ParamReader(Class c) throws IOException
62 {
63 this(getBytes(c));
64 }
65
66
67
68
69
70
71
72 public ParamReader(byte[] b) throws IOException
73 {
74 super(b, findAttributeReaders(ParamReader.class));
75
76
77 if (readInt() != 0xCAFEBABE)
78 {
79
80 throw new IOException();
81 }
82
83 readShort();
84 readShort();
85
86 readCpool();
87
88 readShort();
89 readShort();
90 readShort();
91
92 int count = readShort();
93 for (int i = 0; i < count; i++)
94 {
95 readShort();
96 }
97
98 count = readShort();
99 for (int i = 0; i < count; i++)
100 {
101 readShort();
102 readShort();
103 readShort();
104 skipAttributes();
105 }
106
107 count = readShort();
108 for (int i = 0; i < count; i++)
109 {
110 readShort();
111 int m = readShort();
112 String name = resolveUtf8(m);
113 int d = readShort();
114 this.methodName = name + resolveUtf8(d);
115 readAttributes();
116 }
117
118 }
119
120
121
122
123
124 public static String[] getParameterNamesFromDebugInfo(Method method)
125 {
126
127 int numParams = method.getParameterTypes().length;
128 if (numParams == 0)
129 {
130 return null;
131 }
132
133
134 Class c = method.getDeclaringClass();
135
136
137 if (Proxy.isProxyClass(c))
138 {
139 return null;
140 }
141
142 try
143 {
144
145 ParamReader pr = new ParamReader(c);
146
147 return pr.getParameterNames(method);
148 }
149 catch (IOException e)
150 {
151
152
153 return null;
154 }
155 }
156
157 @Override
158 public void readCode() throws IOException
159 {
160 readShort();
161 int maxLocals = readShort();
162
163 MethodInfo info = new MethodInfo(maxLocals);
164 if (methods != null && methodName != null)
165 {
166 methods.put(methodName, info);
167 }
168
169 skipFully(readInt());
170 skipFully(8 * readShort());
171
172
173 readAttributes();
174 }
175
176
177
178
179
180
181
182
183
184
185 public String[] getParameterNames(Constructor ctor)
186 {
187 paramTypes = ctor.getParameterTypes();
188 return getParameterNames(ctor, paramTypes);
189 }
190
191
192
193
194
195
196
197
198
199
200 public String[] getParameterNames(Method method)
201 {
202 paramTypes = method.getParameterTypes();
203 return getParameterNames(method, paramTypes);
204 }
205
206 protected String[] getParameterNames(Member member, Class[] pTypes)
207 {
208
209 MethodInfo info = methods.get(getSignature(member, pTypes));
210
211
212
213
214 if (info != null)
215 {
216 String[] paramNames = new String[pTypes.length];
217 int j = Modifier.isStatic(member.getModifiers()) ? 0 : 1;
218
219 boolean found = false;
220 for (int i = 0; i < paramNames.length; i++)
221 {
222 if (info.names[j] != null)
223 {
224 found = true;
225 paramNames[i] = info.names[j];
226 }
227 j++;
228 if (pTypes[i] == double.class || pTypes[i] == long.class)
229 {
230
231 j++;
232 }
233 }
234
235 if (found)
236 {
237 return paramNames;
238 }
239 else
240 {
241 return null;
242 }
243 }
244 else
245 {
246 return null;
247 }
248 }
249
250 private static class MethodInfo
251 {
252 String[] names;
253
254 public MethodInfo(int maxLocals)
255 {
256 names = new String[maxLocals];
257 }
258 }
259
260 private MethodInfo getMethodInfo()
261 {
262 MethodInfo info = null;
263 if (methods != null && methodName != null)
264 {
265 info = methods.get(methodName);
266 }
267 return info;
268 }
269
270
271
272
273
274
275 public void readLocalVariableTable() throws IOException
276 {
277 int len = readShort();
278 MethodInfo info = getMethodInfo();
279 for (int j = 0; j < len; j++)
280 {
281 readShort();
282 readShort();
283 int nameIndex = readShort();
284 readShort();
285 int index = readShort();
286 if (info != null)
287 {
288 info.names[index] = resolveUtf8(nameIndex);
289 }
290 }
291 }
292 }