@API(status=MAINTAINED, since="1.0") public final class ReflectionSupport extends Object
ReflectionSupport
provides static utility methods for common
reflection tasks — for example, scanning for classes in the class-path
or module-path, loading classes, finding methods, invoking methods, etc.
TestEngine
and extension
authors are encouraged to use these supported methods in order to align with
the behavior of the JUnit Platform.
AnnotationSupport
,
ClassSupport
,
ModifierSupport
Modifier and Type | Method | Description |
---|---|---|
static List<Class<?>> |
findAllClassesInClasspathRoot(URI root,
Predicate<Class<?>> classFilter,
Predicate<String> classNameFilter) |
Find all classes in the supplied classpath
root
that match the specified classFilter and classNameFilter
predicates. |
static List<Class<?>> |
findAllClassesInModule(String moduleName,
Predicate<Class<?>> classFilter,
Predicate<String> classNameFilter) |
Find all classes in the supplied
moduleName
that match the specified classFilter and classNameFilter
predicates. |
static List<Class<?>> |
findAllClassesInPackage(String basePackageName,
Predicate<Class<?>> classFilter,
Predicate<String> classNameFilter) |
Find all classes in the supplied
basePackageName
that match the specified classFilter and classNameFilter
predicates. |
static List<Field> |
findFields(Class<?> clazz,
Predicate<Field> predicate,
HierarchyTraversalMode traversalMode) |
Find all fields of the supplied class or interface
that match the specified
predicate . |
static Optional<Method> |
findMethod(Class<?> clazz,
String methodName,
Class<?>... parameterTypes) |
Find the first
Method of the supplied class or interface that
meets the specified criteria, beginning with the specified class or
interface and traversing up the type hierarchy until such a method is
found or the type hierarchy is exhausted. |
static Optional<Method> |
findMethod(Class<?> clazz,
String methodName,
String parameterTypeNames) |
Find the first
Method of the supplied class or interface that
meets the specified criteria, beginning with the specified class or
interface and traversing up the type hierarchy until such a method is
found or the type hierarchy is exhausted. |
static List<Method> |
findMethods(Class<?> clazz,
Predicate<Method> predicate,
HierarchyTraversalMode traversalMode) |
Find all methods of the supplied class or interface
that match the specified
predicate . |
static List<Class<?>> |
findNestedClasses(Class<?> clazz,
Predicate<Class<?>> predicate) |
Find all nested classes within the given class that conform to the given
predicate.
|
static Object |
invokeMethod(Method method,
Object target,
Object... args) |
Invoke the supplied method, making it accessible if necessary and
masking any
checked exception as an unchecked exception.
|
static Optional<Class<?>> |
loadClass(String name) |
Deprecated.
Please use
tryToLoadClass(String) instead. |
static <T> T |
newInstance(Class<T> clazz,
Object... args) |
Create a new instance of the specified
Class by invoking
the constructor whose argument list matches the types of the supplied
arguments. |
static Try<Class<?>> |
tryToLoadClass(String name) |
Try to load a class by its primitive name or fully qualified name,
using the default
ClassLoader . |
static Try<Object> |
tryToReadFieldValue(Field field,
Object instance) |
Try to read the value of a potentially inaccessible field.
|
@API(status=DEPRECATED, since="1.4") @Deprecated public static Optional<Class<?>> loadClass(String name)
tryToLoadClass(String)
instead.ClassLoader
.
Class names for arrays may be specified using either the JVM's internal
String representation (e.g., [[I
for int[][]
,
[Ljava.lang.String;
for java.lang.String[]
, etc.) or
source code syntax (e.g., int[][]
, java.lang.String[]
,
etc.).
name
- the name of the class to load; never null
or blankOptional
containing the loaded class; never null
but potentially empty if no such class could be loaded@API(status=MAINTAINED, since="1.4") public static Try<Class<?>> tryToLoadClass(String name)
ClassLoader
.
Class names for arrays may be specified using either the JVM's internal
String representation (e.g., [[I
for int[][]
,
[Lava.lang.String;
for java.lang.String[]
, etc.) or
source code syntax (e.g., int[][]
, java.lang.String[]
,
etc.).
name
- the name of the class to load; never null
or blankTry
containing the loaded class or a failed
Try
containing the exception if no such class could be loaded;
never null
public static List<Class<?>> findAllClassesInClasspathRoot(URI root, Predicate<Class<?>> classFilter, Predicate<String> classNameFilter)
root
that match the specified classFilter
and classNameFilter
predicates.
The classpath scanning algorithm searches recursively in subpackages beginning with the root of the classpath.
root
- the URI for the classpath root in which to scan; never
null
classFilter
- the class type filter; never null
classNameFilter
- the class name filter; never null
null
but potentially emptyfindAllClassesInPackage(String, Predicate, Predicate)
,
findAllClassesInModule(String, Predicate, Predicate)
public static List<Class<?>> findAllClassesInPackage(String basePackageName, Predicate<Class<?>> classFilter, Predicate<String> classNameFilter)
basePackageName
that match the specified classFilter
and classNameFilter
predicates.
The classpath scanning algorithm searches recursively in subpackages beginning within the supplied base package.
basePackageName
- the name of the base package in which to start
scanning; must not be null
and must be valid in terms of Java
syntaxclassFilter
- the class type filter; never null
classNameFilter
- the class name filter; never null
null
but potentially emptyfindAllClassesInClasspathRoot(URI, Predicate, Predicate)
,
findAllClassesInModule(String, Predicate, Predicate)
public static List<Class<?>> findAllClassesInModule(String moduleName, Predicate<Class<?>> classFilter, Predicate<String> classNameFilter)
moduleName
that match the specified classFilter
and classNameFilter
predicates.
The module-path scanning algorithm searches recursively in all packages contained in the module.
moduleName
- the name of the module to scan; never null
or
emptyclassFilter
- the class type filter; never null
classNameFilter
- the class name filter; never null
null
but potentially emptyfindAllClassesInClasspathRoot(URI, Predicate, Predicate)
,
findAllClassesInPackage(String, Predicate, Predicate)
public static <T> T newInstance(Class<T> clazz, Object... args)
Class
by invoking
the constructor whose argument list matches the types of the supplied
arguments.
The constructor will be made accessible if necessary, and any checked exception will be masked as an unchecked exception.
clazz
- the class to instantiate; never null
args
- the arguments to pass to the constructor, none of which may
be null
null
ExceptionUtils.throwAsUncheckedException(Throwable)
public static Object invokeMethod(Method method, Object target, Object... args)
method
- the method to invoke; never null
target
- the object on which to invoke the method; may be
null
if the method is static
args
- the arguments to pass to the methodnull
if the return type is void
ExceptionUtils.throwAsUncheckedException(Throwable)
@API(status=MAINTAINED, since="1.4") public static List<Field> findFields(Class<?> clazz, Predicate<Field> predicate, HierarchyTraversalMode traversalMode)
predicate
.
Fields declared in the same class or interface will be ordered using an algorithm that is deterministic but intentionally nonobvious.
The results will not contain fields that are hidden or synthetic.
clazz
- the class or interface in which to find the fields; never null
predicate
- the field filter; never null
traversalMode
- the hierarchy traversal mode; never null
null
but potentially empty@API(status=MAINTAINED, since="1.4") public static Try<Object> tryToReadFieldValue(Field field, Object instance)
If an exception occurs while reading the field, a failed Try
is returned that contains the corresponding exception.
field
- the field to read; never null
instance
- the instance from which the value is to be read; may
be null
for a static fieldpublic static Optional<Method> findMethod(Class<?> clazz, String methodName, String parameterTypeNames)
Method
of the supplied class or interface that
meets the specified criteria, beginning with the specified class or
interface and traversing up the type hierarchy until such a method is
found or the type hierarchy is exhausted.
The algorithm does not search for methods in Object
.
clazz
- the class or interface in which to find the method; never null
methodName
- the name of the method to find; never null
or emptyparameterTypeNames
- the fully qualified names of the types of parameters
accepted by the method, if any, provided as a comma-separated listOptional
containing the method found; never null
but potentially empty if no such method could be foundfindMethod(Class, String, Class...)
public static Optional<Method> findMethod(Class<?> clazz, String methodName, Class<?>... parameterTypes)
Method
of the supplied class or interface that
meets the specified criteria, beginning with the specified class or
interface and traversing up the type hierarchy until such a method is
found or the type hierarchy is exhausted.
The algorithm does not search for methods in Object
.
clazz
- the class or interface in which to find the method; never null
methodName
- the name of the method to find; never null
or emptyparameterTypes
- the types of parameters accepted by the method, if any;
never null
Optional
containing the method found; never null
but potentially empty if no such method could be foundfindMethod(Class, String, String)
public static List<Method> findMethods(Class<?> clazz, Predicate<Method> predicate, HierarchyTraversalMode traversalMode)
predicate
.
The results will not contain instance methods that are overridden
or static
methods that are hidden.
If you're are looking for methods annotated with a certain annotation
type, consider using
AnnotationSupport.findAnnotatedMethods(Class, Class, HierarchyTraversalMode)
.
clazz
- the class or interface in which to find the methods; never null
predicate
- the method filter; never null
traversalMode
- the hierarchy traversal mode; never null
null
but potentially emptypublic static List<Class<?>> findNestedClasses(Class<?> clazz, Predicate<Class<?>> predicate)
clazz
- the class to be searched; never null
predicate
- the predicate against which the list of nested classes is
checked; never null
null
but potentially emptyCopyright © 2019. All rights reserved.