1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.util; |
12 | |
|
13 | |
import java.io.BufferedReader; |
14 | |
import java.io.CharArrayReader; |
15 | |
import java.io.IOException; |
16 | |
import java.io.Reader; |
17 | |
import java.lang.reflect.Constructor; |
18 | |
import java.lang.reflect.InvocationTargetException; |
19 | |
import java.lang.reflect.Method; |
20 | |
import java.lang.reflect.Modifier; |
21 | |
import java.net.URL; |
22 | |
import java.security.AccessController; |
23 | |
import java.security.PrivilegedAction; |
24 | |
import java.util.ArrayList; |
25 | |
import java.util.Collections; |
26 | |
import java.util.Enumeration; |
27 | |
import java.util.HashMap; |
28 | |
import java.util.List; |
29 | |
import java.util.Map; |
30 | |
import java.util.Set; |
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | 0 | public class ClassUtils extends org.apache.commons.lang.ClassUtils |
41 | |
{ |
42 | 0 | public static final Object[] NO_ARGS = new Object[]{}; |
43 | |
|
44 | 0 | private static final Map wrapperToPrimitiveMap = new HashMap(); |
45 | |
static |
46 | |
{ |
47 | 0 | wrapperToPrimitiveMap.put(Boolean.class, Boolean.TYPE); |
48 | 0 | wrapperToPrimitiveMap.put(Byte.class, Byte.TYPE); |
49 | 0 | wrapperToPrimitiveMap.put(Character.class, Character.TYPE); |
50 | 0 | wrapperToPrimitiveMap.put(Short.class, Short.TYPE); |
51 | 0 | wrapperToPrimitiveMap.put(Integer.class, Integer.TYPE); |
52 | 0 | wrapperToPrimitiveMap.put(Long.class, Long.TYPE); |
53 | 0 | wrapperToPrimitiveMap.put(Double.class, Double.TYPE); |
54 | 0 | wrapperToPrimitiveMap.put(Float.class, Float.TYPE); |
55 | 0 | wrapperToPrimitiveMap.put(Void.TYPE, Void.TYPE); |
56 | 0 | } |
57 | |
|
58 | |
public static boolean isConcrete(Class clazz) |
59 | |
{ |
60 | 0 | if (clazz == null) |
61 | |
{ |
62 | 0 | throw new IllegalArgumentException("clazz may not be null"); |
63 | |
} |
64 | 0 | return !(clazz.isInterface() || Modifier.isAbstract(clazz.getModifiers())); |
65 | |
} |
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
public static URL getResource(final String resourceName, final Class callingClass) |
82 | |
{ |
83 | 0 | URL url = (URL) AccessController.doPrivileged(new PrivilegedAction() |
84 | |
{ |
85 | 0 | public Object run() |
86 | |
{ |
87 | 0 | return Thread.currentThread().getContextClassLoader().getResource(resourceName); |
88 | |
} |
89 | |
}); |
90 | |
|
91 | 0 | if (url == null) |
92 | |
{ |
93 | 0 | url = (URL) AccessController.doPrivileged(new PrivilegedAction() |
94 | |
{ |
95 | 0 | public Object run() |
96 | |
{ |
97 | 0 | return ClassUtils.class.getClassLoader().getResource(resourceName); |
98 | |
} |
99 | |
}); |
100 | |
} |
101 | |
|
102 | 0 | if (url == null) |
103 | |
{ |
104 | 0 | url = (URL) AccessController.doPrivileged(new PrivilegedAction() |
105 | |
{ |
106 | 0 | public Object run() |
107 | |
{ |
108 | 0 | return callingClass.getClassLoader().getResource(resourceName); |
109 | |
} |
110 | |
}); |
111 | |
} |
112 | |
|
113 | 0 | return url; |
114 | |
} |
115 | |
|
116 | |
public static Enumeration getResources(final String resourceName, final Class callingClass) |
117 | |
{ |
118 | 0 | Enumeration enumeration = (Enumeration) AccessController.doPrivileged(new PrivilegedAction() |
119 | |
{ |
120 | 0 | public Object run() |
121 | |
{ |
122 | |
try |
123 | |
{ |
124 | 0 | return Thread.currentThread().getContextClassLoader().getResources(resourceName); |
125 | |
} |
126 | 0 | catch (IOException e) |
127 | |
{ |
128 | 0 | return null; |
129 | |
} |
130 | |
} |
131 | |
}); |
132 | |
|
133 | 0 | if (enumeration == null) |
134 | |
{ |
135 | 0 | enumeration = (Enumeration) AccessController.doPrivileged(new PrivilegedAction() |
136 | |
{ |
137 | 0 | public Object run() |
138 | |
{ |
139 | |
try |
140 | |
{ |
141 | 0 | return ClassUtils.class.getClassLoader().getResources(resourceName); |
142 | |
} |
143 | 0 | catch (IOException e) |
144 | |
{ |
145 | 0 | return null; |
146 | |
} |
147 | |
} |
148 | |
}); |
149 | |
} |
150 | |
|
151 | 0 | if (enumeration == null) |
152 | |
{ |
153 | 0 | enumeration = (Enumeration) AccessController.doPrivileged(new PrivilegedAction() |
154 | |
{ |
155 | 0 | public Object run() |
156 | |
{ |
157 | |
try |
158 | |
{ |
159 | 0 | return callingClass.getClassLoader().getResources(resourceName); |
160 | |
} |
161 | 0 | catch (IOException e) |
162 | |
{ |
163 | 0 | return null; |
164 | |
} |
165 | |
} |
166 | |
}); |
167 | |
} |
168 | |
|
169 | 0 | return enumeration; |
170 | |
} |
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
public static Class loadClass(final String className, final Class callingClass) |
189 | |
throws ClassNotFoundException |
190 | |
{ |
191 | 0 | Class clazz = (Class) AccessController.doPrivileged(new PrivilegedAction() |
192 | |
{ |
193 | 0 | public Object run() |
194 | |
{ |
195 | |
try |
196 | |
{ |
197 | 0 | return Thread.currentThread().getContextClassLoader().loadClass(className); |
198 | |
} |
199 | 0 | catch (ClassNotFoundException e) |
200 | |
{ |
201 | 0 | return null; |
202 | |
} |
203 | |
} |
204 | |
}); |
205 | |
|
206 | 0 | if (clazz == null) |
207 | |
{ |
208 | 0 | clazz = (Class) AccessController.doPrivileged(new PrivilegedAction() |
209 | |
{ |
210 | 0 | public Object run() |
211 | |
{ |
212 | |
try |
213 | |
{ |
214 | 0 | return Class.forName(className); |
215 | |
} |
216 | 0 | catch (ClassNotFoundException e) |
217 | |
{ |
218 | 0 | return null; |
219 | |
} |
220 | |
} |
221 | |
}); |
222 | |
} |
223 | |
|
224 | 0 | if (clazz == null) |
225 | |
{ |
226 | 0 | clazz = (Class) AccessController.doPrivileged(new PrivilegedAction() |
227 | |
{ |
228 | 0 | public Object run() |
229 | |
{ |
230 | |
try |
231 | |
{ |
232 | 0 | return ClassUtils.class.getClassLoader().loadClass(className); |
233 | |
} |
234 | 0 | catch (ClassNotFoundException e) |
235 | |
{ |
236 | 0 | return null; |
237 | |
} |
238 | |
} |
239 | |
}); |
240 | |
} |
241 | |
|
242 | 0 | if (clazz == null) |
243 | |
{ |
244 | 0 | clazz = (Class) AccessController.doPrivileged(new PrivilegedAction() |
245 | |
{ |
246 | 0 | public Object run() |
247 | |
{ |
248 | |
try |
249 | |
{ |
250 | 0 | return callingClass.getClassLoader().loadClass(className); |
251 | |
} |
252 | 0 | catch (ClassNotFoundException e) |
253 | |
{ |
254 | 0 | return null; |
255 | |
} |
256 | |
} |
257 | |
}); |
258 | |
} |
259 | |
|
260 | 0 | if (clazz == null) |
261 | |
{ |
262 | 0 | throw new ClassNotFoundException(className); |
263 | |
} |
264 | |
|
265 | 0 | return clazz; |
266 | |
} |
267 | |
|
268 | |
|
269 | |
|
270 | |
|
271 | |
public static void printClassLoader() |
272 | |
{ |
273 | 0 | System.out.println("ClassLoaderUtils.printClassLoader"); |
274 | 0 | printClassLoader(Thread.currentThread().getContextClassLoader()); |
275 | 0 | } |
276 | |
|
277 | |
|
278 | |
|
279 | |
|
280 | |
|
281 | |
public static void printClassLoader(ClassLoader cl) |
282 | |
{ |
283 | 0 | System.out.println("ClassLoaderUtils.printClassLoader(cl = " + cl + ")"); |
284 | |
|
285 | 0 | if (cl != null) |
286 | |
{ |
287 | 0 | printClassLoader(cl.getParent()); |
288 | |
} |
289 | 0 | } |
290 | |
|
291 | |
public static Object instanciateClass(Class clazz, Object[] constructorArgs) |
292 | |
throws SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, |
293 | |
IllegalAccessException, InvocationTargetException |
294 | |
{ |
295 | |
Class[] args; |
296 | 0 | if (constructorArgs != null) |
297 | |
{ |
298 | 0 | args = new Class[constructorArgs.length]; |
299 | 0 | for (int i = 0; i < constructorArgs.length; i++) |
300 | |
{ |
301 | 0 | if (constructorArgs[i] == null) |
302 | |
{ |
303 | 0 | args[i] = null; |
304 | |
} |
305 | |
else |
306 | |
{ |
307 | 0 | args[i] = constructorArgs[i].getClass(); |
308 | |
} |
309 | |
} |
310 | |
} |
311 | |
else |
312 | |
{ |
313 | 0 | args = new Class[0]; |
314 | |
} |
315 | |
|
316 | |
|
317 | 0 | Constructor ctor = getConstructor(clazz, args); |
318 | |
|
319 | 0 | if (ctor == null) |
320 | |
{ |
321 | |
|
322 | 0 | ctor = getConstructor(clazz, wrappersToPrimitives(args)); |
323 | |
} |
324 | |
|
325 | 0 | if (ctor == null) |
326 | |
{ |
327 | 0 | StringBuffer argsString = new StringBuffer(100); |
328 | 0 | for (int i = 0; i < args.length; i++) |
329 | |
{ |
330 | 0 | argsString.append(args[i].getName()).append(", "); |
331 | |
} |
332 | 0 | throw new NoSuchMethodException("could not find constructor with matching arg params: " |
333 | |
+ argsString); |
334 | |
} |
335 | |
|
336 | 0 | return ctor.newInstance(constructorArgs); |
337 | |
} |
338 | |
|
339 | |
public static Object instanciateClass(String name, Object[] constructorArgs) |
340 | |
throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, |
341 | |
InstantiationException, IllegalAccessException, InvocationTargetException |
342 | |
{ |
343 | 0 | Class clazz = loadClass(name, ClassUtils.class); |
344 | 0 | return instanciateClass(clazz, constructorArgs); |
345 | |
|
346 | |
} |
347 | |
|
348 | |
public static Object instanciateClass(String name, Object[] constructorArgs, Class callingClass) |
349 | |
throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, |
350 | |
InstantiationException, IllegalAccessException, InvocationTargetException |
351 | |
{ |
352 | 0 | Class clazz = loadClass(name, callingClass); |
353 | 0 | return instanciateClass(clazz, constructorArgs); |
354 | |
} |
355 | |
|
356 | |
public static Class[] getParameterTypes(Object bean, String methodName) |
357 | |
{ |
358 | 0 | if (!methodName.startsWith("set")) |
359 | |
{ |
360 | 0 | methodName = "set" + methodName.substring(0, 1).toUpperCase() + methodName.substring(1); |
361 | |
} |
362 | |
|
363 | 0 | Method methods[] = bean.getClass().getMethods(); |
364 | |
|
365 | 0 | for (int i = 0; i < methods.length; i++) |
366 | |
{ |
367 | 0 | if (methods[i].getName().equals(methodName)) |
368 | |
{ |
369 | 0 | return methods[i].getParameterTypes(); |
370 | |
} |
371 | |
} |
372 | |
|
373 | 0 | return new Class[]{}; |
374 | |
} |
375 | |
|
376 | |
|
377 | |
|
378 | |
|
379 | |
|
380 | |
|
381 | |
|
382 | |
|
383 | |
|
384 | |
|
385 | |
|
386 | |
public static Method getMethod(Class clazz, String name, Class[] parameterTypes) |
387 | |
{ |
388 | 0 | Method[] methods = clazz.getMethods(); |
389 | 0 | for (int i = 0; i < methods.length; i++) |
390 | |
{ |
391 | 0 | if (methods[i].getName().equals(name)) |
392 | |
{ |
393 | 0 | if (parameterTypes == null) |
394 | |
{ |
395 | 0 | return methods[i]; |
396 | |
} |
397 | 0 | else if (compare(methods[i].getParameterTypes(), parameterTypes, true)) |
398 | |
{ |
399 | 0 | return methods[i]; |
400 | |
} |
401 | |
} |
402 | |
} |
403 | 0 | return null; |
404 | |
} |
405 | |
|
406 | |
public static Constructor getConstructor(Class clazz, Class[] paramTypes) |
407 | |
{ |
408 | 0 | Constructor[] ctors = clazz.getConstructors(); |
409 | 0 | for (int i = 0; i < ctors.length; i++) |
410 | |
{ |
411 | 0 | Class[] types = ctors[i].getParameterTypes(); |
412 | 0 | if (types.length == paramTypes.length) |
413 | |
{ |
414 | 0 | boolean match = true; |
415 | 0 | for (int x = 0; x < types.length; x++) |
416 | |
{ |
417 | 0 | if (paramTypes[x] == null) |
418 | |
{ |
419 | 0 | match = true; |
420 | |
} |
421 | |
else |
422 | |
{ |
423 | 0 | match = types[x].isAssignableFrom(paramTypes[x]); |
424 | |
} |
425 | |
} |
426 | 0 | if (match) |
427 | |
{ |
428 | 0 | return ctors[i]; |
429 | |
} |
430 | |
} |
431 | |
} |
432 | 0 | return null; |
433 | |
} |
434 | |
|
435 | |
|
436 | |
|
437 | |
|
438 | |
|
439 | |
|
440 | |
|
441 | |
|
442 | |
|
443 | |
|
444 | |
|
445 | |
|
446 | |
|
447 | |
|
448 | |
|
449 | |
public static List getSatisfiableMethods(Class implementation, |
450 | |
Class[] parameterTypes, |
451 | |
boolean voidOk, |
452 | |
boolean matchOnObject, |
453 | |
Set ignoredMethodNames) |
454 | |
{ |
455 | 0 | List result = new ArrayList(); |
456 | |
|
457 | 0 | if (ignoredMethodNames == null) |
458 | |
{ |
459 | 0 | ignoredMethodNames = Collections.EMPTY_SET; |
460 | |
} |
461 | |
|
462 | 0 | Method[] methods = implementation.getMethods(); |
463 | 0 | for (int i = 0; i < methods.length; i++) |
464 | |
{ |
465 | 0 | Method method = methods[i]; |
466 | 0 | Class[] methodParams = method.getParameterTypes(); |
467 | |
|
468 | 0 | if (compare(methodParams, parameterTypes, matchOnObject)) |
469 | |
{ |
470 | 0 | if (!ignoredMethodNames.contains(method.getName())) |
471 | |
{ |
472 | 0 | String returnType = method.getReturnType().getName(); |
473 | 0 | if ((returnType.equals("void") && voidOk) || !returnType.equals("void")) |
474 | |
{ |
475 | 0 | result.add(method); |
476 | |
} |
477 | |
} |
478 | |
} |
479 | |
} |
480 | |
|
481 | 0 | return result; |
482 | |
} |
483 | |
|
484 | |
public static List getSatisfiableMethodsWithReturnType(Class implementation, |
485 | |
Class returnType, |
486 | |
boolean matchOnObject, |
487 | |
Set ignoredMethodNames) |
488 | |
{ |
489 | 0 | List result = new ArrayList(); |
490 | |
|
491 | 0 | if (ignoredMethodNames == null) |
492 | |
{ |
493 | 0 | ignoredMethodNames = Collections.EMPTY_SET; |
494 | |
} |
495 | |
|
496 | 0 | Method[] methods = implementation.getMethods(); |
497 | 0 | for (int i = 0; i < methods.length; i++) |
498 | |
{ |
499 | 0 | Method method = methods[i]; |
500 | 0 | Class returns = method.getReturnType(); |
501 | |
|
502 | 0 | if (compare(new Class[]{returns}, new Class[]{returnType}, matchOnObject)) |
503 | |
{ |
504 | 0 | if (!ignoredMethodNames.contains(method.getName())) |
505 | |
{ |
506 | 0 | result.add(method); |
507 | |
} |
508 | |
} |
509 | |
} |
510 | |
|
511 | 0 | return result; |
512 | |
} |
513 | |
|
514 | |
|
515 | |
|
516 | |
|
517 | |
|
518 | |
|
519 | |
|
520 | |
|
521 | |
|
522 | |
public static boolean isClassOnPath(String className, Class currentClass) |
523 | |
{ |
524 | |
try |
525 | |
{ |
526 | 0 | return (loadClass(className, currentClass) != null); |
527 | |
} |
528 | 0 | catch (ClassNotFoundException e) |
529 | |
{ |
530 | 0 | return false; |
531 | |
} |
532 | |
} |
533 | |
|
534 | |
|
535 | |
|
536 | |
|
537 | |
|
538 | |
|
539 | |
|
540 | |
public static Class[] getClassTypes(Object object) |
541 | |
{ |
542 | |
Class[] types; |
543 | |
|
544 | |
|
545 | |
|
546 | |
|
547 | |
|
548 | |
|
549 | |
|
550 | |
|
551 | 0 | if (object instanceof Object[]) |
552 | |
{ |
553 | 0 | Object[] objects = (Object[]) object; |
554 | 0 | types = new Class[objects.length]; |
555 | 0 | for (int i = 0; i < objects.length; i++) |
556 | |
{ |
557 | 0 | types[i] = objects[i].getClass(); |
558 | |
} |
559 | |
} |
560 | |
else |
561 | |
{ |
562 | 0 | types = new Class[]{object.getClass()}; |
563 | |
} |
564 | |
|
565 | 0 | return types; |
566 | |
} |
567 | |
|
568 | |
public static String getClassName(Class clazz) |
569 | |
{ |
570 | 0 | if (clazz == null) |
571 | |
{ |
572 | 0 | return null; |
573 | |
} |
574 | 0 | String name = clazz.getName(); |
575 | 0 | return name.substring(name.lastIndexOf(".") + 1); |
576 | |
} |
577 | |
|
578 | |
public static boolean compare(Class[] c1, Class[] c2, boolean matchOnObject) |
579 | |
{ |
580 | 0 | if (c1.length != c2.length) |
581 | |
{ |
582 | 0 | return false; |
583 | |
} |
584 | 0 | for (int i = 0; i < c1.length; i++) |
585 | |
{ |
586 | 0 | if (c1[i].equals(Object.class) && !matchOnObject) |
587 | |
{ |
588 | 0 | return false; |
589 | |
} |
590 | 0 | if (!c1[i].isAssignableFrom(c2[i])) |
591 | |
{ |
592 | |
|
593 | 0 | return false; |
594 | |
} |
595 | |
} |
596 | 0 | return true; |
597 | |
} |
598 | |
|
599 | |
public static Class wrapperToPrimitive(Class wrapper) |
600 | |
{ |
601 | 0 | return (Class) MapUtils.getObject(wrapperToPrimitiveMap, wrapper, wrapper); |
602 | |
} |
603 | |
|
604 | |
public static Class[] wrappersToPrimitives(Class[] wrappers) |
605 | |
{ |
606 | 0 | if (wrappers == null) |
607 | |
{ |
608 | 0 | return null; |
609 | |
} |
610 | |
|
611 | 0 | if (wrappers.length == 0) |
612 | |
{ |
613 | 0 | return wrappers; |
614 | |
} |
615 | |
|
616 | 0 | Class[] primitives = new Class[wrappers.length]; |
617 | |
|
618 | 0 | for (int i = 0; i < wrappers.length; i++) |
619 | |
{ |
620 | 0 | primitives[i] = (Class) MapUtils.getObject(wrapperToPrimitiveMap, wrappers[i], wrappers[i]); |
621 | |
} |
622 | |
|
623 | 0 | return primitives; |
624 | |
} |
625 | |
|
626 | |
|
627 | |
|
628 | |
|
629 | |
|
630 | |
|
631 | |
public static String getSimpleName(Class clazz) |
632 | |
{ |
633 | 0 | if (null == clazz) |
634 | |
{ |
635 | 0 | return "null"; |
636 | |
} |
637 | |
else |
638 | |
{ |
639 | 0 | return classNameHelper(new BufferedReader(new CharArrayReader(clazz.getName().toCharArray()))); |
640 | |
} |
641 | |
} |
642 | |
|
643 | |
private static String classNameHelper(Reader encodedName) |
644 | |
{ |
645 | |
|
646 | |
|
647 | |
|
648 | |
|
649 | |
try |
650 | |
{ |
651 | 0 | encodedName.mark(1); |
652 | 0 | switch(encodedName.read()) |
653 | |
{ |
654 | 0 | case -1: return "null"; |
655 | 0 | case 'Z': return "boolean"; |
656 | 0 | case 'B': return "byte"; |
657 | 0 | case 'C': return "char"; |
658 | 0 | case 'D': return "double"; |
659 | 0 | case 'F': return "float"; |
660 | 0 | case 'I': return "int"; |
661 | 0 | case 'J': return "long"; |
662 | 0 | case 'S': return "short"; |
663 | 0 | case '[': return classNameHelper(encodedName) + "[]"; |
664 | 0 | case 'L': return shorten(new BufferedReader(encodedName).readLine()); |
665 | |
default: |
666 | 0 | encodedName.reset(); |
667 | 0 | return shorten(new BufferedReader(encodedName).readLine()); |
668 | |
} |
669 | |
} |
670 | 0 | catch (IOException e) |
671 | |
{ |
672 | 0 | return "unknown type: " + e.getMessage(); |
673 | |
} |
674 | |
} |
675 | |
|
676 | |
|
677 | |
|
678 | |
|
679 | |
|
680 | |
private static String shorten(String clazz) |
681 | |
{ |
682 | 0 | if (null != clazz && clazz.endsWith(";")) |
683 | |
{ |
684 | 0 | clazz = clazz.substring(0, clazz.length() - 1); |
685 | |
} |
686 | 0 | if (null != clazz && clazz.lastIndexOf(".") > -1) |
687 | |
{ |
688 | 0 | clazz = clazz.substring(clazz.lastIndexOf(".") + 1, clazz.length()); |
689 | |
} |
690 | 0 | return clazz; |
691 | |
} |
692 | |
} |