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 public void readCode() throws IOException
158 {
159 readShort();
160 int maxLocals = readShort();
161
162 MethodInfo info = new MethodInfo(maxLocals);
163 if (methods != null && methodName != null)
164 {
165 methods.put(methodName, info);
166 }
167
168 skipFully(readInt());
169 skipFully(8 * readShort());
170
171
172 readAttributes();
173 }
174
175
176
177
178
179
180
181
182
183
184 public String[] getParameterNames(Constructor ctor)
185 {
186 paramTypes = ctor.getParameterTypes();
187 return getParameterNames(ctor, paramTypes);
188 }
189
190
191
192
193
194
195
196
197
198
199 public String[] getParameterNames(Method method)
200 {
201 paramTypes = method.getParameterTypes();
202 return getParameterNames(method, paramTypes);
203 }
204
205 protected String[] getParameterNames(Member member, Class[] pTypes)
206 {
207
208 MethodInfo info = (MethodInfo) methods.get(getSignature(member, pTypes));
209
210
211
212
213 if (info != null)
214 {
215 String[] paramNames = new String[pTypes.length];
216 int j = Modifier.isStatic(member.getModifiers()) ? 0 : 1;
217
218 boolean found = false;
219 for (int i = 0; i < paramNames.length; i++)
220 {
221 if (info.names[j] != null)
222 {
223 found = true;
224 paramNames[i] = info.names[j];
225 }
226 j++;
227 if (pTypes[i] == double.class || pTypes[i] == long.class)
228 {
229
230 j++;
231 }
232 }
233
234 if (found)
235 {
236 return paramNames;
237 }
238 else
239 {
240 return null;
241 }
242 }
243 else
244 {
245 return null;
246 }
247 }
248
249 private static class MethodInfo
250 {
251 String[] names;
252
253 public MethodInfo(int maxLocals)
254 {
255 names = new String[maxLocals];
256 }
257 }
258
259 private MethodInfo getMethodInfo()
260 {
261 MethodInfo info = null;
262 if (methods != null && methodName != null)
263 {
264 info = (MethodInfo) methods.get(methodName);
265 }
266 return info;
267 }
268
269
270
271
272
273
274 public void readLocalVariableTable() throws IOException
275 {
276 int len = readShort();
277 MethodInfo info = getMethodInfo();
278 for (int j = 0; j < len; j++)
279 {
280 readShort();
281 readShort();
282 int nameIndex = readShort();
283 readShort();
284 int index = readShort();
285 if (info != null)
286 {
287 info.names[index] = resolveUtf8(nameIndex);
288 }
289 }
290 }
291 }