Class ReflectionSupport
- java.lang.Object
-
- org.junit.platform.commons.support.ReflectionSupport
-
@API(status=MAINTAINED, since="1.0") public final class ReflectionSupport extends java.lang.ObjectReflectionSupportprovides 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,ResourceSupport
-
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated 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 classpathrootthat match the specifiedclassFilterandclassNameFilterpredicates.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 suppliedmoduleNamethat match the specifiedclassFilterandclassNameFilterpredicates.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 suppliedbasePackageNamethat match the specifiedclassFilterandclassNameFilterpredicates.static java.util.List<Resource>findAllResourcesInClasspathRoot(java.net.URI root, java.util.function.Predicate<Resource> resourceFilter)Deprecated.Please useResourceSupport.findAllResourcesInClasspathRoot(URI, ResourceFilter)insteadstatic java.util.List<Resource>findAllResourcesInModule(java.lang.String moduleName, java.util.function.Predicate<Resource> resourceFilter)Deprecated.Please useResourceSupport.findAllResourcesInModule(String, ResourceFilter)insteadstatic java.util.List<Resource>findAllResourcesInPackage(java.lang.String basePackageName, java.util.function.Predicate<Resource> resourceFilter)Deprecated.Please useResourceSupport.findAllResourcesInPackage(String, ResourceFilter)insteadstatic java.util.List<java.lang.reflect.Field>findFields(java.lang.Class<?> clazz, java.util.function.Predicate<java.lang.reflect.Field> predicate, HierarchyTraversalMode traversalMode)Find all distinct fields of the supplied class or interface that match the specifiedpredicate.static java.util.Optional<java.lang.reflect.Method>findMethod(java.lang.Class<?> clazz, java.lang.String methodName, java.lang.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 java.util.Optional<java.lang.reflect.Method>findMethod(java.lang.Class<?> clazz, java.lang.String methodName, java.lang.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 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 distinct 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 supplied class, or inherited by the supplied class, that conform to the supplied predicate.static java.lang.ObjectinvokeMethod(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)Deprecated.Please usetryToLoadClass(String)instead.static java.lang.reflect.FieldmakeAccessible(java.lang.reflect.Field field)Make the supplied field accessible via reflection.static <T> TnewInstance(java.lang.Class<T> clazz, java.lang.Object... args)Create a new instance of the specifiedClassby invoking the constructor whose argument list matches the types of the supplied arguments.static java.util.stream.Stream<java.lang.Class<?>>streamAllClassesInClasspathRoot(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 classpathrootthat match the specifiedclassFilterandclassNameFilterpredicates.static java.util.stream.Stream<java.lang.Class<?>>streamAllClassesInModule(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 suppliedmoduleNamethat match the specifiedclassFilterandclassNameFilterpredicates.static java.util.stream.Stream<java.lang.Class<?>>streamAllClassesInPackage(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 suppliedbasePackageNamethat match the specifiedclassFilterandclassNameFilterpredicates.static java.util.stream.Stream<Resource>streamAllResourcesInClasspathRoot(java.net.URI root, java.util.function.Predicate<Resource> resourceFilter)Deprecated.Please useResourceSupport.streamAllResourcesInClasspathRoot(URI, ResourceFilter)insteadstatic java.util.stream.Stream<Resource>streamAllResourcesInModule(java.lang.String moduleName, java.util.function.Predicate<Resource> resourceFilter)Deprecated.Please useResourceSupport.streamAllResourcesInModule(String, ResourceFilter)insteadstatic java.util.stream.Stream<Resource>streamAllResourcesInPackage(java.lang.String basePackageName, java.util.function.Predicate<Resource> resourceFilter)Deprecated.Please useResourceSupport.streamAllResourcesInPackage(String, ResourceFilter)insteadstatic java.util.stream.Stream<java.lang.reflect.Field>streamFields(java.lang.Class<?> clazz, java.util.function.Predicate<java.lang.reflect.Field> predicate, HierarchyTraversalMode traversalMode)Find all distinct fields of the supplied class or interface that match the specifiedpredicate.static java.util.stream.Stream<java.lang.reflect.Method>streamMethods(java.lang.Class<?> clazz, java.util.function.Predicate<java.lang.reflect.Method> predicate, HierarchyTraversalMode traversalMode)Find all distinct methods of the supplied class or interface that match the specifiedpredicate.static java.util.stream.Stream<java.lang.Class<?>>streamNestedClasses(java.lang.Class<?> clazz, java.util.function.Predicate<java.lang.Class<?>> predicate)Find all nested classes within the supplied class, or inherited by the supplied class, that conform to the supplied predicate.static Try<java.util.Set<Resource>>tryToGetResources(java.lang.String classpathResourceName)Deprecated.Please useResourceSupport.tryToGetResources(String)insteadstatic Try<java.util.Set<Resource>>tryToGetResources(java.lang.String classpathResourceName, java.lang.ClassLoader classLoader)Deprecated.Please useResourceSupport.tryToGetResources(String, ClassLoader)insteadstatic Try<java.lang.Class<?>>tryToLoadClass(java.lang.String name)Try to load a class by its primitive name or fully qualified name, using the defaultClassLoader.static Try<java.lang.Class<?>>tryToLoadClass(java.lang.String name, java.lang.ClassLoader classLoader)Try to load a class by its primitive name or fully qualified name, using the suppliedClassLoader.static Try<java.lang.Object>tryToReadFieldValue(java.lang.reflect.Field field, java.lang.Object instance)Try to read the value of a potentially inaccessible field.
-
-
-
Method Detail
-
loadClass
@API(status=DEPRECATED, since="1.4") @Deprecated public static java.util.Optional<java.lang.Class<?>> loadClass(java.lang.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<java.lang.Class<?>> tryToLoadClass(java.lang.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
- See Also:
tryToLoadClass(String, ClassLoader),ResourceSupport.tryToGetResources(String)
-
tryToLoadClass
@API(status=MAINTAINED, since="1.13.3") public static Try<java.lang.Class<?>> tryToLoadClass(java.lang.String name, java.lang.ClassLoader classLoader)Try to load a class by its primitive name or fully qualified name, using the suppliedClassLoader.See
tryToLoadClass(String)for details on support for class names for arrays.- Parameters:
name- the name of the class to load; nevernullor blankclassLoader- theClassLoaderto use; nevernull- Returns:
- a successful
Trycontaining the loaded class or a failedTrycontaining the exception if no such class could be loaded; nevernull - Since:
- 1.10
- See Also:
tryToLoadClass(String),ResourceSupport.tryToGetResources(String, ClassLoader)
-
tryToGetResources
@API(status=DEPRECATED, since="1.14") @Deprecated public static Try<java.util.Set<Resource>> tryToGetResources(java.lang.String classpathResourceName)Deprecated.Please useResourceSupport.tryToGetResources(String)insteadTry to get the resources for the supplied classpath resource name.The name of a classpath resource must follow the semantics for resource paths as defined in
ClassLoader.getResource(String).If the supplied classpath resource name is prefixed with a slash (
/), the slash will be removed.- Parameters:
classpathResourceName- the name of the resource to load; nevernullor blank- Returns:
- a successful
Trycontaining the loaded resources or a failedTrycontaining the exception if no such resources could be loaded; nevernull - Since:
- 1.12
- See Also:
tryToGetResources(String, ClassLoader)
-
tryToGetResources
@API(status=DEPRECATED, since="1.14") @Deprecated public static Try<java.util.Set<Resource>> tryToGetResources(java.lang.String classpathResourceName, java.lang.ClassLoader classLoader)Deprecated.Please useResourceSupport.tryToGetResources(String, ClassLoader)insteadTry to load the resources for the supplied classpath resource name, using the suppliedClassLoader.The name of a classpath resource must follow the semantics for resource paths as defined in
ClassLoader.getResource(String).If the supplied classpath resource name is prefixed with a slash (
/), the slash will be removed.- Parameters:
classpathResourceName- the name of the resource to load; nevernullor blankclassLoader- theClassLoaderto use; nevernull- Returns:
- a successful
Trycontaining the loaded resources or a failedTrycontaining the exception if no such resources could be loaded; nevernull - Since:
- 1.12
- See Also:
tryToGetResources(String)
-
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 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),ResourceSupport.findAllResourcesInClasspathRoot(URI, ResourceFilter)
-
findAllResourcesInClasspathRoot
@API(status=DEPRECATED, since="1.14") @Deprecated public static java.util.List<Resource> findAllResourcesInClasspathRoot(java.net.URI root, java.util.function.Predicate<Resource> resourceFilter)Deprecated.Please useResourceSupport.findAllResourcesInClasspathRoot(URI, ResourceFilter)insteadFind all resources in the supplied classpathrootthat match the specifiedresourceFilterpredicate.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; nevernullresourceFilter- the resource type filter; nevernull- Returns:
- an immutable list of all such resources found; never
nullbut potentially empty - Since:
- 1.11
- See Also:
findAllResourcesInPackage(String, Predicate),findAllResourcesInModule(String, Predicate)
-
streamAllClassesInClasspathRoot
@API(status=MAINTAINED, since="1.10") public static java.util.stream.Stream<java.lang.Class<?>> streamAllClassesInClasspathRoot(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 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:
- a stream of all such classes found; never
nullbut potentially empty - Since:
- 1.10
- See Also:
streamAllClassesInPackage(String, Predicate, Predicate),streamAllClassesInModule(String, Predicate, Predicate),ResourceSupport.streamAllResourcesInClasspathRoot(URI, ResourceFilter)
-
streamAllResourcesInClasspathRoot
@API(status=DEPRECATED, since="1.14") @Deprecated public static java.util.stream.Stream<Resource> streamAllResourcesInClasspathRoot(java.net.URI root, java.util.function.Predicate<Resource> resourceFilter)Deprecated.Please useResourceSupport.streamAllResourcesInClasspathRoot(URI, ResourceFilter)insteadFind all resources in the supplied classpathrootthat match the specifiedresourceFilterpredicate.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; nevernullresourceFilter- the resource type filter; nevernull- Returns:
- a stream of all such classes found; never
nullbut potentially empty - Since:
- 1.11
- See Also:
streamAllResourcesInPackage(String, Predicate),streamAllResourcesInModule(String, 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 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),ResourceSupport.findAllResourcesInPackage(String, ResourceFilter)
-
findAllResourcesInPackage
@API(status=DEPRECATED, since="1.14") @Deprecated public static java.util.List<Resource> findAllResourcesInPackage(java.lang.String basePackageName, java.util.function.Predicate<Resource> resourceFilter)Deprecated.Please useResourceSupport.findAllResourcesInPackage(String, ResourceFilter)insteadFind all resources in the suppliedbasePackageNamethat match the specifiedresourceFilterpredicate.The classpath scanning algorithm searches recursively in subpackages beginning within the supplied base package. The resulting list may include identically named resources from different classpath roots.
- Parameters:
basePackageName- the name of the base package in which to start scanning; must not benulland must be valid in terms of Java syntaxresourceFilter- the resource type filter; nevernull- Returns:
- an immutable list of all such classes found; never
nullbut potentially empty - Since:
- 1.11
- See Also:
findAllResourcesInClasspathRoot(URI, Predicate),findAllResourcesInModule(String, Predicate)
-
streamAllClassesInPackage
@API(status=MAINTAINED, since="1.10") public static java.util.stream.Stream<java.lang.Class<?>> streamAllClassesInPackage(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 suppliedbasePackageNamethat match the specifiedclassFilterandclassNameFilterpredicates.The classpath scanning algorithm searches recursively in subpackages beginning within the supplied base package. The resulting stream may include identically named resources from different classpath roots.
- 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:
- a stream of all such classes found; never
nullbut potentially empty - Since:
- 1.10
- See Also:
streamAllClassesInClasspathRoot(URI, Predicate, Predicate),streamAllClassesInModule(String, Predicate, Predicate),ResourceSupport.streamAllResourcesInPackage(String, ResourceFilter)
-
streamAllResourcesInPackage
@API(status=DEPRECATED, since="1.14") @Deprecated public static java.util.stream.Stream<Resource> streamAllResourcesInPackage(java.lang.String basePackageName, java.util.function.Predicate<Resource> resourceFilter)Deprecated.Please useResourceSupport.streamAllResourcesInPackage(String, ResourceFilter)insteadFind all resources in the suppliedbasePackageNamethat match the specifiedresourceFilterpredicate.The classpath scanning algorithm searches recursively in subpackages beginning within the supplied base package. The resulting stream may include identically named resources from different classpath roots.
- Parameters:
basePackageName- the name of the base package in which to start scanning; must not benulland must be valid in terms of Java syntaxresourceFilter- the resource type filter; nevernull- Returns:
- a stream of all such resources found; never
nullbut potentially empty - Since:
- 1.11
- See Also:
streamAllResourcesInClasspathRoot(URI, Predicate),streamAllResourcesInModule(String, Predicate)
-
findAllClassesInModule
@API(status=MAINTAINED, since="1.1.1") 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 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),ResourceSupport.findAllResourcesInModule(String, ResourceFilter)
-
findAllResourcesInModule
@API(status=DEPRECATED, since="1.14") @Deprecated public static java.util.List<Resource> findAllResourcesInModule(java.lang.String moduleName, java.util.function.Predicate<Resource> resourceFilter)Deprecated.Please useResourceSupport.findAllResourcesInModule(String, ResourceFilter)insteadFind all resources in the suppliedmoduleNamethat match the specifiedresourceFilterpredicate.The module-path scanning algorithm searches recursively in all packages contained in the module.
- Parameters:
moduleName- the name of the module to scan; nevernullor emptyresourceFilter- the resource type filter; nevernull- Returns:
- an immutable list of all such resources found; never
nullbut potentially empty - Since:
- 1.11
- See Also:
findAllResourcesInClasspathRoot(URI, Predicate),findAllResourcesInPackage(String, Predicate)
-
streamAllClassesInModule
@API(status=MAINTAINED, since="1.10") public static java.util.stream.Stream<java.lang.Class<?>> streamAllClassesInModule(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 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:
- a stream of all such classes found; never
nullbut potentially empty - Since:
- 1.10
- See Also:
streamAllClassesInClasspathRoot(URI, Predicate, Predicate),streamAllClassesInPackage(String, Predicate, Predicate)
-
streamAllResourcesInModule
@API(status=DEPRECATED, since="1.14") @Deprecated public static java.util.stream.Stream<Resource> streamAllResourcesInModule(java.lang.String moduleName, java.util.function.Predicate<Resource> resourceFilter)Deprecated.Please useResourceSupport.streamAllResourcesInModule(String, ResourceFilter)insteadFind all resources in the suppliedmoduleNamethat match the specifiedresourceFilterpredicate.The module-path scanning algorithm searches recursively in all packages contained in the module.
- Parameters:
moduleName- the name of the module to scan; nevernullor emptyresourceFilter- the resource type filter; nevernull- Returns:
- a stream of all such resources found; never
nullbut potentially empty - Since:
- 1.11
- See Also:
streamAllResourcesInClasspathRoot(URI, Predicate),streamAllResourcesInPackage(String, Predicate)
-
newInstance
public static <T> T newInstance(java.lang.Class<T> clazz, java.lang.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 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; 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 java.util.List<java.lang.reflect.Field> findFields(java.lang.Class<?> clazz, java.util.function.Predicate<java.lang.reflect.Field> predicate, HierarchyTraversalMode traversalMode)Find all distinct 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 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
-
streamFields
@API(status=MAINTAINED, since="1.10") public static java.util.stream.Stream<java.lang.reflect.Field> streamFields(java.lang.Class<?> clazz, java.util.function.Predicate<java.lang.reflect.Field> predicate, HierarchyTraversalMode traversalMode)Find all distinct 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 synthetic.
- Parameters:
clazz- the class or interface in which to find the fields; nevernullpredicate- the field filter; nevernulltraversalMode- the hierarchy traversal mode; nevernull- Returns:
- a stream of all such fields found; never
nullbut potentially empty - Since:
- 1.10
-
tryToReadFieldValue
@API(status=MAINTAINED, since="1.4") public static Try<java.lang.Object> tryToReadFieldValue(java.lang.reflect.Field field, java.lang.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 java.util.Optional<java.lang.reflect.Method> findMethod(java.lang.Class<?> clazz, java.lang.String methodName, java.lang.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.As of JUnit Platform 1.10, this method uses the
ClassLoaderof the suppliedclazzto load parameter types instead of using the defaultClassLoader, which allows parameter types to be resolved in differentClassLoaderarrangements.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 java.util.Optional<java.lang.reflect.Method> findMethod(java.lang.Class<?> clazz, java.lang.String methodName, java.lang.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 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 distinct methods of the supplied class or interface that match the specifiedpredicate.The results will not contain methods that are overridden.
If you 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
-
streamMethods
@API(status=MAINTAINED, since="1.10") public static java.util.stream.Stream<java.lang.reflect.Method> streamMethods(java.lang.Class<?> clazz, java.util.function.Predicate<java.lang.reflect.Method> predicate, HierarchyTraversalMode traversalMode)Find all distinct methods of the supplied class or interface that match the specifiedpredicate.The results will not contain methods that are overridden.
If you 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:
- a stream of all such methods found; never
nullbut potentially empty - Since:
- 1.10
-
findNestedClasses
public static java.util.List<java.lang.Class<?>> findNestedClasses(java.lang.Class<?> clazz, java.util.function.Predicate<java.lang.Class<?>> predicate) throws JUnitExceptionFind all nested classes within the supplied class, or inherited by the supplied class, that conform to the supplied predicate.This method does not search for nested classes recursively.
As of JUnit Platform 1.6, this method detects cycles in inner class hierarchies — from the supplied class up to the outermost enclosing class — and throws a
JUnitExceptionif such a cycle is detected. Cycles within inner class hierarchies below the supplied class are not detected by this method.- 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 - Throws:
JUnitException- if a cycle is detected within an inner class hierarchy
-
streamNestedClasses
@API(status=MAINTAINED, since="1.10") public static java.util.stream.Stream<java.lang.Class<?>> streamNestedClasses(java.lang.Class<?> clazz, java.util.function.Predicate<java.lang.Class<?>> predicate) throws JUnitExceptionFind all nested classes within the supplied class, or inherited by the supplied class, that conform to the supplied predicate.This method does not search for nested classes recursively.
As of JUnit Platform 1.6, this method detects cycles in inner class hierarchies — from the supplied class up to the outermost enclosing class — and throws a
JUnitExceptionif such a cycle is detected. Cycles within inner class hierarchies below the supplied class are not detected by this method.- Parameters:
clazz- the class to be searched; nevernullpredicate- the predicate against which the list of nested classes is checked; nevernull- Returns:
- a stream of all such classes found; never
nullbut potentially empty - Throws:
JUnitException- if a cycle is detected within an inner class hierarchy- Since:
- 1.10
-
makeAccessible
@API(status=MAINTAINED, since="1.13.3") public static java.lang.reflect.Field makeAccessible(java.lang.reflect.Field field)Make the supplied field accessible via reflection.If you're looking for similar functionality for constructors or methods, consider using
newInstance(Class, Object...)orinvokeMethod(Method, Object, Object...).- Parameters:
field- the field to make accessible; nevernull- Returns:
- the supplied field
- Since:
- 1.12
- See Also:
Field.setAccessible(boolean)
-
-