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