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.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 Map<String, Method> found = new HashMap<String, Method>();
76 getAllMethods(aClass, found);
78 return new ArrayList<Method>(found.values());
81 private static void getAllMethods(Class aClass, Map<String, Method> aFound) {
82 List<Method> declared = Arrays.asList(aClass.getDeclaredMethods());
84 for (Method method : declared) {
85 Method superMethod = aFound.get(method.getName());
87 if (superMethod == null) {
88 // no superclass method
89 aFound.put(method.getName(), method);
91 // super class method. Check for override.
92 if (!Arrays.equals(superMethod.getParameterTypes(), method
93 .getParameterTypes())) {
94 // parameters differ so this is a new method.
95 aFound.put(method.getName(), method);
100 Class superClass = aClass.getSuperclass();
102 if (superClass != null) {
103 getAllMethods(superClass, aFound);