copyright messages updated in all java filees.
[utils] / support / general / src / main / java / org / wamblee / reflection / ReflectionUtils.java
1 /*
2  * Copyright 2005-2010 the original author or authors.
3  * 
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
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
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.
15  */ 
16 package org.wamblee.reflection;
17
18 import java.lang.reflect.Method;
19
20 import java.util.ArrayList;
21 import java.util.Arrays;
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.Map;
25
26 public class ReflectionUtils {
27     /**
28      * Wraps a type by the corresponding wrapper type if it is a primitive type.
29      * 
30      * @param aClass
31      *            Type to wrap.
32      * @return Wrapped type for primitives or the provided argument value.
33      */
34     public static Class wrapIfNeeded(Class aClass) {
35         if (aClass == boolean.class) {
36             return Boolean.class;
37         }
38
39         if (aClass == byte.class) {
40             return Byte.class;
41         }
42
43         if (aClass == char.class) {
44             return Character.class;
45         }
46
47         if (aClass == short.class) {
48             return Short.class;
49         }
50
51         if (aClass == int.class) {
52             return Integer.class;
53         }
54
55         if (aClass == long.class) {
56             return Long.class;
57         }
58
59         if (aClass == float.class) {
60             return Float.class;
61         }
62
63         if (aClass == double.class) {
64             return Double.class;
65         }
66
67         if (aClass == void.class) {
68             return Void.class;
69         }
70
71         return aClass;
72     }
73
74     public static List<Method> getAllMethods(Class aClass) {
75         Map<String, Method> found = new HashMap<String, Method>();
76         getAllMethods(aClass, found);
77
78         return new ArrayList<Method>(found.values());
79     }
80
81     private static void getAllMethods(Class aClass, Map<String, Method> aFound) {
82         List<Method> declared = Arrays.asList(aClass.getDeclaredMethods());
83
84         for (Method method : declared) {
85             Method superMethod = aFound.get(method.getName());
86
87             if (superMethod == null) {
88                 // no superclass method
89                 aFound.put(method.getName(), method);
90             } else {
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);
96                 }
97             }
98         }
99
100         Class superClass = aClass.getSuperclass();
101
102         if (superClass != null) {
103             getAllMethods(superClass, aFound);
104         }
105     }
106 }