7c5c45f94fd7f1e2d44a58fafbf38b7cd3f444f2
[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 /**
27  * Some utilities for reflection. 
28  * 
29  * @author Erik Brakkee
30  */
31 public class ReflectionUtils {
32     /**
33      * Wraps a type by the corresponding wrapper type if it is a primitive type.
34      * 
35      * @param aClass
36      *            Type to wrap.
37      * @return Wrapped type for primitives or the provided argument value.
38      */
39     public static Class wrapIfNeeded(Class aClass) {
40         if (aClass == boolean.class) {
41             return Boolean.class;
42         }
43
44         if (aClass == byte.class) {
45             return Byte.class;
46         }
47
48         if (aClass == char.class) {
49             return Character.class;
50         }
51
52         if (aClass == short.class) {
53             return Short.class;
54         }
55
56         if (aClass == int.class) {
57             return Integer.class;
58         }
59
60         if (aClass == long.class) {
61             return Long.class;
62         }
63
64         if (aClass == float.class) {
65             return Float.class;
66         }
67
68         if (aClass == double.class) {
69             return Double.class;
70         }
71
72         if (aClass == void.class) {
73             return Void.class;
74         }
75
76         return aClass;
77     }
78
79     public static List<Method> getAllMethods(Class aClass,
80         Class... aExcludedClasses) {
81         if (aClass.isInterface()) {
82             throw new IllegalArgumentException(aClass.getName() +
83                 " is not an interface.");
84         }
85         Map<String, Method> found = new HashMap<String, Method>();
86         getAllMethods(aClass, found, Arrays.asList(aExcludedClasses));
87
88         return new ArrayList<Method>(found.values());
89     }
90
91     private static void getAllMethods(Class aClass, Map<String, Method> aFound,
92         List<Class> aExcludedClasses) {
93         List<Method> declared = Arrays.asList(aClass.getDeclaredMethods());
94
95         for (Method method : declared) {
96             Method superMethod = aFound.get(method.getName());
97
98             if (superMethod == null) {
99                 // no subclass method
100                 aFound.put(method.getName(), method);
101             } else {
102                 // subclass method. Check for override.
103                 if (!Arrays.equals(superMethod.getParameterTypes(), method
104                     .getParameterTypes())) {
105                     // parameters differ so this is a new method.
106                     aFound.put(method.getName(), method);
107                 }
108             }
109         }
110
111         Class superClass = aClass.getSuperclass();
112
113         if (superClass != null && !aExcludedClasses.contains(superClass)) {
114             getAllMethods(superClass, aFound, aExcludedClasses);
115         }
116     }
117
118     public static List<Field> getAllFields(Class aClass,
119         Class... aExcludedClasses) {
120         if (aClass.isInterface()) {
121             throw new IllegalArgumentException(aClass.getName() +
122                 " is an interface.");
123         }
124         List<Field> found = new ArrayList<Field>();
125         getAllFields(aClass, found, Arrays.asList(aExcludedClasses));
126
127         return found;
128     }
129
130     private static void getAllFields(Class aClass, List<Field> aFound,
131         List<Class> aExcludedClasses) {
132         List<Field> declared = Arrays.asList(aClass.getDeclaredFields());
133
134         for (Field field : declared) {
135             aFound.add(field);
136         }
137
138         Class superClass = aClass.getSuperclass();
139
140         if (superClass != null && !aExcludedClasses.contains(superClass)) {
141             getAllFields(superClass, aFound, aExcludedClasses);
142         }
143     }
144 }