Class ReflectionSupport
- java.lang.Object
-
- org.junit.platform.commons.support.ReflectionSupport
-
@API(status=MAINTAINED, since="1.0") public final class ReflectionSupport extends java.lang.Object
Common reflection and classpath scanning support.- Since:
- 1.0
- See Also:
AnnotationSupport
,ClassSupport
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.util.List<java.lang.Class<?>>
findAllClassesInClasspathRoot(java.net.URI root, java.util.function.Predicate<java.lang.Class<?>> classFilter, java.util.function.Predicate<java.lang.String> classNameFilter)
Find all classes in the supplied classpathroot
that match the specifiedclassFilter
andclassNameFilter
predicates.static java.util.List<java.lang.Class<?>>
findAllClassesInModule(java.lang.String moduleName, java.util.function.Predicate<java.lang.Class<?>> classFilter, java.util.function.Predicate<java.lang.String> classNameFilter)
Find all classes in the suppliedmoduleName
that match the specifiedclassFilter
andclassNameFilter
predicates.static java.util.List<java.lang.Class<?>>
findAllClassesInPackage(java.lang.String basePackageName, java.util.function.Predicate<java.lang.Class<?>> classFilter, java.util.function.Predicate<java.lang.String> classNameFilter)
Find all classes in the suppliedbasePackageName
that match the specifiedclassFilter
andclassNameFilter
predicates.static java.util.Optional<java.lang.reflect.Method>
findMethod(java.lang.Class<?> clazz, java.lang.String methodName, java.lang.Class<?>... parameterTypes)
Find the firstMethod
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 java.util.Optional<java.lang.reflect.Method>
findMethod(java.lang.Class<?> clazz, java.lang.String methodName, java.lang.String parameterTypeNames)
Find the firstMethod
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 java.util.List<java.lang.reflect.Method>
findMethods(java.lang.Class<?> clazz, java.util.function.Predicate<java.lang.reflect.Method> predicate, HierarchyTraversalMode traversalMode)
Find all methods of the supplied class or interface that match the specifiedpredicate
.static java.util.List<java.lang.Class<?>>
findNestedClasses(java.lang.Class<?> clazz, java.util.function.Predicate<java.lang.Class<?>> predicate)
Find all nested classes within the given class that conform to the given predicate.static java.lang.Object
invokeMethod(java.lang.reflect.Method method, java.lang.Object target, java.lang.Object... args)
Invoke the supplied method, making it accessible if necessary and masking any checked exception as an unchecked exception.static java.util.Optional<java.lang.Class<?>>
loadClass(java.lang.String name)
Load a class by its primitive name or fully qualified name, using the defaultClassLoader
.static <T> T
newInstance(java.lang.Class<T> clazz, java.lang.Object... args)
Create a new instance of the specifiedClass
by invoking the constructor whose argument list matches the types of the supplied arguments.
-
-
-
Method Detail
-
loadClass
public static java.util.Optional<java.lang.Class<?>> loadClass(java.lang.String name)
Load a class by its primitive name or fully qualified name, using the defaultClassLoader
.Class names for arrays may be specified using either the JVM's internal String representation (e.g.,
[[I
forint[][]
,[Lava.lang.String;
forjava.lang.String[]
, etc.) or source code syntax (e.g.,int[][]
,java.lang.String[]
, etc.).- Parameters:
name
- the name of the class to load; nevernull
or blank- Returns:
- an
Optional
containing the loaded class; nevernull
but potentially empty if no such class could be loaded
-
findAllClassesInClasspathRoot
public static java.util.List<java.lang.Class<?>> findAllClassesInClasspathRoot(java.net.URI root, java.util.function.Predicate<java.lang.Class<?>> classFilter, java.util.function.Predicate<java.lang.String> classNameFilter)
Find all classes in the supplied classpathroot
that match the specifiedclassFilter
andclassNameFilter
predicates.The classpath scanning algorithm searches recursively in subpackages beginning with the root of the classpath.
- Parameters:
root
- the URI for the classpath root in which to scanclassFilter
- the class type filter; nevernull
classNameFilter
- the class name filter; nevernull
- Returns:
- an immutable list of all such classes found; never
null
but potentially empty - See Also:
findAllClassesInPackage(String, Predicate, Predicate)
,findAllClassesInModule(String, Predicate, Predicate)
-
findAllClassesInPackage
public static java.util.List<java.lang.Class<?>> findAllClassesInPackage(java.lang.String basePackageName, java.util.function.Predicate<java.lang.Class<?>> classFilter, java.util.function.Predicate<java.lang.String> classNameFilter)
Find all classes in the suppliedbasePackageName
that match the specifiedclassFilter
andclassNameFilter
predicates.The classpath scanning algorithm searches recursively in subpackages beginning within the supplied base package.
- Parameters:
basePackageName
- the name of the base package in which to start scanningclassFilter
- the class type filter; nevernull
classNameFilter
- the class name filter; nevernull
- Returns:
- an immutable list of all such classes found; never
null
but potentially empty - See Also:
findAllClassesInClasspathRoot(URI, Predicate, Predicate)
,findAllClassesInModule(String, Predicate, Predicate)
-
findAllClassesInModule
public static java.util.List<java.lang.Class<?>> findAllClassesInModule(java.lang.String moduleName, java.util.function.Predicate<java.lang.Class<?>> classFilter, java.util.function.Predicate<java.lang.String> classNameFilter)
Find all classes in the suppliedmoduleName
that match the specifiedclassFilter
andclassNameFilter
predicates.The module-path scanning algorithm searches recursively in all packages contained in the module.
- Parameters:
moduleName
- the name of the module to scanclassFilter
- the class type filter; nevernull
classNameFilter
- the class name filter; nevernull
- Returns:
- an immutable list of all such classes found; never
null
but potentially empty - Since:
- 1.1.1
- See Also:
findAllClassesInClasspathRoot(URI, Predicate, Predicate)
,findAllClassesInPackage(String, Predicate, Predicate)
-
newInstance
public static <T> T newInstance(java.lang.Class<T> clazz, java.lang.Object... args)
Create a new instance of the specifiedClass
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.
- Parameters:
clazz
- the class to instantiate; nevernull
args
- the arguments to pass to the constructor none of which may benull
- Returns:
- the new instance
- See Also:
ExceptionUtils.throwAsUncheckedException(Throwable)
-
invokeMethod
public static java.lang.Object invokeMethod(java.lang.reflect.Method method, java.lang.Object target, java.lang.Object... args)
Invoke the supplied method, making it accessible if necessary and masking any checked exception as an unchecked exception.- Parameters:
method
- the method to invoke; nevernull
target
- the object on which to invoke the method; may benull
if the method isstatic
args
- the arguments to pass to the method- Returns:
- the value returned by the method invocation or
null
if the return type isvoid
- See Also:
ExceptionUtils.throwAsUncheckedException(Throwable)
-
findMethod
public static java.util.Optional<java.lang.reflect.Method> findMethod(java.lang.Class<?> clazz, java.lang.String methodName, java.lang.String parameterTypeNames)
Find the firstMethod
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
.- Parameters:
clazz
- the class or interface in which to find the method; nevernull
methodName
- the name of the method to find; nevernull
or emptyparameterTypeNames
- the fully qualified names of the types of parameters accepted by the method, if any, provided as a comma-separated list- Returns:
- an
Optional
containing the method found; nevernull
but potentially empty if no such method could be found - See Also:
findMethod(Class, String, Class...)
-
findMethod
public static java.util.Optional<java.lang.reflect.Method> findMethod(java.lang.Class<?> clazz, java.lang.String methodName, java.lang.Class<?>... parameterTypes)
Find the firstMethod
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
.- Parameters:
clazz
- the class or interface in which to find the method; nevernull
methodName
- the name of the method to find; nevernull
or emptyparameterTypes
- the types of parameters accepted by the method, if any; nevernull
- Returns:
- an
Optional
containing the method found; nevernull
but potentially empty if no such method could be found - See Also:
findMethod(Class, String, String)
-
findMethods
public static java.util.List<java.lang.reflect.Method> findMethods(java.lang.Class<?> clazz, java.util.function.Predicate<java.lang.reflect.Method> predicate, HierarchyTraversalMode traversalMode)
Find all methods of the supplied class or interface that match the specifiedpredicate
.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)
.- Parameters:
clazz
- the class or interface in which to find the methods; nevernull
predicate
- the method filter; nevernull
traversalMode
- the hierarchy traversal mode; nevernull
- Returns:
- an immutable list of all such methods found; never
null
but potentially empty
-
findNestedClasses
public static java.util.List<java.lang.Class<?>> findNestedClasses(java.lang.Class<?> clazz, java.util.function.Predicate<java.lang.Class<?>> predicate)
Find all nested classes within the given class that conform to the given predicate.- Parameters:
clazz
- the class to be searched; nevernull
predicate
- the predicate against which the list of nested classes is checked; nevernull
- Returns:
- an immutable list of all such classes found; never
null
but potentially empty
-
-