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