Top Description Constructors Methods
org.junit.platform.commons.support

public final Class ReflectionSupport

extends Object
Class Inheritance
Annotations
@API
status:MAINTAINED
since:1.0
Static Imports
org.apiguardian.api.API.Status.DEPRECATED, .API.Status.MAINTAINED

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.

Since
1.0
See Also
AnnotationSupport, ClassSupport, ModifierSupport, ResourceSupport

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static List<Class<?>>

Returns:

an immutable list of all such classes found; never null but potentially empty
findAllClassesInClasspathRoot
(URI
the URI for the classpath root in which to scan; never null
root
,
Predicate<Class<?>>
the class type filter; never null
classFilter
,
Predicate<String>
the class name filter; never null
classNameFilter
)

Find all classes in the supplied classpath root that match the specified classFilter and classNameFilter predicates.

public static List<Class<?>>

Returns:

an immutable list of all such classes found; never null but potentially empty
findAllClassesInModule
(String
the name of the module to scan; never null or empty
moduleName
,
Predicate<Class<?>>
the class type filter; never null
classFilter
,
Predicate<String>
the class name filter; never null
classNameFilter
)

Find all classes in the supplied moduleName that match the specified classFilter and classNameFilter predicates.

public static List<Class<?>>

Returns:

an immutable list of all such classes found; never null but potentially empty
findAllClassesInPackage
(String
the name of the base package in which to start scanning; must not be null and must be valid in terms of Java syntax
basePackageName
,
Predicate<Class<?>>
the class type filter; never null
classFilter
,
Predicate<String>
the class name filter; never null
classNameFilter
)

Find all classes in the supplied basePackageName that match the specified classFilter and classNameFilter predicates.

public static List<Resource>

Returns:

an immutable list of all such resources found; never null but potentially empty
findAllResourcesInClasspathRoot
(URI
the URI for the classpath root in which to scan; never null
root
,
Predicate<Resource>
the resource type filter; never null
resourceFilter
)
Deprecated for removal since 1.14. Please use ResourceSupport#findAllResourcesInClasspathRoot(URI, ResourceFilter) instead

Find all resources in the supplied classpath root that match the specified resourceFilter predicate.

public static List<Resource>

Returns:

an immutable list of all such resources found; never null but potentially empty
findAllResourcesInModule
(String
the name of the module to scan; never null or empty
moduleName
,
Predicate<Resource>
the resource type filter; never null
resourceFilter
)
Deprecated for removal since 1.14. Please use ResourceSupport#findAllResourcesInModule(String, ResourceFilter) instead

Find all resources in the supplied moduleName that match the specified resourceFilter predicate.

public static List<Resource>

Returns:

an immutable list of all such classes found; never null but potentially empty
findAllResourcesInPackage
(String
the name of the base package in which to start scanning; must not be null and must be valid in terms of Java syntax
basePackageName
,
Predicate<Resource>
the resource type filter; never null
resourceFilter
)
Deprecated for removal since 1.14. Please use ResourceSupport#findAllResourcesInPackage(String, ResourceFilter) instead

Find all resources in the supplied basePackageName that match the specified resourceFilter predicate.

public static List<Field>

Returns:

an immutable list of all such fields found; never null but potentially empty
findFields
(Class<?>
the class or interface in which to find the fields; never null
clazz
,
Predicate<Field>
the field filter; never null
predicate
,
HierarchyTraversalMode
the hierarchy traversal mode; never null
traversalMode
)

Find all distinct fields of the supplied class or interface that match the specified predicate.

public static Optional<Method>

Returns:

an Optional containing the method found; never null but potentially empty if no such method could be found
findMethod
(Class<?>
the class or interface in which to find the method; never null
clazz
,
String
the name of the method to find; never null or empty
methodName
,
@Nullable String
the fully qualified names of the types of parameters accepted by the method, if any, provided as a comma-separated list
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.

public static Optional<Method>

Returns:

an Optional containing the method found; never null but potentially empty if no such method could be found
findMethod
(Class<?>
the class or interface in which to find the method; never null
clazz
,
String
the name of the method to find; never null or empty
methodName
,
Class<?>...
the types of parameters accepted by the method, if any; never null
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.

public static List<Method>

Returns:

an immutable list of all such methods found; never null but potentially empty
findMethods
(Class<?>
the class or interface in which to find the methods; never null
clazz
,
Predicate<Method>
the method filter; never null
predicate
,
HierarchyTraversalMode
the hierarchy traversal mode; never null
traversalMode
)

Find all distinct methods of the supplied class or interface that match the specified predicate.

public static List<Class<?>>

Returns:

an immutable list of all such classes found; never null but potentially empty
findNestedClasses
(Class<?>
the class to be searched; never null
clazz
,
Predicate<Class<?>>
the predicate against which the list of nested classes is checked; never null
predicate
)

Find all nested classes within the supplied class, or inherited by the supplied class, that conform to the supplied predicate.

public static @Nullable Object

Returns:

the value returned by the method invocation or null if the return type is void
invokeMethod
(Method
the method to invoke; never null
method
,
@Nullable Object
the object on which to invoke the method; may be null if the method is static
target
,
@Nullable Object...
the arguments to pass to the method; never null
args
)

Invoke the supplied method, making it accessible if necessary and masking any checked exception as an unchecked exception.

public static Field

Returns:

the supplied field
makeAccessible
(Field
the field to make accessible; never null
field
)

Make the supplied field accessible via reflection.

public static <T> T

Returns:

the new instance; never null
newInstance
(Class<T>
the class to instantiate; never null
clazz
,
Object...
the arguments to pass to the constructor, none of which may be null
args
)

Create a new instance of the specified Class by invoking the constructor whose argument list matches the types of the supplied arguments.

public static Stream<Class<?>>

Returns:

a stream of all such classes found; never null but potentially empty
streamAllClassesInClasspathRoot
(URI
the URI for the classpath root in which to scan; never null
root
,
Predicate<Class<?>>
the class type filter; never null
classFilter
,
Predicate<String>
the class name filter; never null
classNameFilter
)

Find all classes in the supplied classpath root that match the specified classFilter and classNameFilter predicates.

public static Stream<Class<?>>

Returns:

a stream of all such classes found; never null but potentially empty
streamAllClassesInModule
(String
the name of the module to scan; never null or empty
moduleName
,
Predicate<Class<?>>
the class type filter; never null
classFilter
,
Predicate<String>
the class name filter; never null
classNameFilter
)

Find all classes in the supplied moduleName that match the specified classFilter and classNameFilter predicates.

public static Stream<Class<?>>

Returns:

a stream of all such classes found; never null but potentially empty
streamAllClassesInPackage
(String
the name of the base package in which to start scanning; must not be null and must be valid in terms of Java syntax
basePackageName
,
Predicate<Class<?>>
the class type filter; never null
classFilter
,
Predicate<String>
the class name filter; never null
classNameFilter
)

Find all classes in the supplied basePackageName that match the specified classFilter and classNameFilter predicates.

public static Stream<Resource>

Returns:

a stream of all such classes found; never null but potentially empty
streamAllResourcesInClasspathRoot
(URI
the URI for the classpath root in which to scan; never null
root
,
Predicate<Resource>
the resource type filter; never null
resourceFilter
)
Deprecated for removal since 1.14. Please use ResourceSupport#streamAllResourcesInClasspathRoot(URI, ResourceFilter) instead

Find all resources in the supplied classpath root that match the specified resourceFilter predicate.

public static Stream<Resource>

Returns:

a stream of all such resources found; never null but potentially empty
streamAllResourcesInModule
(String
the name of the module to scan; never null or empty
moduleName
,
Predicate<Resource>
the resource type filter; never null
resourceFilter
)
Deprecated for removal since 1.14. Please use ResourceSupport#streamAllResourcesInModule(String, ResourceFilter) instead

Find all resources in the supplied moduleName that match the specified resourceFilter predicate.

public static Stream<Resource>

Returns:

a stream of all such resources found; never null but potentially empty
streamAllResourcesInPackage
(String
the name of the base package in which to start scanning; must not be null and must be valid in terms of Java syntax
basePackageName
,
Predicate<Resource>
the resource type filter; never null
resourceFilter
)
Deprecated for removal since 1.14. Please use ResourceSupport#streamAllResourcesInPackage(String, ResourceFilter) instead

Find all resources in the supplied basePackageName that match the specified resourceFilter predicate.

public static Stream<Field>

Returns:

a stream of all such fields found; never null but potentially empty
streamFields
(Class<?>
the class or interface in which to find the fields; never null
clazz
,
Predicate<Field>
the field filter; never null
predicate
,
HierarchyTraversalMode
the hierarchy traversal mode; never null
traversalMode
)

Find all distinct fields of the supplied class or interface that match the specified predicate.

public static Stream<Method>

Returns:

a stream of all such methods found; never null but potentially empty
streamMethods
(Class<?>
the class or interface in which to find the methods; never null
clazz
,
Predicate<Method>
the method filter; never null
predicate
,
HierarchyTraversalMode
the hierarchy traversal mode; never null
traversalMode
)

Find all distinct methods of the supplied class or interface that match the specified predicate.

public static Stream<Class<?>>

Returns:

a stream of all such classes found; never null but potentially empty
streamNestedClasses
(Class<?>
the class to be searched; never null
clazz
,
Predicate<Class<?>>
the predicate against which the list of nested classes is checked; never null
predicate
)

Find all nested classes within the supplied class, or inherited by the supplied class, that conform to the supplied predicate.

private static ResourceFilter
pack-priv static List<Resource>
pack-priv static Set<Resource>
pack-priv static Stream<Resource>
public static Try<Set<Resource>>

Returns:

a successful Try containing the set of loaded resources (potentially empty) or a failed Try containing the exception in case a failure occurred while trying to list resources; never null
tryToGetResources
(String
the name of the resource to load; never null or blank
classpathResourceName
)
Deprecated for removal since 1.14. Please use ResourceSupport#tryToGetResources(String) instead

Try to get the resources for the supplied classpath resource name.

public static Try<Set<Resource>>

Returns:

a successful Try containing the set of loaded resources (potentially empty) or a failed Try containing the exception in case a failure occurred while trying to list resources; never null
tryToGetResources
(String
the name of the resource to load; never null or blank
classpathResourceName
,
ClassLoader
the ClassLoader to use; never null
classLoader
)
Deprecated for removal since 1.14. Please use ResourceSupport#tryToGetResources(String, ClassLoader) instead

Try to load the resources for the supplied classpath resource name, using the supplied ClassLoader.

public static Try<Class<?>>

Returns:

a successful Try containing the loaded class or a failed Try containing the exception if no such class could be loaded; never null
tryToLoadClass
(String
the name of the class to load; never null or blank
name
)

Try to load a class by its primitive name or fully qualified name, using the default ClassLoader.

public static Try<Class<?>>

Returns:

a successful Try containing the loaded class or a failed Try containing the exception if no such class could be loaded; never null
tryToLoadClass
(String
the name of the class to load; never null or blank
name
,
ClassLoader
the ClassLoader to use; never null
classLoader
)

Try to load a class by its primitive name or fully qualified name, using the supplied ClassLoader.

public static Try<@Nullable Object>
tryToReadFieldValue(Field
the field to read; never null
field
,
@Nullable Object
the instance from which the value is to be read; may be null for a static field
instance
)

Try to read the value of a potentially inaccessible field.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait