/
ReflectionUtils.java
710 lines (609 loc) · 24.6 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
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
/*
* Copyright 2015-2016 the original author or authors.
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v1.0 which
* accompanies this distribution and is available at
*
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.junit.platform.commons.util;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet;
import static org.junit.platform.commons.meta.API.Usage.Internal;
import java.io.File;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.junit.platform.commons.JUnitException;
import org.junit.platform.commons.meta.API;
/**
* Collection of utilities for working with the Java reflection APIs.
*
* <h3>DISCLAIMER</h3>
*
* <p>These utilities are intended solely for usage within the JUnit framework
* itself. <strong>Any usage by external parties is not supported.</strong>
* Use at your own risk!
*
* @since 1.0
*/
@API(Internal)
public final class ReflectionUtils {
///CLOVER:OFF
private ReflectionUtils() {
/* no-op */
}
///CLOVER:ON
public enum MethodSortOrder {
HierarchyDown, HierarchyUp
}
// Pattern: [fully qualified class name]#[methodName]((comma-separated list of parameter type names))
private static final Pattern FULLY_QUALIFIED_METHOD_NAME_PATTERN = Pattern.compile("(.+)#([^()]+?)(\\((.*)\\))?");
private static final Class<?>[] EMPTY_CLASS_ARRAY = new Class<?>[0];
private static final ClasspathScanner classpathScanner = new ClasspathScanner(
ReflectionUtils::getDefaultClassLoader, ReflectionUtils::loadClass);
private static final Map<String, Class<?>> primitiveNameToTypeMap;
private static final Map<Class<?>, Class<?>> primitiveToWrapperMap;
static {
Map<String, Class<?>> primitiveTypes = new HashMap<>(16);
primitiveTypes.put(boolean.class.getName(), boolean.class);
primitiveTypes.put(byte.class.getName(), byte.class);
primitiveTypes.put(char.class.getName(), char.class);
primitiveTypes.put(short.class.getName(), short.class);
primitiveTypes.put(int.class.getName(), int.class);
primitiveTypes.put(long.class.getName(), long.class);
primitiveTypes.put(float.class.getName(), float.class);
primitiveTypes.put(double.class.getName(), double.class);
primitiveTypes.put(boolean[].class.getName(), boolean[].class);
primitiveTypes.put(byte[].class.getName(), byte[].class);
primitiveTypes.put(char[].class.getName(), char[].class);
primitiveTypes.put(short[].class.getName(), short[].class);
primitiveTypes.put(int[].class.getName(), int[].class);
primitiveTypes.put(long[].class.getName(), long[].class);
primitiveTypes.put(float[].class.getName(), float[].class);
primitiveTypes.put(double[].class.getName(), double[].class);
primitiveNameToTypeMap = Collections.unmodifiableMap(primitiveTypes);
Map<Class<?>, Class<?>> primitiveToWrapper = new HashMap<>(8);
primitiveToWrapper.put(boolean.class, Boolean.class);
primitiveToWrapper.put(byte.class, Byte.class);
primitiveToWrapper.put(char.class, Character.class);
primitiveToWrapper.put(short.class, Short.class);
primitiveToWrapper.put(int.class, Integer.class);
primitiveToWrapper.put(long.class, Long.class);
primitiveToWrapper.put(float.class, Float.class);
primitiveToWrapper.put(double.class, Double.class);
primitiveToWrapperMap = Collections.unmodifiableMap(primitiveToWrapper);
}
public static ClassLoader getDefaultClassLoader() {
try {
return Thread.currentThread().getContextClassLoader();
}
catch (Throwable ex) {
/* ignore */
}
return ClassLoader.getSystemClassLoader();
}
public static boolean isPublic(Class<?> clazz) {
return Modifier.isPublic(clazz.getModifiers());
}
public static boolean isPublic(Member member) {
return Modifier.isPublic(member.getModifiers());
}
public static boolean isPrivate(Class<?> clazz) {
return Modifier.isPrivate(clazz.getModifiers());
}
public static boolean isPrivate(Member member) {
return Modifier.isPrivate(member.getModifiers());
}
public static boolean isAbstract(Class<?> clazz) {
return Modifier.isAbstract(clazz.getModifiers());
}
public static boolean isAbstract(Member member) {
return Modifier.isAbstract(member.getModifiers());
}
public static boolean isStatic(Class<?> clazz) {
return Modifier.isStatic(clazz.getModifiers());
}
public static boolean isStatic(Member member) {
return Modifier.isStatic(member.getModifiers());
}
/**
* Determine if the supplied object is an array.
*
* @param obj the object to test; potentially {@code null}
* @return {@code true} if the object is an array
*/
public static boolean isArray(Object obj) {
return (obj != null && obj.getClass().isArray());
}
/**
* Determine if the supplied object can be assigned to the supplied type
* for the purpose of reflective method invocations.
*
* <p>In contrast to {@link Class#isInstance(Object)}, this method returns
* {@code true} if the supplied type represents a primitive type whose
* wrapper matches the supplied object's type.
*
* <p>Returns {@code true} if the supplied object is {@code null} and the
* supplied type does not represent a primitive type.
*
* @param obj the object to test for assignment compatibility; potentially {@code null}
* @param type the type to check against; never {@code null}
* @return {@code true} if the object is assignment compatible
* @see Class#isInstance(Object)
* @see Class#isAssignableFrom(Class)
*/
public static boolean isAssignableTo(Object obj, Class<?> type) {
Preconditions.notNull(type, "type must not be null");
if (obj == null) {
return !type.isPrimitive();
}
if (type.isInstance(obj)) {
return true;
}
if (type.isPrimitive()) {
return primitiveToWrapperMap.get(type) == obj.getClass();
}
return false;
}
/**
* Get the wrapper type for the supplied primitive type.
*
* @param type the primitive type for which to retrieve the wrapper type
* @return the corresponding wrapper type or {@code null} if the
* supplied type is {@code null} or not a primitive type
*/
public static Class<?> getWrapperType(Class<?> type) {
return primitiveToWrapperMap.get(type);
}
/**
* Create a new instance of the specified {@link Class} by invoking
* the constructor whose argument list matches the types of the supplied
* arguments.
*
* <p>The constructor will be made accessible if necessary, and any checked
* exception will be {@linkplain ExceptionUtils#throwAsUncheckedException masked}
* as an unchecked exception.
*
* @param clazz the class to instantiate; never {@code null}
* @param args the arguments to pass to the constructor none of which may be {@code null}
* @return the new instance
* @see #newInstance(Constructor, Object...)
* @see ExceptionUtils#throwAsUncheckedException(Throwable)
*/
public static <T> T newInstance(Class<T> clazz, Object... args) {
Preconditions.notNull(clazz, "class must not be null");
Preconditions.notNull(args, "none of the arguments may be null");
try {
Class<?>[] parameterTypes = Arrays.stream(args).map(Object::getClass).toArray(Class[]::new);
return newInstance(clazz.getDeclaredConstructor(parameterTypes), args);
}
catch (Throwable t) {
throw ExceptionUtils.throwAsUncheckedException(getUnderlyingCause(t));
}
}
/**
* Create a new instance of type {@code T} by invoking the supplied constructor
* with the supplied arguments.
*
* <p>The constructor will be made accessible if necessary, and any checked
* exception will be {@linkplain ExceptionUtils#throwAsUncheckedException masked}
* as an unchecked exception.
*
* @param constructor the constructor to invoke; never {@code null}
* @param args the arguments to pass to the constructor
* @return the new instance; never {@code null}
* @see #newInstance(Class, Object...)
* @see ExceptionUtils#throwAsUncheckedException(Throwable)
*/
public static <T> T newInstance(Constructor<T> constructor, Object... args) {
Preconditions.notNull(constructor, "constructor must not be null");
try {
return makeAccessible(constructor).newInstance(args);
}
catch (Throwable t) {
throw ExceptionUtils.throwAsUncheckedException(getUnderlyingCause(t));
}
}
/**
* Invoke the supplied method, making it accessible if necessary and
* {@linkplain ExceptionUtils#throwAsUncheckedException masking} any
* checked exception as an unchecked exception.
*
* @param method the method to invoke; never {@code null}
* @param target the object on which to invoke the method; may be
* {@code null} if the method is {@code static}
* @param args the arguments to pass to the method
* @return the value returned by the method invocation or {@code null}
* if the return type is {@code void}
* @see ExceptionUtils#throwAsUncheckedException(Throwable)
*/
public static Object invokeMethod(Method method, Object target, Object... args) {
Preconditions.notNull(method, "method must not be null");
Preconditions.condition((target != null || isStatic(method)),
() -> String.format("Cannot invoke non-static method [%s] on a null target.", method.toGenericString()));
try {
return makeAccessible(method).invoke(target, args);
}
catch (Throwable t) {
throw ExceptionUtils.throwAsUncheckedException(getUnderlyingCause(t));
}
}
/**
* Load a class by its <em>fully qualified name</em>.
* @param name the fully qualified name of the class to load; never
* {@code null} or blank
* @see #loadClass(String, ClassLoader)
*/
public static Optional<Class<?>> loadClass(String name) {
return loadClass(name, getDefaultClassLoader());
}
/**
* Load a class by its <em>fully qualified name</em>, using the supplied
* {@link ClassLoader}.
* @param name the fully qualified name of the class to load; never
* {@code null} or blank
* @param classLoader the {@code ClassLoader} to use; never {@code null}
* @see #loadClass(String)
*/
public static Optional<Class<?>> loadClass(String name, ClassLoader classLoader) {
Preconditions.notBlank(name, "class name must not be null or blank");
Preconditions.notNull(classLoader, "ClassLoader must not be null");
name = name.trim();
if (primitiveNameToTypeMap.containsKey(name)) {
return Optional.of(primitiveNameToTypeMap.get(name));
}
try {
// Object array such as: [Ljava.lang.String;
if (name.startsWith("[L") && name.endsWith(";")) {
Class<?> componentType = classLoader.loadClass(name.substring(2, name.length() - 1));
return Optional.of(Array.newInstance(componentType, 0).getClass());
}
return Optional.of(classLoader.loadClass(name));
}
catch (ClassNotFoundException ex) {
return Optional.empty();
}
}
/**
* Load a method by its <em>fully qualified name</em>.
*
* <p>The following formats are supported.
*
* <ul>
* <li>{@code [fully qualified class name]#[methodName]}</li>
* <li>{@code [fully qualified class name]#[methodName](parameter type list)}
* <ul><li>The <em>parameter type list</em> is a comma-separated list of
* fully qualified class names for the types of parameters accepted by
* the method.</li></ul>
* </li>
* </ul>
*
* <h3>Examples</h3>
*
* <table border="1">
* <tr><th>Method</th><th>Fully Qualified Method Name</th></tr>
* <tr><td>{@link String#chars()}</td><td>{@code java.lang.String#chars}</td></tr>
* <tr><td>{@link String#chars()}</td><td>{@code java.lang.String#chars()}</td></tr>
* <tr><td>{@link String#equalsIgnoreCase(String)}</td><td>{@code java.lang.String#equalsIgnoreCase(java.lang.String)}</td></tr>
* <tr><td>{@link String#substring(int, int)}</td><td>{@code java.lang.String#substring(int, int)}</td></tr>
* </table>
*
* @param fullyQualifiedMethodName the fully qualified name of the method to load;
* never {@code null} or blank
* @return an {@code Optional} containing the method; never {@code null} but
* potentially empty
*/
public static Optional<Method> loadMethod(String fullyQualifiedMethodName) {
Preconditions.notBlank(fullyQualifiedMethodName, "fully qualified method name must not be null or blank");
String fqmn = fullyQualifiedMethodName.trim();
Matcher matcher = FULLY_QUALIFIED_METHOD_NAME_PATTERN.matcher(fqmn);
Preconditions.condition(matcher.matches(),
() -> String.format("Fully qualified method name [%s] does not match pattern [%s]", fqmn,
FULLY_QUALIFIED_METHOD_NAME_PATTERN));
String className = matcher.group(1);
String methodName = matcher.group(2);
// Note: group #3 includes the parameter types enclosed in parentheses;
// group #4 contains the actual parameter types.
String parameterTypeNames = matcher.group(4);
Optional<Class<?>> classOptional = loadClass(className);
if (classOptional.isPresent()) {
try {
return findMethod(classOptional.get(), methodName.trim(), parameterTypeNames);
}
catch (Exception ex) {
/* ignore */
}
}
return Optional.empty();
}
private static Optional<Object> getOuterInstance(Object inner) {
// This is risky since it depends on the name of the field which is nowhere guaranteed
// but has been stable so far in all JDKs
// @formatter:off
return Arrays.stream(inner.getClass().getDeclaredFields())
.filter(field -> field.getName().startsWith("this$"))
.findFirst()
.map(field -> {
try {
return makeAccessible(field).get(inner);
}
catch (SecurityException | IllegalArgumentException | IllegalAccessException ex) {
return Optional.empty();
}
});
// @formatter:on
}
public static Optional<Object> getOuterInstance(Object inner, Class<?> targetType) {
Preconditions.notNull(inner, "inner object must not be null");
Preconditions.notNull(targetType, "targetType must not be null");
if (targetType.isInstance(inner)) {
return Optional.of(inner);
}
Optional<Object> candidate = getOuterInstance(inner);
if (candidate.isPresent()) {
return getOuterInstance(candidate.get(), targetType);
}
return Optional.empty();
}
/**
* Determine if the supplied package name refers to a package that is present
* in the classpath.
*
* <p>The default package is represented by an empty string ({@code ""}).
*
* @param packageName the package name to check; never {@code null} and never
* containing whitespace only
*/
public static boolean isPackage(String packageName) {
return classpathScanner.isPackage(packageName);
}
public static Set<File> getAllClasspathRootDirectories() {
// This is quite a hack, since sometimes the classpath is quite different
String fullClassPath = System.getProperty("java.class.path");
// @formatter:off
return Arrays.stream(fullClassPath.split(File.pathSeparator))
.filter(part -> !part.endsWith(".jar"))
.map(File::new)
.filter(File::isDirectory)
.collect(toSet());
// @formatter:on
}
public static List<Class<?>> findAllClassesInClasspathRoot(Path root, Predicate<Class<?>> classTester) {
return classpathScanner.scanForClassesInClasspathRoot(root, classTester);
}
public static List<Class<?>> findAllClassesInPackage(String basePackageName, Predicate<Class<?>> classTester) {
return classpathScanner.scanForClassesInPackage(basePackageName, classTester);
}
public static List<Class<?>> findNestedClasses(Class<?> clazz, Predicate<Class<?>> predicate) {
Preconditions.notNull(clazz, "Class must not be null");
Preconditions.notNull(predicate, "predicate must not be null");
return Arrays.stream(clazz.getDeclaredClasses()).filter(predicate).collect(toList());
}
/**
* Get the sole declared {@link Constructor} for the supplied class.
*
* <p>Throws a {@link PreconditionViolationException} if the supplied
* class declares more than one constructor.
*
* @param clazz the class to get the constructor for
* @return the sole declared constructor; never {@code null}
* @see Class#getDeclaredConstructors()
*/
@SuppressWarnings("unchecked")
public static <T> Constructor<T> getDeclaredConstructor(Class<T> clazz) {
Preconditions.notNull(clazz, "Class must not be null");
try {
Constructor<?>[] constructors = clazz.getDeclaredConstructors();
Preconditions.condition(constructors.length == 1,
() -> String.format("Class [%s] must declare a single constructor", clazz.getName()));
return (Constructor<T>) constructors[0];
}
catch (Throwable t) {
throw ExceptionUtils.throwAsUncheckedException(getUnderlyingCause(t));
}
}
public static Optional<Method> getMethod(Class<?> clazz, String methodName, Class<?>... parameterTypes) {
Preconditions.notNull(clazz, "Class must not be null");
Preconditions.notBlank(methodName, "method name must not be null or empty");
try {
return Optional.ofNullable(clazz.getMethod(methodName, parameterTypes));
}
catch (Throwable t) {
throw ExceptionUtils.throwAsUncheckedException(getUnderlyingCause(t));
}
}
public static Optional<Method> findMethod(Class<?> clazz, String methodName, String parameterTypeNames) {
return findMethod(clazz, methodName, resolveParameterTypes(parameterTypeNames));
}
private static Class<?>[] resolveParameterTypes(String parameterTypeNames) {
if (StringUtils.isBlank(parameterTypeNames)) {
return EMPTY_CLASS_ARRAY;
}
// @formatter:off
return Arrays.stream(parameterTypeNames.split(","))
.map(ReflectionUtils::loadRequiredParameterType)
.toArray(Class[]::new);
// @formatter:on
}
private static Class<?> loadRequiredParameterType(String typeName) {
return loadClass(typeName).orElseThrow(
() -> new JUnitException(String.format("Failed to load parameter type [%s]", typeName)));
}
public static Optional<Method> findMethod(Class<?> clazz, String methodName, Class<?>... parameterTypes) {
Preconditions.notNull(clazz, "Class must not be null");
Preconditions.notBlank(methodName, "method name must not be null or empty");
Predicate<Method> nameAndParameterTypesMatch = (method -> method.getName().equals(methodName)
&& Arrays.equals(method.getParameterTypes(), parameterTypes));
List<Method> candidates = findMethods(clazz, nameAndParameterTypesMatch);
return (!candidates.isEmpty() ? Optional.of(candidates.get(0)) : Optional.empty());
}
public static List<Method> findMethods(Class<?> clazz, Predicate<Method> predicate) {
return findMethods(clazz, predicate, MethodSortOrder.HierarchyDown);
}
public static List<Method> findMethods(Class<?> clazz, Predicate<Method> predicate, MethodSortOrder sortOrder) {
Preconditions.notNull(clazz, "Class must not be null");
Preconditions.notNull(predicate, "predicate must not be null");
Preconditions.notNull(sortOrder, "MethodSortOrder must not be null");
// @formatter:off
return findAllMethodsInHierarchy(clazz, sortOrder).stream()
.filter(predicate)
.collect(toList());
// @formatter:on
}
/**
* Return all methods in superclass hierarchy except from Object.
*/
private static List<Method> findAllMethodsInHierarchy(Class<?> clazz, MethodSortOrder sortOrder) {
Preconditions.notNull(clazz, "Class must not be null");
Preconditions.notNull(sortOrder, "MethodSortOrder must not be null");
// TODO [#333] Determine if we need to support bridged methods.
List<Method> localMethods = Arrays.asList(clazz.getDeclaredMethods());
// @formatter:off
List<Method> superclassMethods = getSuperclassMethods(clazz, sortOrder).stream()
.filter(method -> !isMethodShadowedByLocalMethods(method, localMethods))
.collect(toList());
// @formatter:on
// @formatter:off
List<Method> interfaceMethods = getInterfaceMethods(clazz, sortOrder).stream()
.filter(method -> !isMethodShadowedByLocalMethods(method, localMethods))
.collect(toList());
// @formatter:on
List<Method> methods = new ArrayList<>();
if (sortOrder == MethodSortOrder.HierarchyDown) {
methods.addAll(superclassMethods);
methods.addAll(interfaceMethods);
}
methods.addAll(localMethods);
if (sortOrder == MethodSortOrder.HierarchyUp) {
methods.addAll(interfaceMethods);
methods.addAll(superclassMethods);
}
return methods;
}
/**
* Read the value of a potentially inaccessible field.
*
* <p>If the field does not exist, an exception occurs while reading it, or
* the value of the field is {@code null}, an empty {@link Optional} is
* returned.
*
* @param clazz the class where the field is declared; never {@code null}
* @param fieldName the name of the field; never {@code null} or empty
* @param instance the instance from where the value is to be read; may
* be {@code null} for a static field
*/
public static <T> Optional<Object> readFieldValue(Class<T> clazz, String fieldName, T instance) {
Preconditions.notNull(clazz, "Class must not be null");
Preconditions.notBlank(fieldName, "fieldName must not be null or empty");
try {
Field field = makeAccessible(clazz.getDeclaredField(fieldName));
return Optional.ofNullable(field.get(instance));
}
catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
return Optional.empty();
}
}
private static List<Method> getInterfaceMethods(Class<?> clazz, MethodSortOrder sortOrder) {
Preconditions.notNull(clazz, "Class must not be null");
Preconditions.notNull(sortOrder, "MethodSortOrder must not be null");
List<Method> allInterfaceMethods = new ArrayList<>();
for (Class<?> ifc : clazz.getInterfaces()) {
List<Method> localMethods = Arrays.stream(ifc.getDeclaredMethods()).filter(Method::isDefault).collect(
toList());
// @formatter:off
List<Method> subInterfaceMethods = getInterfaceMethods(ifc, sortOrder).stream()
.filter(method -> !isMethodShadowedByLocalMethods(method, localMethods))
.collect(toList());
// @formatter:on
if (sortOrder == MethodSortOrder.HierarchyDown) {
allInterfaceMethods.addAll(subInterfaceMethods);
}
allInterfaceMethods.addAll(localMethods);
if (sortOrder == MethodSortOrder.HierarchyUp) {
allInterfaceMethods.addAll(subInterfaceMethods);
}
}
return allInterfaceMethods;
}
private static List<Method> getSuperclassMethods(Class<?> clazz, MethodSortOrder sortOrder) {
Class<?> superclass = clazz.getSuperclass();
if (superclass == null || superclass == Object.class) {
return Collections.emptyList();
}
return findAllMethodsInHierarchy(superclass, sortOrder);
}
private static boolean isMethodShadowedByLocalMethods(Method method, List<Method> localMethods) {
return localMethods.stream().anyMatch(local -> isMethodShadowedBy(method, local));
}
private static boolean isMethodShadowedBy(Method upper, Method lower) {
if (!lower.getName().equals(upper.getName())) {
return false;
}
return Arrays.equals(lower.getParameterTypes(), upper.getParameterTypes());
}
private static <T extends AccessibleObject> T makeAccessible(T object) {
if (!object.isAccessible()) {
object.setAccessible(true);
}
return object;
}
/**
* Get the underlying cause of the supplied {@link Throwable}.
*
* <p>If the supplied {@code Throwable} is an instance of
* {@link InvocationTargetException}, this method will be invoked
* recursively with the underlying
* {@linkplain InvocationTargetException#getTargetException() target
* exception}; otherwise, this method simply returns the supplied
* {@code Throwable}.
*/
private static Throwable getUnderlyingCause(Throwable t) {
if (t instanceof InvocationTargetException) {
return getUnderlyingCause(((InvocationTargetException) t).getTargetException());
}
return t;
}
/**
* Return all classes and interfaces that can be used as assignment types
* for instances of the specified {@link Class}, including itself.
*
* @param clazz the {@code Class} to lookup
* @see Class#isAssignableFrom
*/
public static Set<Class<?>> getAllAssignmentCompatibleClasses(Class<?> clazz) {
Preconditions.notNull(clazz, "class must not be null");
Set<Class<?>> result = new LinkedHashSet<>();
getAllAssignmentCompatibleClasses(clazz, result);
return result;
}
private static void getAllAssignmentCompatibleClasses(Class<?> clazz, Set<Class<?>> result) {
for (Class<?> current = clazz; current != null; current = current.getSuperclass()) {
result.add(current);
for (Class<?> interfaceClass : current.getInterfaces()) {
if (!result.contains(interfaceClass)) {
getAllAssignmentCompatibleClasses(interfaceClass, result);
}
}
}
}
}