1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.codehaus.gmavenplus.util;
18
19 import java.lang.reflect.Constructor;
20 import java.lang.reflect.Field;
21 import java.lang.reflect.InvocationTargetException;
22 import java.lang.reflect.Method;
23 import java.lang.reflect.Modifier;
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.List;
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41 public class ReflectionUtils {
42
43 private ReflectionUtils() {
44 }
45
46
47
48
49
50
51
52
53
54 public static Constructor<?> findConstructor(final Class<?> clazz, final Class<?>... paramTypes) {
55 if (clazz == null) {
56 throw new IllegalArgumentException("Class must not be null.");
57 }
58 Class<?> searchType = clazz;
59 while (searchType != null) {
60 Constructor<?>[] constructors = searchType.isInterface() ? clazz.getConstructors() : clazz.getDeclaredConstructors();
61 for (Constructor<?> constructor : constructors) {
62 if (paramTypes == null || Arrays.equals(paramTypes, constructor.getParameterTypes())) {
63 return constructor;
64 }
65 }
66 searchType = searchType.getSuperclass();
67 }
68 throw new IllegalArgumentException("Unable to find constructor " + clazz.getName() + "(" + Arrays.toString(paramTypes).replaceAll("^\\[", "").replaceAll("]$", "").replaceAll("class ", "") + ").");
69 }
70
71
72
73
74
75
76
77
78
79
80 public static Field findField(final Class<?> clazz, final String name, final Class<?> type) {
81 if (clazz == null) {
82 throw new IllegalArgumentException("Class must not be null");
83 }
84 if (name == null && type == null) {
85 throw new IllegalArgumentException("Either name or type of the field must be specified.");
86 }
87 Class<?> searchType = clazz;
88 while (Object.class != searchType && searchType != null) {
89 Field[] fields = searchType.getDeclaredFields();
90 for (Field field : fields) {
91 if ((name == null || name.equals(field.getName())) && (type == null || type.equals(field.getType()))) {
92 return field;
93 }
94 }
95 searchType = searchType.getSuperclass();
96 }
97 throw new IllegalArgumentException("Unable to find " + (type != null ? type.getName() : "") + " " + (name != null ? name : "") + ".");
98 }
99
100
101
102
103
104
105
106
107
108
109
110 public static Method findMethod(final Class<?> clazz, final String name, final Class<?>... paramTypes) {
111 if (clazz == null) {
112 throw new IllegalArgumentException("Class must not be null.");
113 }
114 if (name == null) {
115 throw new IllegalArgumentException("Method name must not be null.");
116 }
117 Class<?> searchType = clazz;
118 while (searchType != null) {
119 Method[] methods = (searchType.isInterface() ? searchType.getMethods() : getDeclaredMethods(searchType));
120 for (Method method : methods) {
121 if (name.equals(method.getName()) && (paramTypes == null || Arrays.equals(paramTypes, method.getParameterTypes()))) {
122 return method;
123 }
124 }
125 searchType = searchType.getSuperclass();
126 }
127 throw new IllegalArgumentException("Unable to find method " + clazz.getName() + "." + name + "(" + Arrays.toString(paramTypes).replaceAll("^\\[", "").replaceAll("]$", "").replaceAll("class ", "") + ").");
128 }
129
130
131
132
133
134
135
136
137 public static Object getEnumValue(final Class<?> clazz, final String valueName) {
138 if (clazz.isEnum()) {
139 for (Object o : clazz.getEnumConstants()) {
140 if (o.toString().equals(valueName)) {
141 return o;
142 }
143 }
144 throw new IllegalArgumentException("Unable to get an enum constant with that name.");
145 } else {
146 throw new IllegalArgumentException(clazz + " must be an enum.");
147 }
148 }
149
150
151
152
153
154
155
156
157
158
159 public static Object getField(final Field field, final Object target) throws IllegalAccessException {
160 field.setAccessible(true);
161 return field.get(target);
162 }
163
164
165
166
167
168
169
170
171
172 public static Object getStaticField(final Field field) throws IllegalAccessException {
173 if (!Modifier.isStatic(field.getModifiers())) {
174 throw new IllegalArgumentException("Field must be static.");
175 }
176 return getField(field, null);
177 }
178
179
180
181
182
183
184
185
186
187
188
189 public static Object invokeConstructor(final Constructor<?> constructor, final Object... args) throws InvocationTargetException, IllegalAccessException, InstantiationException {
190 if (constructor == null) {
191 throw new IllegalArgumentException("Constructor must not be null.");
192 }
193 constructor.setAccessible(true);
194 return constructor.newInstance(args);
195 }
196
197
198
199
200
201
202
203
204
205
206
207
208 public static Object invokeMethod(final Method method, final Object target, final Object... args) throws InvocationTargetException, IllegalAccessException {
209 if (method == null) {
210 throw new IllegalArgumentException("Method must not be null.");
211 }
212 if (target == null) {
213 throw new IllegalArgumentException("Object must not be null.");
214 }
215 method.setAccessible(true);
216 return method.invoke(target, args);
217 }
218
219
220
221
222
223
224
225
226
227
228 public static Object invokeStaticMethod(final Method method, final Object... args) throws InvocationTargetException, IllegalAccessException {
229 if (method == null) {
230 throw new IllegalArgumentException("Method must not be null.");
231 }
232 if (!Modifier.isStatic(method.getModifiers())) {
233 throw new IllegalArgumentException("Method must be static.");
234 }
235 method.setAccessible(true);
236 return method.invoke(null, args);
237 }
238
239
240
241
242
243
244
245
246
247 private static Method[] getDeclaredMethods(Class<?> clazz) {
248 Method[] result;
249 Method[] declaredMethods = clazz.getDeclaredMethods();
250 List<Method> defaultMethods = findConcreteMethodsOnInterfaces(clazz);
251 if (defaultMethods != null) {
252 result = new Method[declaredMethods.length + defaultMethods.size()];
253 System.arraycopy(declaredMethods, 0, result, 0, declaredMethods.length);
254 int index = declaredMethods.length;
255 for (Method defaultMethod : defaultMethods) {
256 result[index] = defaultMethod;
257 index++;
258 }
259 } else {
260 result = declaredMethods;
261 }
262 return result;
263 }
264
265 private static List<Method> findConcreteMethodsOnInterfaces(Class<?> clazz) {
266 List<Method> result = null;
267 for (Class<?> ifc : clazz.getInterfaces()) {
268 for (Method ifcMethod : ifc.getMethods()) {
269 if (!Modifier.isAbstract(ifcMethod.getModifiers())) {
270 if (result == null) {
271 result = new ArrayList<>();
272 }
273 result.add(ifcMethod);
274 }
275 }
276 }
277 return result;
278 }
279
280 }