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;
26 public class ReflectionUtils {
28 * Wraps a type by the corresponding wrapper type if it is a primitive type.
32 * @return Wrapped type for primitives or the provided argument value.
34 public static Class wrapIfNeeded(Class aClass) {
35 if (aClass == boolean.class) {
39 if (aClass == byte.class) {
43 if (aClass == char.class) {
44 return Character.class;
47 if (aClass == short.class) {
51 if (aClass == int.class) {
55 if (aClass == long.class) {
59 if (aClass == float.class) {
63 if (aClass == double.class) {
67 if (aClass == void.class) {
74 public static List<Method> getAllMethods(Class aClass,
75 Class... aExcludedClasses) {
76 if (aClass.isInterface()) {
77 throw new IllegalArgumentException(aClass.getName() +
78 " is not an interface.");
80 Map<String, Method> found = new HashMap<String, Method>();
81 getAllMethods(aClass, found, Arrays.asList(aExcludedClasses));
83 return new ArrayList<Method>(found.values());
86 private static void getAllMethods(Class aClass, Map<String, Method> aFound,
87 List<Class> aExcludedClasses) {
88 List<Method> declared = Arrays.asList(aClass.getDeclaredMethods());
90 for (Method method : declared) {
91 Method superMethod = aFound.get(method.getName());
93 if (superMethod == null) {
95 aFound.put(method.getName(), method);
97 // subclass method. Check for override.
98 if (!Arrays.equals(superMethod.getParameterTypes(), method
99 .getParameterTypes())) {
100 // parameters differ so this is a new method.
101 aFound.put(method.getName(), method);
106 Class superClass = aClass.getSuperclass();
108 if (superClass != null && !aExcludedClasses.contains(superClass)) {
109 getAllMethods(superClass, aFound, aExcludedClasses);
113 public static List<Field> getAllFields(Class aClass,
114 Class... aExcludedClasses) {
115 if (aClass.isInterface()) {
116 throw new IllegalArgumentException(aClass.getName() +
117 " is an interface.");
119 List<Field> found = new ArrayList<Field>();
120 getAllFields(aClass, found, Arrays.asList(aExcludedClasses));
125 private static void getAllFields(Class aClass, List<Field> aFound,
126 List<Class> aExcludedClasses) {
127 List<Field> declared = Arrays.asList(aClass.getDeclaredFields());
129 for (Field field : declared) {
133 Class superClass = aClass.getSuperclass();
135 if (superClass != null && !aExcludedClasses.contains(superClass)) {
136 getAllFields(superClass, aFound, aExcludedClasses);