1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.model.interpolation.reflection;
20
21 import java.lang.reflect.Method;
22 import java.lang.reflect.Modifier;
23 import java.util.Hashtable;
24 import java.util.Map;
25
26
27
28
29
30
31 class ClassMap {
32 private static final class CacheMiss {}
33
34 private static final CacheMiss CACHE_MISS = new CacheMiss();
35
36 private static final Object OBJECT = new Object();
37
38
39
40
41
42 private final Class<?> clazz;
43
44
45
46
47
48 private final Map<String, Object> methodCache = new Hashtable<>();
49
50 private MethodMap methodMap = new MethodMap();
51
52
53
54
55
56 ClassMap(Class<?> clazz) {
57 this.clazz = clazz;
58 populateMethodCache();
59 }
60
61
62
63
64 Class<?> getCachedClass() {
65 return clazz;
66 }
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81 public Method findMethod(String name, Object... params) throws MethodMap.AmbiguousException {
82 String methodKey = makeMethodKey(name, params);
83 Object cacheEntry = methodCache.get(methodKey);
84
85 if (cacheEntry == CACHE_MISS) {
86 return null;
87 }
88
89 if (cacheEntry == null) {
90 try {
91 cacheEntry = methodMap.find(name, params);
92 } catch (MethodMap.AmbiguousException ae) {
93
94 methodCache.put(methodKey, CACHE_MISS);
95 throw ae;
96 }
97
98 if (cacheEntry == null) {
99 methodCache.put(methodKey, CACHE_MISS);
100 } else {
101 methodCache.put(methodKey, cacheEntry);
102 }
103 }
104
105
106 return (Method) cacheEntry;
107 }
108
109
110
111
112
113
114 private void populateMethodCache() {
115
116 Method[] methods = getAccessibleMethods(clazz);
117
118
119 for (Method method : methods) {
120
121
122
123
124 Method publicMethod = getPublicMethod(method);
125
126
127
128
129
130 if (publicMethod != null) {
131 methodMap.add(publicMethod);
132 methodCache.put(makeMethodKey(publicMethod), publicMethod);
133 }
134 }
135 }
136
137
138
139
140
141
142 private String makeMethodKey(Method method) {
143 Class<?>[] parameterTypes = method.getParameterTypes();
144
145 StringBuilder methodKey = new StringBuilder(method.getName());
146
147 for (Class<?> parameterType : parameterTypes) {
148
149
150
151
152 if (parameterType.isPrimitive()) {
153 if (parameterType.equals(Boolean.TYPE)) {
154 methodKey.append("java.lang.Boolean");
155 } else if (parameterType.equals(Byte.TYPE)) {
156 methodKey.append("java.lang.Byte");
157 } else if (parameterType.equals(Character.TYPE)) {
158 methodKey.append("java.lang.Character");
159 } else if (parameterType.equals(Double.TYPE)) {
160 methodKey.append("java.lang.Double");
161 } else if (parameterType.equals(Float.TYPE)) {
162 methodKey.append("java.lang.Float");
163 } else if (parameterType.equals(Integer.TYPE)) {
164 methodKey.append("java.lang.Integer");
165 } else if (parameterType.equals(Long.TYPE)) {
166 methodKey.append("java.lang.Long");
167 } else if (parameterType.equals(Short.TYPE)) {
168 methodKey.append("java.lang.Short");
169 }
170 } else {
171 methodKey.append(parameterType.getName());
172 }
173 }
174
175 return methodKey.toString();
176 }
177
178 private static String makeMethodKey(String method, Object... params) {
179 StringBuilder methodKey = new StringBuilder().append(method);
180
181 for (Object param : params) {
182 Object arg = param;
183
184 if (arg == null) {
185 arg = OBJECT;
186 }
187
188 methodKey.append(arg.getClass().getName());
189 }
190
191 return methodKey.toString();
192 }
193
194
195
196
197
198
199
200 private static Method[] getAccessibleMethods(Class<?> clazz) {
201 Method[] methods = clazz.getMethods();
202
203
204
205 if (Modifier.isPublic(clazz.getModifiers())) {
206 return methods;
207 }
208
209
210 MethodInfo[] methodInfos = new MethodInfo[methods.length];
211 for (int i = methods.length; i-- > 0; ) {
212 methodInfos[i] = new MethodInfo(methods[i]);
213 }
214
215 int upcastCount = getAccessibleMethods(clazz, methodInfos, 0);
216
217
218 if (upcastCount < methods.length) {
219 methods = new Method[upcastCount];
220 }
221
222 int j = 0;
223 for (MethodInfo methodInfo : methodInfos) {
224 if (methodInfo.upcast) {
225 methods[j++] = methodInfo.method;
226 }
227 }
228 return methods;
229 }
230
231
232
233
234
235
236
237
238
239
240 private static int getAccessibleMethods(Class<?> clazz, MethodInfo[] methodInfos, int upcastCount) {
241 int l = methodInfos.length;
242
243
244
245 if (Modifier.isPublic(clazz.getModifiers())) {
246 for (int i = 0; i < l && upcastCount < l; ++i) {
247 try {
248 MethodInfo methodInfo = methodInfos[i];
249 if (!methodInfo.upcast) {
250 methodInfo.tryUpcasting(clazz);
251 upcastCount++;
252 }
253 } catch (NoSuchMethodException e) {
254
255 }
256 }
257
258
259
260
261
262 if (upcastCount == l) {
263 return upcastCount;
264 }
265 }
266
267
268 Class<?> superclazz = clazz.getSuperclass();
269 if (superclazz != null) {
270 upcastCount = getAccessibleMethods(superclazz, methodInfos, upcastCount);
271
272
273 if (upcastCount == l) {
274 return upcastCount;
275 }
276 }
277
278
279
280
281 Class<?>[] interfaces = clazz.getInterfaces();
282 for (int i = interfaces.length; i-- > 0; ) {
283 upcastCount = getAccessibleMethods(interfaces[i], methodInfos, upcastCount);
284
285
286 if (upcastCount == l) {
287 return upcastCount;
288 }
289 }
290
291 return upcastCount;
292 }
293
294
295
296
297
298
299
300
301
302
303
304
305 private static Method getPublicMethod(Method method) {
306 Class<?> clazz = method.getDeclaringClass();
307
308
309
310 if ((clazz.getModifiers() & Modifier.PUBLIC) != 0) {
311 return method;
312 }
313
314 return getPublicMethod(clazz, method.getName(), method.getParameterTypes());
315 }
316
317
318
319
320
321
322
323
324
325 private static Method getPublicMethod(Class<?> clazz, String name, Class<?>... paramTypes) {
326
327 if ((clazz.getModifiers() & Modifier.PUBLIC) != 0) {
328 try {
329 return clazz.getMethod(name, paramTypes);
330 } catch (NoSuchMethodException e) {
331
332
333 return null;
334 }
335 }
336
337
338 Class<?> superclazz = clazz.getSuperclass();
339
340 if (superclazz != null) {
341 Method superclazzMethod = getPublicMethod(superclazz, name, paramTypes);
342
343 if (superclazzMethod != null) {
344 return superclazzMethod;
345 }
346 }
347
348
349 Class<?>[] interfaces = clazz.getInterfaces();
350
351 for (Class<?> anInterface : interfaces) {
352 Method interfaceMethod = getPublicMethod(anInterface, name, paramTypes);
353
354 if (interfaceMethod != null) {
355 return interfaceMethod;
356 }
357 }
358
359 return null;
360 }
361
362
363
364
365 private static final class MethodInfo {
366 Method method;
367
368 String name;
369
370 Class<?>[] parameterTypes;
371
372 boolean upcast;
373
374 MethodInfo(Method method) {
375 this.method = null;
376 name = method.getName();
377 parameterTypes = method.getParameterTypes();
378 upcast = false;
379 }
380
381 void tryUpcasting(Class<?> clazz) throws NoSuchMethodException {
382 method = clazz.getMethod(name, parameterTypes);
383 name = null;
384 parameterTypes = null;
385 upcast = true;
386 }
387 }
388 }