@API(value=Internal) public final class ReflectionUtils extends Object
These utilities are intended solely for usage within the JUnit framework itself. Any usage by external parties is not supported. Use at your own risk!
Some utilities are published via the maintained ReflectionSupport
class.
ReflectionSupport| Modifier and Type | Class and Description |
|---|---|
static class |
ReflectionUtils.HierarchyTraversalMode
Modes in which a hierarchy can be traversed — for example, when
searching for methods or fields within a class hierarchy.
|
| Modifier and Type | Method and Description |
|---|---|
static List<Class<?>> |
findAllClassesInClasspathRoot(URI root,
Predicate<Class<?>> classTester,
Predicate<String> classNameFilter) |
static List<Class<?>> |
findAllClassesInPackage(String basePackageName,
Predicate<Class<?>> classTester,
Predicate<String> classNameFilter) |
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)
Find all methods of the supplied class or interface
that match the specified
predicate, using top-down search semantics
within the type hierarchy. |
static List<Method> |
findMethods(Class<?> clazz,
Predicate<Method> predicate,
ReflectionUtils.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) |
static Set<Class<?>> |
getAllAssignmentCompatibleClasses(Class<?> clazz)
Return all classes and interfaces that can be used as assignment types
for instances of the specified
Class, including itself. |
static Set<Path> |
getAllClasspathRootDirectories() |
static <T> Constructor<T> |
getDeclaredConstructor(Class<T> clazz)
Get the sole declared
Constructor for the supplied class. |
static String |
getFullyQualifiedMethodName(Class<?> clazz,
String methodName,
Class<?>... params)
Build the fully qualified method name for the method described by the
supplied class, method name, and parameter types.
|
static Optional<Method> |
getMethod(Class<?> clazz,
String methodName,
Class<?>... parameterTypes) |
static Optional<Object> |
getOutermostInstance(Object inner,
Class<?> requiredType)
Get the outermost instance of the required type, searching recursively
through enclosing instances.
|
static Class<?> |
getWrapperType(Class<?> type)
Get the wrapper type for the supplied primitive type.
|
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 boolean |
isAbstract(Class<?> clazz) |
static boolean |
isAbstract(Member member) |
static boolean |
isArray(Object obj)
Determine if the supplied object is an array.
|
static boolean |
isAssignableTo(Object obj,
Class<?> type)
Determine if the supplied object can be assigned to the supplied type
for the purpose of reflective method invocations.
|
static boolean |
isPrivate(Class<?> clazz) |
static boolean |
isPrivate(Member member) |
static boolean |
isPublic(Class<?> clazz) |
static boolean |
isPublic(Member member) |
static boolean |
isStatic(Class<?> clazz) |
static boolean |
isStatic(Member member) |
static Optional<Class<?>> |
loadClass(String name)
Load a class by its primitive name or fully qualified name,
using the default
ClassLoader. |
static Optional<Class<?>> |
loadClass(String name,
ClassLoader classLoader)
Load a class by its primitive name or fully qualified name,
using the supplied
ClassLoader. |
static Optional<Method> |
loadMethod(String fullyQualifiedMethodName)
Load a method by its fully qualified name.
|
static <T extends AccessibleObject> |
makeAccessible(T object) |
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 <T> T |
newInstance(Constructor<T> constructor,
Object... args)
Create a new instance of type
T by invoking the supplied constructor
with the supplied arguments. |
static <T> Optional<Object> |
readFieldValue(Class<T> clazz,
String fieldName,
T instance)
Read the value of a potentially inaccessible field.
|
public static boolean isPublic(Class<?> clazz)
public static boolean isPublic(Member member)
public static boolean isPrivate(Class<?> clazz)
public static boolean isPrivate(Member member)
public static boolean isAbstract(Class<?> clazz)
public static boolean isAbstract(Member member)
public static boolean isStatic(Class<?> clazz)
public static boolean isStatic(Member member)
public static boolean isArray(Object obj)
obj - the object to test; potentially nulltrue if the object is an arraypublic static boolean isAssignableTo(Object obj, Class<?> type)
In contrast to Class.isInstance(Object), this method returns
true if the supplied type represents a primitive type whose
wrapper matches the supplied object's type.
Returns true if the supplied object is null and the
supplied type does not represent a primitive type.
obj - the object to test for assignment compatibility; potentially nulltype - the type to check against; never nulltrue if the object is assignment compatibleClass.isInstance(Object),
Class.isAssignableFrom(Class)public static Class<?> getWrapperType(Class<?> type)
type - the primitive type for which to retrieve the wrapper typenull if the
supplied type is null or not a primitive typepublic 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 nullargs - the arguments to pass to the constructor none of which may be nullnewInstance(Constructor, Object...),
ExceptionUtils.throwAsUncheckedException(Throwable)public static <T> T newInstance(Constructor<T> constructor, Object... args)
T by invoking the supplied constructor
with the supplied arguments.
The constructor will be made accessible if necessary, and any checked exception will be masked as an unchecked exception.
constructor - the constructor to invoke; never nullargs - the arguments to pass to the constructornullnewInstance(Class, Object...),
ExceptionUtils.throwAsUncheckedException(Throwable)public static Object invokeMethod(Method method, Object target, Object... args)
method - the method to invoke; never nulltarget - the object on which to invoke the method; may be
null if the method is staticargs - the arguments to pass to the methodnull
if the return type is voidExceptionUtils.throwAsUncheckedException(Throwable)public static Optional<Class<?>> loadClass(String name)
ClassLoader.
See loadClass(String, ClassLoader) for details on support for
class names for arrays.
name - the name of the class to load; never null or blankloadClass(String, ClassLoader)public static Optional<Class<?>> loadClass(String name, ClassLoader classLoader)
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 blankclassLoader - the ClassLoader to use; never nullloadClass(String)public static Optional<Method> loadMethod(String fullyQualifiedMethodName)
The following formats are supported.
[fully qualified class name]#[methodName][fully qualified class name]#[methodName](parameter type list)
The parameter type list is a comma-separated list of primitive names or fully qualified class names for the types of parameters accepted by the method.
See loadClass(String, ClassLoader) for details on the supported
syntax for array parameter types.
| Method | Fully Qualified Method Name |
|---|---|
java.lang.String.chars() | java.lang.String#chars |
java.lang.String.chars() | java.lang.String#chars() |
java.lang.String.equalsIgnoreCase(String) | java.lang.String#equalsIgnoreCase(java.lang.String) |
java.lang.String.substring(int, int) | java.lang.String#substring(int, int) |
example.Calc.avg(int[]) | example.Calc#avg([I) |
example.Calc.avg(int[]) | example.Calc#avg(int[]) |
example.Matrix.multiply(double[][]) | example.Matrix#multiply([[D) |
example.Matrix.multiply(double[][]) | example.Matrix#multiply(double[][]) |
example.Service.process(String[]) | example.Service#process([Ljava.lang.String;) |
example.Service.process(String[]) | example.Service#process(java.lang.String[]) |
example.Service.process(String[][]) | example.Service#process([[Ljava.lang.String;) |
example.Service.process(String[][]) | example.Service#process(java.lang.String[][]) |
fullyQualifiedMethodName - the fully qualified name of the method to load;
never null or blankOptional containing the method; never null but
potentially emptygetFullyQualifiedMethodName(Class, String, Class...)public static String getFullyQualifiedMethodName(Class<?> clazz, String methodName, Class<?>... params)
See loadMethod(String) for details on the format.
clazz - the class that declares the method; never nullmethodName - the name of the method; never null or blankparams - the parameter types of the method; may be null or emptynullloadMethod(String)public static Optional<Object> getOutermostInstance(Object inner, Class<?> requiredType)
If the supplied inner object is of the required type, it will simply be returned.
inner - the inner object from which to begin the search; never nullrequiredType - the required type of the outermost instance; never nullOptional containing the outermost instance; never null
but potentially emptypublic static List<Class<?>> findAllClassesInClasspathRoot(URI root, Predicate<Class<?>> classTester, Predicate<String> classNameFilter)
public static List<Class<?>> findAllClassesInPackage(String basePackageName, Predicate<Class<?>> classTester, Predicate<String> classNameFilter)
public static List<Class<?>> findNestedClasses(Class<?> clazz, Predicate<Class<?>> predicate)
public static <T> Constructor<T> getDeclaredConstructor(Class<T> clazz)
Constructor for the supplied class.
Throws a PreconditionViolationException if the supplied
class declares more than one constructor.
clazz - the class to get the constructor fornullClass.getDeclaredConstructors()public static Optional<Method> getMethod(Class<?> clazz, String methodName, Class<?>... parameterTypes)
public 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.
Note, however, that the current algorithm traverses the entire type hierarchy even after having found a match.
clazz - the class or interface in which to find the method; never nullmethodName - 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...),
ReflectionUtils.HierarchyTraversalMode.BOTTOM_UPpublic 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.
Note, however, that the current algorithm traverses the entire type hierarchy even after having found a match.
clazz - the class or interface in which to find the method; never nullmethodName - the name of the method to find; never null or emptyparameterTypes - the types of parameters accepted by the method, if any;
never nullOptional containing the method found; never null
but potentially empty if no such method could be foundfindMethod(Class, String, String),
ReflectionUtils.HierarchyTraversalMode.BOTTOM_UPpublic static List<Method> findMethods(Class<?> clazz, Predicate<Method> predicate)
predicate, using top-down search semantics
within the type hierarchy.clazz - the class or interface in which to find the methods; never nullpredicate - the method filter; never nullnullReflectionUtils.HierarchyTraversalMode.TOP_DOWN,
findMethods(Class, Predicate, HierarchyTraversalMode)public static List<Method> findMethods(Class<?> clazz, Predicate<Method> predicate, ReflectionUtils.HierarchyTraversalMode traversalMode)
predicate.clazz - the class or interface in which to find the methods; never nullpredicate - the method filter; never nulltraversalMode - the hierarchy traversal mode; never nullnullReflectionSupport.findMethods(Class, Predicate, org.junit.platform.commons.support.HierarchyTraversalMode)public static <T> Optional<Object> readFieldValue(Class<T> clazz, String fieldName, T instance)
If the field does not exist, an exception occurs while reading it, or
the value of the field is null, an empty Optional is
returned.
clazz - the class where the field is declared; never nullfieldName - the name of the field; never null or emptyinstance - the instance from where the value is to be read; may
be null for a static fieldpublic static <T extends AccessibleObject> T makeAccessible(T object)
public static Set<Class<?>> getAllAssignmentCompatibleClasses(Class<?> clazz)
Class, including itself.clazz - the Class to lookupClass.isAssignableFrom(java.lang.Class<?>)