Class ReflectionSupport
- java.lang.Object
-
- org.junit.platform.commons.support.ReflectionSupport
-
@API(status=MAINTAINED, since="1.0") public final class ReflectionSupport extends Object
ReflectionSupportprovides 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.TestEngineand 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
-
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static List<Class<?>>findAllClassesInClasspathRoot(URI root, Predicate<Class<?>> classFilter, Predicate<String> classNameFilter)Find all classes in the supplied classpathrootthat match the specifiedclassFilterandclassNameFilterpredicates.static List<Class<?>>findAllClassesInModule(String moduleName, Predicate<Class<?>> classFilter, Predicate<String> classNameFilter)Find all classes in the suppliedmoduleNamethat match the specifiedclassFilterandclassNameFilterpredicates.static List<Class<?>>findAllClassesInPackage(String basePackageName, Predicate<Class<?>> classFilter, Predicate<String> classNameFilter)Find all classes in the suppliedbasePackageNamethat match the specifiedclassFilterandclassNameFilterpredicates.static List<Field>findFields(Class<?> clazz, Predicate<Field> predicate, HierarchyTraversalMode traversalMode)Find all fields of the supplied class or interface that match the specifiedpredicate.static Optional<Method>findMethod(Class<?> clazz, String methodName, Class<?>... parameterTypes)Find the firstMethodof 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 firstMethodof 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 specifiedpredicate.static List<Class<?>>findNestedClasses(Class<?> clazz, Predicate<Class<?>> predicate)Find all nested classes within the given class that conform to the given predicate.static ObjectinvokeMethod(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 usetryToLoadClass(String)instead.static <T> TnewInstance(Class<T> clazz, Object... args)Create a new instance of the specifiedClassby 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 defaultClassLoader.static Try<Object>tryToReadFieldValue(Field field, Object instance)Try to read the value of a potentially inaccessible field.
-
-
-
Method Detail
-
loadClass
@API(status=DEPRECATED, since="1.4") @Deprecated public static Optional<Class<?>> loadClass(String name)
Deprecated.Please usetryToLoadClass(String)instead.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.,
[[Iforint[][],[Ljava.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; nevernullor blank- Returns:
- an
Optionalcontaining the loaded class; nevernullbut potentially empty if no such class could be loaded
-
tryToLoadClass
@API(status=MAINTAINED, since="1.4") public static Try<Class<?>> tryToLoadClass(String name)
Try to 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.,
[[Iforint[][],[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; nevernullor blank- Returns:
- a successful
Trycontaining the loaded class or a failedTrycontaining the exception if no such class could be loaded; nevernull - Since:
- 1.4
-
findAllClassesInClasspathRoot
public static List<Class<?>> findAllClassesInClasspathRoot(URI root, Predicate<Class<?>> classFilter, Predicate<String> classNameFilter)
Find all classes in the supplied classpathrootthat match the specifiedclassFilterandclassNameFilterpredicates.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 scan; nevernullclassFilter- the class type filter; nevernullclassNameFilter- the class name filter; nevernull- Returns:
- an immutable list of all such classes found; never
nullbut potentially empty - See Also:
findAllClassesInPackage(String, Predicate, Predicate),findAllClassesInModule(String, Predicate, Predicate)
-
findAllClassesInPackage
public static List<Class<?>> findAllClassesInPackage(String basePackageName, Predicate<Class<?>> classFilter, Predicate<String> classNameFilter)
Find all classes in the suppliedbasePackageNamethat match the specifiedclassFilterandclassNameFilterpredicates.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 scanning; must not benulland must be valid in terms of Java syntaxclassFilter- the class type filter; nevernullclassNameFilter- the class name filter; nevernull- Returns:
- an immutable list of all such classes found; never
nullbut potentially empty - See Also:
findAllClassesInClasspathRoot(URI, Predicate, Predicate),findAllClassesInModule(String, Predicate, Predicate)
-
findAllClassesInModule
public static List<Class<?>> findAllClassesInModule(String moduleName, Predicate<Class<?>> classFilter, Predicate<String> classNameFilter)
Find all classes in the suppliedmoduleNamethat match the specifiedclassFilterandclassNameFilterpredicates.The module-path scanning algorithm searches recursively in all packages contained in the module.
- Parameters:
moduleName- the name of the module to scan; nevernullor emptyclassFilter- the class type filter; nevernullclassNameFilter- the class name filter; nevernull- Returns:
- an immutable list of all such classes found; never
nullbut potentially empty - Since:
- 1.1.1
- See Also:
findAllClassesInClasspathRoot(URI, Predicate, Predicate),findAllClassesInPackage(String, Predicate, Predicate)
-
newInstance
public static <T> T newInstance(Class<T> clazz, Object... args)
Create a new instance of the specifiedClassby 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; nevernullargs- the arguments to pass to the constructor, none of which may benull- Returns:
- the new instance; never
null - See Also:
ExceptionUtils.throwAsUncheckedException(Throwable)
-
invokeMethod
public 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.- Parameters:
method- the method to invoke; nevernulltarget- the object on which to invoke the method; may benullif the method isstaticargs- the arguments to pass to the method- Returns:
- the value returned by the method invocation or
nullif the return type isvoid - See Also:
ExceptionUtils.throwAsUncheckedException(Throwable)
-
findFields
@API(status=MAINTAINED, since="1.4") public static List<Field> findFields(Class<?> clazz, Predicate<Field> predicate, HierarchyTraversalMode traversalMode)
Find all fields of the supplied class or interface that match the specifiedpredicate.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.
- Parameters:
clazz- the class or interface in which to find the fields; nevernullpredicate- the field filter; nevernulltraversalMode- the hierarchy traversal mode; nevernull- Returns:
- an immutable list of all such fields found; never
nullbut potentially empty - Since:
- 1.4
-
tryToReadFieldValue
@API(status=MAINTAINED, since="1.4") public static Try<Object> tryToReadFieldValue(Field field, Object instance)
Try to read the value of a potentially inaccessible field.If an exception occurs while reading the field, a failed
Tryis returned that contains the corresponding exception.- Parameters:
field- the field to read; nevernullinstance- the instance from which the value is to be read; may benullfor a static field- Since:
- 1.4
-
findMethod
public static Optional<Method> findMethod(Class<?> clazz, String methodName, String parameterTypeNames)
Find the firstMethodof 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; nevernullmethodName- the name of the method to find; nevernullor emptyparameterTypeNames- the fully qualified names of the types of parameters accepted by the method, if any, provided as a comma-separated list- Returns:
- an
Optionalcontaining the method found; nevernullbut potentially empty if no such method could be found - See Also:
findMethod(Class, String, Class...)
-
findMethod
public static Optional<Method> findMethod(Class<?> clazz, String methodName, Class<?>... parameterTypes)
Find the firstMethodof 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; nevernullmethodName- the name of the method to find; nevernullor emptyparameterTypes- the types of parameters accepted by the method, if any; nevernull- Returns:
- an
Optionalcontaining the method found; nevernullbut potentially empty if no such method could be found - See Also:
findMethod(Class, String, String)
-
findMethods
public static List<Method> findMethods(Class<?> clazz, Predicate<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
staticmethods 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; nevernullpredicate- the method filter; nevernulltraversalMode- the hierarchy traversal mode; nevernull- Returns:
- an immutable list of all such methods found; never
nullbut potentially empty
-
findNestedClasses
public static List<Class<?>> findNestedClasses(Class<?> clazz, Predicate<Class<?>> predicate)
Find all nested classes within the given class that conform to the given predicate.- Parameters:
clazz- the class to be searched; nevernullpredicate- the predicate against which the list of nested classes is checked; nevernull- Returns:
- an immutable list of all such classes found; never
nullbut potentially empty
-
-