1 package org.wamblee.reflection;
3 import java.lang.reflect.Method;
5 import java.util.ArrayList;
6 import java.util.Arrays;
7 import java.util.HashMap;
11 public class ReflectionUtils {
13 * Wraps a type by the corresponding wrapper type if it is a primitive type.
17 * @return Wrapped type for primitives or the provided argument value.
19 public static Class wrapIfNeeded(Class aClass) {
20 if (aClass == boolean.class) {
24 if (aClass == byte.class) {
28 if (aClass == char.class) {
29 return Character.class;
32 if (aClass == short.class) {
36 if (aClass == int.class) {
40 if (aClass == long.class) {
44 if (aClass == float.class) {
48 if (aClass == double.class) {
52 if (aClass == void.class) {
59 public static List<Method> getAllMethods(Class aClass) {
60 Map<String, Method> found = new HashMap<String, Method>();
61 getAllMethods(aClass, found);
63 return new ArrayList<Method>(found.values());
66 private static void getAllMethods(Class aClass, Map<String, Method> aFound) {
67 List<Method> declared = Arrays.asList(aClass.getDeclaredMethods());
69 for (Method method : declared) {
70 Method superMethod = aFound.get(method.getName());
72 if (superMethod == null) {
73 // no superclass method
74 aFound.put(method.getName(), method);
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);
85 Class superClass = aClass.getSuperclass();
87 if (superClass != null) {
88 getAllMethods(superClass, aFound);