(no commit message)
[utils] / support / general / src / main / java / org / wamblee / reflection / ReflectionUtils.java
1 /*
2  * Copyright 2005-2010 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.wamblee.reflection;
17
18 import java.lang.reflect.Field;
19 import java.lang.reflect.Method;
20 import java.util.ArrayList;
21 import java.util.Arrays;
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.Map;
25
26 public class ReflectionUtils {
27     /**
28      * Wraps a type by the corresponding wrapper type if it is a primitive type.
29      * 
30      * @param aClass
31      *            Type to wrap.
32      * @return Wrapped type for primitives or the provided argument value.
33      */
34     public static Class wrapIfNeeded(Class aClass) {
35         if (aClass == boolean.class) {
36             return Boolean.class;
37         }
38
39         if (aClass == byte.class) {
40             return Byte.class;
41         }
42
43         if (aClass == char.class) {
44             return Character.class;
45         }
46
47         if (aClass == short.class) {
48             return Short.class;
49         }
50
51         if (aClass == int.class) {
52             return Integer.class;
53         }
54
55         if (aClass == long.class) {
56             return Long.class;
57         }
58
59         if (aClass == float.class) {
60             return Float.class;
61         }
62
63         if (aClass == double.class) {
64             return Double.class;
65         }
66
67         if (aClass == void.class) {
68             return Void.class;
69         }
70
71         return aClass;
72     }
73
74     public static List<Method> getAllMethods(Class aClass,
75         Class... aExcludedClasses) {
76         if (aClass.isInterface()) {
77             throw new IllegalArgumentException(aClass.getName() +
78                 " is not an interface.");
79         }
80         Map<String, Method> found = new HashMap<String, Method>();
81         getAllMethods(aClass, found, Arrays.asList(aExcludedClasses));
82
83         return new ArrayList<Method>(found.values());
84     }
85
86     private static void getAllMethods(Class aClass, Map<String, Method> aFound,
87         List<Class> aExcludedClasses) {
88         List<Method> declared = Arrays.asList(aClass.getDeclaredMethods());
89
90         for (Method method : declared) {
91             Method superMethod = aFound.get(method.getName());
92
93             if (superMethod == null) {
94                 // no subclass method
95                 aFound.put(method.getName(), method);
96             } else {
97                 // subclass method. Check for override.
98                 if (!Arrays.equals(superMethod.getParameterTypes(), method
99                     .getParameterTypes())) {
100                     // parameters differ so this is a new method.
101                     aFound.put(method.getName(), method);
102                 }
103             }
104         }
105
106         Class superClass = aClass.getSuperclass();
107
108         if (superClass != null && !aExcludedClasses.contains(superClass)) {
109             getAllMethods(superClass, aFound, aExcludedClasses);
110         }
111     }
112
113     public static List<Field> getAllFields(Class aClass,
114         Class... aExcludedClasses) {
115         if (aClass.isInterface()) {
116             throw new IllegalArgumentException(aClass.getName() +
117                 " is an interface.");
118         }
119         List<Field> found = new ArrayList<Field>();
120         getAllFields(aClass, found, Arrays.asList(aExcludedClasses));
121
122         return found;
123     }
124
125     private static void getAllFields(Class aClass, List<Field> aFound,
126         List<Class> aExcludedClasses) {
127         List<Field> declared = Arrays.asList(aClass.getDeclaredFields());
128
129         for (Field field : declared) {
130             aFound.add(field);
131         }
132
133         Class superClass = aClass.getSuperclass();
134
135         if (superClass != null && !aExcludedClasses.contains(superClass)) {
136             getAllFields(superClass, aFound, aExcludedClasses);
137         }
138     }
139 }