a1fa5522b8f68bd0da7a8271a9168ef73421bd7c
[utils] / support / general / src / main / java / org / wamblee / reflection / ReflectionUtils.java
1 package org.wamblee.reflection;
2
3 import java.lang.reflect.Method;
4 import java.util.ArrayList;
5 import java.util.Arrays;
6 import java.util.HashMap;
7 import java.util.List;
8 import java.util.Map;
9
10 public class ReflectionUtils {
11
12         /**
13          * Wraps a type by the corresponding wrapper type if it is a primitive
14          * type.
15          * @param aClass Type to wrap. 
16          * @return Wrapped type for primitives or the provided argument value. 
17          */
18         public static Class wrapIfNeeded(Class aClass) {
19
20                 if (aClass == boolean.class) {
21                         return Boolean.class;
22                 }
23                 if (aClass == byte.class) {
24                         return Byte.class;
25                 }
26                 if (aClass == char.class) {
27                         return Character.class;
28                 }
29                 if (aClass == short.class) {
30                         return Short.class;
31                 }
32                 if (aClass == int.class) {
33                         return Integer.class;
34                 }
35                 if (aClass == long.class) {
36                         return Long.class;
37                 }
38                 if (aClass == float.class) {
39                         return Float.class;
40                 }
41                 if (aClass == double.class) {
42                         return Double.class;
43                 }
44                 if (aClass == void.class) {
45                         return Void.class;
46                 }
47                 return aClass;
48         }
49         
50         
51         public static List<Method> getAllMethods(Class aClass) {
52                 
53                 Map<String,Method> found = new HashMap<String, Method>();
54                 getAllMethods(aClass, found);
55                 return new ArrayList<Method>(found.values());
56         }
57
58         private static void getAllMethods(Class aClass, Map<String,Method> aFound) { 
59                 List<Method> declared = Arrays.asList(aClass.getDeclaredMethods());
60                 for (Method method: declared) { 
61                         Method superMethod = aFound.get(method.getName());
62                         if ( superMethod == null ) { 
63                                 // no superclass method
64                                 aFound.put(method.getName(), method);
65                         }
66                         else { 
67                                 // super class method. Check for override.
68                                 if ( !Arrays.equals(superMethod.getParameterTypes(), method.getParameterTypes())) { 
69                                         // parameters differ so this is a new method.
70                                         aFound.put(method.getName(), method);
71                                 }
72                         }
73                 }
74                 Class superClass = aClass.getSuperclass();
75                 if (superClass != null) {
76                         getAllMethods(superClass, aFound);
77                 }
78         }
79 }