2 * Copyright 2005-2010 the original author or authors.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.wamblee.reflection;
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;
27 * Some utilities for reflection.
29 * @author Erik Brakkee
31 public class ReflectionUtils {
33 public static final List<Class> PRIMITIVE_WRAPPERS = createPrimitiveWrappers();
35 private static final List<Class> createPrimitiveWrappers() {
36 Class[] vals = { Boolean.class, Byte.class, Character.class,
37 Short.class, Integer.class, Long.class, Float.class, Double.class };
38 return Arrays.asList(vals);
42 * Wraps a type by the corresponding wrapper type if it is a primitive type.
46 * @return Wrapped type for primitives or the provided argument value.
48 public static Class wrapIfNeeded(Class aClass) {
49 if (aClass == boolean.class) {
53 if (aClass == byte.class) {
57 if (aClass == char.class) {
58 return Character.class;
61 if (aClass == short.class) {
65 if (aClass == int.class) {
69 if (aClass == long.class) {
73 if (aClass == float.class) {
77 if (aClass == double.class) {
81 if (aClass == void.class) {
88 public static List<Method> getAllMethods(Class aClass,
89 Class... aExcludedClasses) {
90 if (aClass.isInterface()) {
91 throw new IllegalArgumentException(aClass.getName() +
92 " is not an interface.");
94 Map<String, Method> found = new HashMap<String, Method>();
95 getAllMethods(aClass, found, Arrays.asList(aExcludedClasses));
97 return new ArrayList<Method>(found.values());
100 private static void getAllMethods(Class aClass, Map<String, Method> aFound,
101 List<Class> aExcludedClasses) {
102 List<Method> declared = Arrays.asList(aClass.getDeclaredMethods());
104 for (Method method : declared) {
105 Method superMethod = aFound.get(method.getName());
107 if (superMethod == null) {
108 // no subclass method
109 aFound.put(method.getName(), method);
111 // subclass method. Check for override.
112 if (!Arrays.equals(superMethod.getParameterTypes(), method
113 .getParameterTypes())) {
114 // parameters differ so this is a new method.
115 aFound.put(method.getName(), method);
120 Class superClass = aClass.getSuperclass();
122 if (superClass != null && !aExcludedClasses.contains(superClass)) {
123 getAllMethods(superClass, aFound, aExcludedClasses);
127 public static List<Field> getAllFields(Class aClass,
128 Class... aExcludedClasses) {
129 if (aClass.isInterface()) {
130 throw new IllegalArgumentException(aClass.getName() +
131 " is an interface.");
133 List<Field> found = new ArrayList<Field>();
134 getAllFields(aClass, found, Arrays.asList(aExcludedClasses));
139 private static void getAllFields(Class aClass, List<Field> aFound,
140 List<Class> aExcludedClasses) {
141 List<Field> declared = Arrays.asList(aClass.getDeclaredFields());
143 for (Field field : declared) {
147 Class superClass = aClass.getSuperclass();
149 if (superClass != null && !aExcludedClasses.contains(superClass)) {
150 getAllFields(superClass, aFound, aExcludedClasses);
155 * Checks if a class is a primitive type or wrapper type.
160 public static boolean isPrimitive(Class aClass) {
161 if (aClass.isPrimitive()) {
164 return PRIMITIVE_WRAPPERS.contains(aClass);