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 * Wraps a type by the corresponding wrapper type if it is a primitive type.
37 * @return Wrapped type for primitives or the provided argument value.
39 public static Class wrapIfNeeded(Class aClass) {
40 if (aClass == boolean.class) {
44 if (aClass == byte.class) {
48 if (aClass == char.class) {
49 return Character.class;
52 if (aClass == short.class) {
56 if (aClass == int.class) {
60 if (aClass == long.class) {
64 if (aClass == float.class) {
68 if (aClass == double.class) {
72 if (aClass == void.class) {
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.");
85 Map<String, Method> found = new HashMap<String, Method>();
86 getAllMethods(aClass, found, Arrays.asList(aExcludedClasses));
88 return new ArrayList<Method>(found.values());
91 private static void getAllMethods(Class aClass, Map<String, Method> aFound,
92 List<Class> aExcludedClasses) {
93 List<Method> declared = Arrays.asList(aClass.getDeclaredMethods());
95 for (Method method : declared) {
96 Method superMethod = aFound.get(method.getName());
98 if (superMethod == null) {
100 aFound.put(method.getName(), method);
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);
111 Class superClass = aClass.getSuperclass();
113 if (superClass != null && !aExcludedClasses.contains(superClass)) {
114 getAllMethods(superClass, aFound, aExcludedClasses);
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.");
124 List<Field> found = new ArrayList<Field>();
125 getAllFields(aClass, found, Arrays.asList(aExcludedClasses));
130 private static void getAllFields(Class aClass, List<Field> aFound,
131 List<Class> aExcludedClasses) {
132 List<Field> declared = Arrays.asList(aClass.getDeclaredFields());
134 for (Field field : declared) {
138 Class superClass = aClass.getSuperclass();
140 if (superClass != null && !aExcludedClasses.contains(superClass)) {
141 getAllFields(superClass, aFound, aExcludedClasses);