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 public class ClassUtils extends org.apache.commons.lang.ClassUtils
41 {
42 public static final Object[] NO_ARGS = new Object[]{};
43
44 private static final Map wrapperToPrimitiveMap = new HashMap();
45 static
46 {
47 wrapperToPrimitiveMap.put(Boolean.class, Boolean.TYPE);
48 wrapperToPrimitiveMap.put(Byte.class, Byte.TYPE);
49 wrapperToPrimitiveMap.put(Character.class, Character.TYPE);
50 wrapperToPrimitiveMap.put(Short.class, Short.TYPE);
51 wrapperToPrimitiveMap.put(Integer.class, Integer.TYPE);
52 wrapperToPrimitiveMap.put(Long.class, Long.TYPE);
53 wrapperToPrimitiveMap.put(Double.class, Double.TYPE);
54 wrapperToPrimitiveMap.put(Float.class, Float.TYPE);
55 wrapperToPrimitiveMap.put(Void.TYPE, Void.TYPE);
56 }
57
58 public static boolean isConcrete(Class clazz)
59 {
60 if (clazz == null)
61 {
62 throw new IllegalArgumentException("clazz may not be null");
63 }
64 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 URL url = (URL) AccessController.doPrivileged(new PrivilegedAction()
84 {
85 public Object run()
86 {
87 return Thread.currentThread().getContextClassLoader().getResource(resourceName);
88 }
89 });
90
91 if (url == null)
92 {
93 url = (URL) AccessController.doPrivileged(new PrivilegedAction()
94 {
95 public Object run()
96 {
97 return ClassUtils.class.getClassLoader().getResource(resourceName);
98 }
99 });
100 }
101
102 if (url == null)
103 {
104 url = (URL) AccessController.doPrivileged(new PrivilegedAction()
105 {
106 public Object run()
107 {
108 return callingClass.getClassLoader().getResource(resourceName);
109 }
110 });
111 }
112
113 return url;
114 }
115
116 public static Enumeration getResources(final String resourceName, final Class callingClass)
117 {
118 Enumeration enumeration = (Enumeration) AccessController.doPrivileged(new PrivilegedAction()
119 {
120 public Object run()
121 {
122 try
123 {
124 return Thread.currentThread().getContextClassLoader().getResources(resourceName);
125 }
126 catch (IOException e)
127 {
128 return null;
129 }
130 }
131 });
132
133 if (enumeration == null)
134 {
135 enumeration = (Enumeration) AccessController.doPrivileged(new PrivilegedAction()
136 {
137 public Object run()
138 {
139 try
140 {
141 return ClassUtils.class.getClassLoader().getResources(resourceName);
142 }
143 catch (IOException e)
144 {
145 return null;
146 }
147 }
148 });
149 }
150
151 if (enumeration == null)
152 {
153 enumeration = (Enumeration) AccessController.doPrivileged(new PrivilegedAction()
154 {
155 public Object run()
156 {
157 try
158 {
159 return callingClass.getClassLoader().getResources(resourceName);
160 }
161 catch (IOException e)
162 {
163 return null;
164 }
165 }
166 });
167 }
168
169 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 Class clazz = (Class) AccessController.doPrivileged(new PrivilegedAction()
192 {
193 public Object run()
194 {
195 try
196 {
197 return Thread.currentThread().getContextClassLoader().loadClass(className);
198 }
199 catch (ClassNotFoundException e)
200 {
201 return null;
202 }
203 }
204 });
205
206 if (clazz == null)
207 {
208 clazz = (Class) AccessController.doPrivileged(new PrivilegedAction()
209 {
210 public Object run()
211 {
212 try
213 {
214 return Class.forName(className);
215 }
216 catch (ClassNotFoundException e)
217 {
218 return null;
219 }
220 }
221 });
222 }
223
224 if (clazz == null)
225 {
226 clazz = (Class) AccessController.doPrivileged(new PrivilegedAction()
227 {
228 public Object run()
229 {
230 try
231 {
232 return ClassUtils.class.getClassLoader().loadClass(className);
233 }
234 catch (ClassNotFoundException e)
235 {
236 return null;
237 }
238 }
239 });
240 }
241
242 if (clazz == null)
243 {
244 clazz = (Class) AccessController.doPrivileged(new PrivilegedAction()
245 {
246 public Object run()
247 {
248 try
249 {
250 return callingClass.getClassLoader().loadClass(className);
251 }
252 catch (ClassNotFoundException e)
253 {
254 return null;
255 }
256 }
257 });
258 }
259
260 if (clazz == null)
261 {
262 throw new ClassNotFoundException(className);
263 }
264
265 return clazz;
266 }
267
268
269
270
271 public static void printClassLoader()
272 {
273 System.out.println("ClassLoaderUtils.printClassLoader");
274 printClassLoader(Thread.currentThread().getContextClassLoader());
275 }
276
277
278
279
280
281 public static void printClassLoader(ClassLoader cl)
282 {
283 System.out.println("ClassLoaderUtils.printClassLoader(cl = " + cl + ")");
284
285 if (cl != null)
286 {
287 printClassLoader(cl.getParent());
288 }
289 }
290
291 public static Object instanciateClass(Class clazz, Object[] constructorArgs)
292 throws SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException,
293 IllegalAccessException, InvocationTargetException
294 {
295 Class[] args;
296 if (constructorArgs != null)
297 {
298 args = new Class[constructorArgs.length];
299 for (int i = 0; i < constructorArgs.length; i++)
300 {
301 if (constructorArgs[i] == null)
302 {
303 args[i] = null;
304 }
305 else
306 {
307 args[i] = constructorArgs[i].getClass();
308 }
309 }
310 }
311 else
312 {
313 args = new Class[0];
314 }
315
316
317 Constructor ctor = getConstructor(clazz, args);
318
319 if (ctor == null)
320 {
321
322 ctor = getConstructor(clazz, wrappersToPrimitives(args));
323 }
324
325 if (ctor == null)
326 {
327 StringBuffer argsString = new StringBuffer(100);
328 for (int i = 0; i < args.length; i++)
329 {
330 argsString.append(args[i].getName()).append(", ");
331 }
332 throw new NoSuchMethodException("could not find constructor with matching arg params: "
333 + argsString);
334 }
335
336 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 Class clazz = loadClass(name, ClassUtils.class);
344 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 Class clazz = loadClass(name, callingClass);
353 return instanciateClass(clazz, constructorArgs);
354 }
355
356 public static Class[] getParameterTypes(Object bean, String methodName)
357 {
358 if (!methodName.startsWith("set"))
359 {
360 methodName = "set" + methodName.substring(0, 1).toUpperCase() + methodName.substring(1);
361 }
362
363 Method methods[] = bean.getClass().getMethods();
364
365 for (int i = 0; i < methods.length; i++)
366 {
367 if (methods[i].getName().equals(methodName))
368 {
369 return methods[i].getParameterTypes();
370 }
371 }
372
373 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 Method[] methods = clazz.getMethods();
389 for (int i = 0; i < methods.length; i++)
390 {
391 if (methods[i].getName().equals(name))
392 {
393 if (parameterTypes == null)
394 {
395 return methods[i];
396 }
397 else if (compare(methods[i].getParameterTypes(), parameterTypes, true))
398 {
399 return methods[i];
400 }
401 }
402 }
403 return null;
404 }
405
406 public static Constructor getConstructor(Class clazz, Class[] paramTypes)
407 {
408 Constructor[] ctors = clazz.getConstructors();
409 for (int i = 0; i < ctors.length; i++)
410 {
411 Class[] types = ctors[i].getParameterTypes();
412 if (types.length == paramTypes.length)
413 {
414 boolean match = true;
415 for (int x = 0; x < types.length; x++)
416 {
417 if (paramTypes[x] == null)
418 {
419 match = true;
420 }
421 else
422 {
423 match = types[x].isAssignableFrom(paramTypes[x]);
424 }
425 }
426 if (match)
427 {
428 return ctors[i];
429 }
430 }
431 }
432 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 List result = new ArrayList();
456
457 if (ignoredMethodNames == null)
458 {
459 ignoredMethodNames = Collections.EMPTY_SET;
460 }
461
462 Method[] methods = implementation.getMethods();
463 for (int i = 0; i < methods.length; i++)
464 {
465 Method method = methods[i];
466 Class[] methodParams = method.getParameterTypes();
467
468 if (compare(methodParams, parameterTypes, matchOnObject))
469 {
470 if (!ignoredMethodNames.contains(method.getName()))
471 {
472 String returnType = method.getReturnType().getName();
473 if ((returnType.equals("void") && voidOk) || !returnType.equals("void"))
474 {
475 result.add(method);
476 }
477 }
478 }
479 }
480
481 return result;
482 }
483
484 public static List getSatisfiableMethodsWithReturnType(Class implementation,
485 Class returnType,
486 boolean matchOnObject,
487 Set ignoredMethodNames)
488 {
489 List result = new ArrayList();
490
491 if (ignoredMethodNames == null)
492 {
493 ignoredMethodNames = Collections.EMPTY_SET;
494 }
495
496 Method[] methods = implementation.getMethods();
497 for (int i = 0; i < methods.length; i++)
498 {
499 Method method = methods[i];
500 Class returns = method.getReturnType();
501
502 if (compare(new Class[]{returns}, new Class[]{returnType}, matchOnObject))
503 {
504 if (!ignoredMethodNames.contains(method.getName()))
505 {
506 result.add(method);
507 }
508 }
509 }
510
511 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 return (loadClass(className, currentClass) != null);
527 }
528 catch (ClassNotFoundException e)
529 {
530 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 if (object instanceof Object[])
552 {
553 Object[] objects = (Object[]) object;
554 types = new Class[objects.length];
555 for (int i = 0; i < objects.length; i++)
556 {
557 types[i] = objects[i].getClass();
558 }
559 }
560 else
561 {
562 types = new Class[]{object.getClass()};
563 }
564
565 return types;
566 }
567
568 public static String getClassName(Class clazz)
569 {
570 if (clazz == null)
571 {
572 return null;
573 }
574 String name = clazz.getName();
575 return name.substring(name.lastIndexOf(".") + 1);
576 }
577
578 public static boolean compare(Class[] c1, Class[] c2, boolean matchOnObject)
579 {
580 if (c1.length != c2.length)
581 {
582 return false;
583 }
584 for (int i = 0; i < c1.length; i++)
585 {
586 if (c1[i].equals(Object.class) && !matchOnObject)
587 {
588 return false;
589 }
590 if (!c1[i].isAssignableFrom(c2[i]))
591 {
592
593 return false;
594 }
595 }
596 return true;
597 }
598
599 public static Class wrapperToPrimitive(Class wrapper)
600 {
601 return (Class) MapUtils.getObject(wrapperToPrimitiveMap, wrapper, wrapper);
602 }
603
604 public static Class[] wrappersToPrimitives(Class[] wrappers)
605 {
606 if (wrappers == null)
607 {
608 return null;
609 }
610
611 if (wrappers.length == 0)
612 {
613 return wrappers;
614 }
615
616 Class[] primitives = new Class[wrappers.length];
617
618 for (int i = 0; i < wrappers.length; i++)
619 {
620 primitives[i] = (Class) MapUtils.getObject(wrapperToPrimitiveMap, wrappers[i], wrappers[i]);
621 }
622
623 return primitives;
624 }
625
626
627
628
629
630
631 public static String getSimpleName(Class clazz)
632 {
633 if (null == clazz)
634 {
635 return "null";
636 }
637 else
638 {
639 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 encodedName.mark(1);
652 switch(encodedName.read())
653 {
654 case -1: return "null";
655 case 'Z': return "boolean";
656 case 'B': return "byte";
657 case 'C': return "char";
658 case 'D': return "double";
659 case 'F': return "float";
660 case 'I': return "int";
661 case 'J': return "long";
662 case 'S': return "short";
663 case '[': return classNameHelper(encodedName) + "[]";
664 case 'L': return shorten(new BufferedReader(encodedName).readLine());
665 default:
666 encodedName.reset();
667 return shorten(new BufferedReader(encodedName).readLine());
668 }
669 }
670 catch (IOException e)
671 {
672 return "unknown type: " + e.getMessage();
673 }
674 }
675
676
677
678
679
680 private static String shorten(String clazz)
681 {
682 if (null != clazz && clazz.endsWith(";"))
683 {
684 clazz = clazz.substring(0, clazz.length() - 1);
685 }
686 if (null != clazz && clazz.lastIndexOf(".") > -1)
687 {
688 clazz = clazz.substring(clazz.lastIndexOf(".") + 1, clazz.length());
689 }
690 return clazz;
691 }
692 }