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