forked from jdereg/java-util
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReflectionUtils.java
652 lines (613 loc) · 25.1 KB
/
ReflectionUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
package com.cedarsoftware.util;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
/**
* Utilities to simplify writing reflective code as well as improve performance of reflective operations like
* method and annotation lookups.
*
* @author John DeRegnaucourt (jdereg@gmail.com)
* <br>
* Copyright (c) Cedar Software LLC
* <br><br>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <br><br>
* <a href="http://www.apache.org/licenses/LICENSE-2.0">License</a>
* <br><br>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
public final class ReflectionUtils
{
private static final ConcurrentMap<String, Collection<Field>> FIELD_MAP = new ConcurrentHashMap<>();
private static final ConcurrentMap<String, Method> METHOD_MAP = new ConcurrentHashMap<>();
private static final ConcurrentMap<String, Method> METHOD_MAP2 = new ConcurrentHashMap<>();
private static final ConcurrentMap<String, Method> METHOD_MAP3 = new ConcurrentHashMap<>();
private static final ConcurrentMap<String, Constructor<?>> CONSTRUCTORS = new ConcurrentHashMap<>();
private static final ConcurrentMap<Class<?>, List<Field>> FIELD_META_CACHE = new ConcurrentHashMap<>();
private ReflectionUtils()
{
super();
}
/**
* Determine if the passed in class (classToCheck) has the annotation (annoClass) on itself,
* any of its super classes, any of it's interfaces, or any of it's super interfaces.
* This is a exhaustive check throughout the complete inheritance hierarchy.
* @return the Annotation if found, null otherwise.
*/
public static <T extends Annotation> T getClassAnnotation(final Class<?> classToCheck, final Class<T> annoClass)
{
final Set<Class<?>> visited = new HashSet<>();
final LinkedList<Class<?>> stack = new LinkedList<>();
stack.add(classToCheck);
while (!stack.isEmpty())
{
Class<?> classToChk = stack.pop();
if (classToChk == null || visited.contains(classToChk))
{
continue;
}
visited.add(classToChk);
T a = (T) classToChk.getAnnotation(annoClass);
if (a != null)
{
return a;
}
stack.push(classToChk.getSuperclass());
addInterfaces(classToChk, stack);
}
return null;
}
private static void addInterfaces(final Class<?> classToCheck, final LinkedList<Class<?>> stack)
{
for (Class<?> interFace : classToCheck.getInterfaces())
{
stack.push(interFace);
}
}
public static <T extends Annotation> T getMethodAnnotation(final Method method, final Class<T> annoClass)
{
final Set<Class<?>> visited = new HashSet<>();
final LinkedList<Class<?>> stack = new LinkedList<>();
stack.add(method.getDeclaringClass());
while (!stack.isEmpty())
{
Class<?> classToChk = stack.pop();
if (classToChk == null || visited.contains(classToChk))
{
continue;
}
visited.add(classToChk);
Method m = getMethod(classToChk, method.getName(), method.getParameterTypes());
if (m == null)
{
continue;
}
T a = m.getAnnotation(annoClass);
if (a != null)
{
return a;
}
stack.push(classToChk.getSuperclass());
addInterfaces(method.getDeclaringClass(), stack);
}
return null;
}
/**
* Fetch a public method reflectively by name with argument types. This method caches the lookup, so that
* subsequent calls are significantly faster. The method can be on an inherited class of the passed in [starting]
* Class.
* @param c Class on which method is to be found.
* @param methodName String name of method to find.
* @param types Argument types for the method (null is used for no argument methods).
* @return Method located, or null if not found.
*/
public static Method getMethod(Class<?> c, String methodName, Class<?>...types)
{
try
{
StringBuilder builder = new StringBuilder(getClassLoaderName(c));
builder.append('.');
builder.append(c.getName());
builder.append('.');
builder.append(methodName);
builder.append(makeParamKey(types));
// methodKey is in form ClassName.methodName:arg1.class|arg2.class|...
String methodKey = builder.toString();
Method method = METHOD_MAP.get(methodKey);
if (method == null)
{
method = c.getMethod(methodName, types);
Method other = METHOD_MAP.putIfAbsent(methodKey, method);
if (other != null)
{
method = other;
}
}
return method;
}
catch (Exception nse)
{
return null;
}
}
/**
* Retrieve the declared fields on a Class.
*/
public static List<Field> getDeclaredFields(final Class<?> c) {
return FIELD_META_CACHE.computeIfAbsent(c, ReflectionUtils::buildDeclaredFields);
}
/**
* Get all non static, non transient, fields of the passed in class, including
* private fields. Note, the special this$ field is also not returned. The result
* is cached in a static ConcurrentHashMap to benefit execution performance.
* @param c Class instance
* @return Collection of only the fields in the passed in class
* that would need further processing (reference fields). This
* makes field traversal on a class faster as it does not need to
* continually process known fields like primitives.
*/
public static Collection<Field> getDeepDeclaredFields(Class<?> c)
{
StringBuilder builder = new StringBuilder(getClassLoaderName(c));
builder.append('.');
builder.append(c.getName());
String key = builder.toString();
Collection<Field> fields = FIELD_MAP.get(key);
if (fields != null)
{
return fields;
}
fields = new ArrayList<>();
Class<?> curr = c;
while (curr != null)
{
getDeclaredFields(curr, fields);
curr = curr.getSuperclass();
}
FIELD_MAP.put(key, fields);
return fields;
}
/**
* Get all non static, non transient, fields of the passed in class, including
* private fields. Note, the special this$ field is also not returned. The
* resulting fields are stored in a Collection.
* @param c Class instance
* that would need further processing (reference fields). This
* makes field traversal on a class faster as it does not need to
* continually process known fields like primitives.
*/
public static void getDeclaredFields(Class<?> c, Collection<Field> fields) {
try
{
Field[] local = c.getDeclaredFields();
for (Field field : local)
{
int modifiers = field.getModifiers();
if (Modifier.isStatic(modifiers) || Modifier.isTransient(modifiers))
{ // skip static and transient fields
continue;
}
String fieldName = field.getName();
if ("metaClass".equals(fieldName) && "groovy.lang.MetaClass".equals(field.getType().getName()))
{ // skip Groovy metaClass field if present (without tying this project to Groovy in any way).
continue;
}
if (fieldName.startsWith("this$"))
{ // Skip field in nested class pointing to enclosing outer class instance
continue;
}
if (Modifier.isPublic(modifiers))
{
fields.add(field);
}
else
{
// JDK11+ field.trySetAccessible();
try
{
field.setAccessible(true);
}
catch(Exception e) { }
// JDK11+
fields.add(field);
}
}
}
catch (Throwable ignore)
{
ExceptionUtilities.safelyIgnoreException(ignore);
}
}
/**
* Return all Fields from a class (including inherited), mapped by
* String field name to java.lang.reflect.Field.
* @param c Class whose fields are being fetched.
* @return Map of all fields on the Class, keyed by String field
* name to java.lang.reflect.Field.
*/
public static Map<String, Field> getDeepDeclaredFieldMap(Class<?> c)
{
Map<String, Field> fieldMap = new HashMap<>();
Collection<Field> fields = getDeepDeclaredFields(c);
for (Field field : fields)
{
String fieldName = field.getName();
if (fieldMap.containsKey(fieldName))
{ // Can happen when parent and child class both have private field with same name
fieldMap.put(field.getDeclaringClass().getName() + '.' + fieldName, field);
}
else
{
fieldMap.put(fieldName, field);
}
}
return fieldMap;
}
/**
* Make reflective method calls without having to handle two checked exceptions (IllegalAccessException and
* InvocationTargetException). These exceptions are caught and rethrown as RuntimeExceptions, with the original
* exception passed (nested) on.
* @param bean Object (instance) on which to call method.
* @param method Method instance from target object [easily obtained by calling ReflectionUtils.getMethod()].
* @param args Arguments to pass to method.
* @return Object Value from reflectively called method.
*/
public static Object call(Object bean, Method method, Object... args)
{
if (method == null)
{
String className = bean == null ? "null bean" : bean.getClass().getName();
throw new IllegalArgumentException("null Method passed to ReflectionUtils.call() on bean of type: " + className);
}
if (bean == null)
{
throw new IllegalArgumentException("Cannot call [" + method.getName() + "()] on a null object.");
}
try
{
return method.invoke(bean, args);
}
catch (IllegalAccessException e)
{
throw new RuntimeException("IllegalAccessException occurred attempting to reflectively call method: " + method.getName() + "()", e);
}
catch (InvocationTargetException e)
{
throw new RuntimeException("Exception thrown inside reflectively called method: " + method.getName() + "()", e.getTargetException());
}
}
/**
* Make a reflective method call in one step. This approach does not support calling two different methods with
* the same argument count, since it caches methods internally by "className.methodName|argCount". For example,
* if you had a class with two methods, foo(int, String) and foo(String, String), you cannot use this method.
* However, this method would support calling foo(int), foo(int, String), foo(int, String, Object), etc.
* Internally, it is caching the reflective method lookups as mentioned earlier for speed, using argument count
* as part of the key (not all argument types).
*
* Ideally, use the call(Object, Method, Object...args) method when possible, as it will support any method, and
* also provides caching. There are times, however, when all that is passed in (REST APIs) is argument values,
* and if some of those are null, you may have an ambiguous targeted method. With this approach, you can still
* call these methods, assuming the methods are not overloaded with the same number of arguments and differing
* types.
*
* @param bean Object instance on which to call method.
* @param methodName String name of method to call.
* @param args Arguments to pass.
* @return Object value returned from the reflectively invoked method.
*/
public static Object call(Object bean, String methodName, Object... args)
{
Method method = getMethod(bean, methodName, args.length);
try
{
return method.invoke(bean, args);
}
catch (IllegalAccessException e)
{
throw new RuntimeException("IllegalAccessException occurred attempting to reflectively call method: " + method.getName() + "()", e);
}
catch (InvocationTargetException e)
{
throw new RuntimeException("Exception thrown inside reflectively called method: " + method.getName() + "()", e.getTargetException());
}
}
/**
* Fetch the named method from the passed in Object instance. This method caches found methods, so it should be used
* instead of reflectively searching for the method every time. Ideally, use the other getMethod() API that
* takes an additional argument, Class[] of argument types (most desirable). This is to better support overloaded
* methods. Sometimes, you only have the argument values, and if they can be null, you cannot call the getMethod()
* API that take argument Class types.
* @param bean Object on which the named method will be found.
* @param methodName String name of method to be located on the controller.
* @param argCount int number of arguments. This is used as part of the cache key to allow for
* duplicate method names as long as the argument list length is different.
* @throws IllegalArgumentException
*/
public static Method getMethod(Object bean, String methodName, int argCount)
{
if (bean == null)
{
throw new IllegalArgumentException("Attempted to call getMethod() [" + methodName + "()] on a null instance.");
}
if (methodName == null)
{
throw new IllegalArgumentException("Attempted to call getMethod() with a null method name on an instance of: " + bean.getClass().getName());
}
Class<?> beanClass = bean.getClass();
StringBuilder builder = new StringBuilder(getClassLoaderName(beanClass));
builder.append('.');
builder.append(beanClass.getName());
builder.append('.');
builder.append(methodName);
builder.append('|');
builder.append(argCount);
String methodKey = builder.toString();
Method method = METHOD_MAP2.get(methodKey);
if (method == null)
{
method = getMethodWithArgs(beanClass, methodName, argCount);
if (method == null)
{
throw new IllegalArgumentException("Method: " + methodName + "() is not found on class: " + beanClass.getName() + ". Perhaps the method is protected, private, or misspelled?");
}
Method other = METHOD_MAP2.putIfAbsent(methodKey, method);
if (other != null)
{
method = other;
}
}
return method;
}
/**
* Reflectively find the requested method on the requested class, only matching on argument count.
*/
private static Method getMethodWithArgs(Class<?> c, String methodName, int argc)
{
Method[] methods = c.getMethods();
for (Method method : methods)
{
if (methodName.equals(method.getName()) && method.getParameterTypes().length == argc)
{
return method;
}
}
return null;
}
public static Constructor<?> getConstructor(Class<?> clazz, Class<?>... parameterTypes)
{
try
{
String key = clazz.getName() + makeParamKey(parameterTypes);
Constructor<?> constructor = CONSTRUCTORS.get(key);
if (constructor == null)
{
constructor = clazz.getConstructor(parameterTypes);
Constructor<?> constructorRef = CONSTRUCTORS.putIfAbsent(key, constructor);
if (constructorRef != null)
{
constructor = constructorRef;
}
}
return constructor;
}
catch (NoSuchMethodException e)
{
throw new IllegalArgumentException("Attempted to get Constructor that did not exist.", e);
}
}
private static String makeParamKey(Class<?>... parameterTypes)
{
if (parameterTypes == null || parameterTypes.length == 0)
{
return "";
}
StringBuilder builder = new StringBuilder(":");
Iterator<Class<?>> i = Arrays.stream(parameterTypes).iterator();
while (i.hasNext())
{
Class<?> param = i.next();
builder.append(param.getName());
if (i.hasNext())
{
builder.append('|');
}
}
return builder.toString();
}
/**
* Fetch the named method from the passed in Class. This method caches found methods, so it should be used
* instead of reflectively searching for the method every time. This method expects the desired method name to
* not be overloaded.
* @param clazz Class that contains the desired method.
* @param methodName String name of method to be located on the controller.
* @return Method instance found on the passed in class, or an IllegalArgumentException is thrown.
* @throws IllegalArgumentException
*/
public static Method getNonOverloadedMethod(Class<?> clazz, String methodName)
{
if (clazz == null)
{
throw new IllegalArgumentException("Attempted to call getMethod() [" + methodName + "()] on a null class.");
}
if (methodName == null)
{
throw new IllegalArgumentException("Attempted to call getMethod() with a null method name on class: " + clazz.getName());
}
StringBuilder builder = new StringBuilder(getClassLoaderName(clazz));
builder.append('.');
builder.append(clazz.getName());
builder.append('.');
builder.append(methodName);
String methodKey = builder.toString();
Method method = METHOD_MAP3.get(methodKey);
if (method == null)
{
method = getMethodNoArgs(clazz, methodName);
if (method == null)
{
throw new IllegalArgumentException("Method: " + methodName + "() is not found on class: " + clazz.getName() + ". Perhaps the method is protected, private, or misspelled?");
}
Method other = METHOD_MAP3.putIfAbsent(methodKey, method);
if (other != null)
{
method = other;
}
}
return method;
}
/**
* Reflectively find the requested method on the requested class, only matching on argument count.
*/
private static Method getMethodNoArgs(Class<?> c, String methodName)
{
Method[] methods = c.getMethods();
Method foundMethod = null;
for (Method method : methods)
{
if (methodName.equals(method.getName()))
{
if (foundMethod != null)
{
throw new IllegalArgumentException("Method: " + methodName + "() called on a class with overloaded methods - ambiguous as to which one to return. Use getMethod() that takes argument types or argument count.");
}
foundMethod = method;
}
}
return foundMethod;
}
/**
* Return the name of the class on the object, or "null" if the object is null.
* @param o Object to get the class name.
* @return String name of the class or "null"
*/
public static String getClassName(Object o)
{
return o == null ? "null" : o.getClass().getName();
}
/**
* Given a byte[] of a Java .class file (compiled Java), this code will retrieve the class name from those bytes.
* @param byteCode byte[] of compiled byte code.
* @return String name of class
* @throws Exception potential io exceptions can happen
*/
public static String getClassNameFromByteCode(byte[] byteCode) throws Exception
{
InputStream is = new ByteArrayInputStream(byteCode);
DataInputStream dis = new DataInputStream(is);
dis.readInt(); // magic number
dis.readShort(); // minor version
dis.readShort(); // major version
int cpcnt = (dis.readShort() & 0xffff) - 1;
int[] classes = new int[cpcnt];
String[] strings = new String[cpcnt];
int prevT;
int t = 0;
for (int i=0; i < cpcnt; i++)
{
prevT = t;
t = dis.read(); // tag - 1 byte
if (t == 1) // CONSTANT_Utf8
{
strings[i] = dis.readUTF();
}
else if (t == 3 || t == 4) // CONSTANT_Integer || CONSTANT_Float
{
dis.readInt(); // bytes
}
else if (t == 5 || t == 6) // CONSTANT_Long || CONSTANT_Double
{
dis.readInt(); // high_bytes
dis.readInt(); // low_bytes
i++; // All 8-byte constants take up two entries in the constant_pool table of the class file.
}
else if (t == 7) // CONSTANT_Class
{
classes[i] = dis.readShort() & 0xffff;
}
else if (t == 8) // CONSTANT_String
{
dis.readShort(); // string_index
}
else if (t == 9 || t == 10 || t == 11) // CONSTANT_Fieldref || CONSTANT_Methodref || CONSTANT_InterfaceMethodref
{
dis.readShort(); // class_index
dis.readShort(); // name_and_type_index
}
else if (t == 12) // CONSTANT_NameAndType
{
dis.readShort(); // name_index
dis.readShort(); // descriptor_index
}
else if (t == 15) // CONSTANT_MethodHandle
{
dis.readByte(); // reference_kind
dis.readShort(); // reference_index
}
else if (t == 16) // CONSTANT_MethodType
{
dis.readShort(); // descriptor_index
}
else if (t == 17 || t == 18) // CONSTANT_Dynamic || CONSTANT_InvokeDynamic
{
dis.readShort(); // bootstrap_method_attr_index
dis.readShort(); // name_and_type_index
}
else if (t == 19 || t == 20) // CONSTANT_Module || CONSTANT_Package
{
dis.readShort(); // name_index
}
else
{
throw new IllegalStateException("Byte code format exceeds JDK 17 format.");
}
}
dis.readShort(); // access flags
int thisClassIndex = dis.readShort() & 0xffff; // this_class
int stringIndex = classes[thisClassIndex - 1];
String className = strings[stringIndex - 1];
return className.replace('/', '.');
}
static String getClassLoaderName(Class<?> c)
{
ClassLoader loader = c.getClassLoader();
return loader == null ? "bootstrap" : loader.toString();
}
private static List<Field> buildDeclaredFields(final Class<?> c) {
Convention.throwIfNull(c, "class cannot be null");
Field[] fields = c.getDeclaredFields();
List<Field> list = new ArrayList<>(fields.length);
for (Field field : fields) {
if (Modifier.isStatic(field.getModifiers()) ||
(field.getDeclaringClass().isEnum() && ("internal".equals(field.getName()) || "ENUM$VALUES".equals(field.getName()))) ||
(field.getDeclaringClass().isAssignableFrom(Enum.class) && ("hash".equals(field.getName()) || "ordinal".equals(field.getName())))) {
continue;
}
list.add(field);
}
return list;
}
}