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 =
34 createPrimitiveWrappers();
36 private static final List<Class> createPrimitiveWrappers() {
38 Boolean.class, Byte.class, Character.class, Short.class, Integer.class, Long.class,
39 Float.class, Double.class };
40 return Arrays.asList(vals);
47 * Wraps a type by the corresponding wrapper type if it is a primitive type.
51 * @return Wrapped type for primitives or the provided argument value.
53 public static Class wrapIfNeeded(Class aClass) {
54 if (aClass == boolean.class) {
58 if (aClass == byte.class) {
62 if (aClass == char.class) {
63 return Character.class;
66 if (aClass == short.class) {
70 if (aClass == int.class) {
74 if (aClass == long.class) {
78 if (aClass == float.class) {
82 if (aClass == double.class) {
86 if (aClass == void.class) {
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.");
99 Map<String, Method> found = new HashMap<String, Method>();
100 getAllMethods(aClass, found, Arrays.asList(aExcludedClasses));
102 return new ArrayList<Method>(found.values());
105 private static void getAllMethods(Class aClass, Map<String, Method> aFound,
106 List<Class> aExcludedClasses) {
107 List<Method> declared = Arrays.asList(aClass.getDeclaredMethods());
109 for (Method method : declared) {
110 Method superMethod = aFound.get(method.getName());
112 if (superMethod == null) {
113 // no subclass method
114 aFound.put(method.getName(), method);
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);
125 Class superClass = aClass.getSuperclass();
127 if (superClass != null && !aExcludedClasses.contains(superClass)) {
128 getAllMethods(superClass, aFound, aExcludedClasses);
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.");
138 List<Field> found = new ArrayList<Field>();
139 getAllFields(aClass, found, Arrays.asList(aExcludedClasses));
144 private static void getAllFields(Class aClass, List<Field> aFound,
145 List<Class> aExcludedClasses) {
146 List<Field> declared = Arrays.asList(aClass.getDeclaredFields());
148 for (Field field : declared) {
152 Class superClass = aClass.getSuperclass();
154 if (superClass != null && !aExcludedClasses.contains(superClass)) {
155 getAllFields(superClass, aFound, aExcludedClasses);
160 * Checks if a class is a primitive type or wrapper type.
164 public static boolean isPrimitive(Class aClass) {
165 if ( aClass.isPrimitive()) {
168 return PRIMITIVE_WRAPPERS.contains(aClass);