@API(status=INTERNAL,
since="1.0")
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,
ClassFilter classFilter) |
static List<Class<?>> |
findAllClassesInClasspathRoot(URI root,
Predicate<Class<?>> classFilter,
Predicate<String> classNameFilter) |
static List<Class<?>> |
findAllClassesInModule(String moduleName,
ClassFilter classFilter) |
static List<Class<?>> |
findAllClassesInModule(String moduleName,
Predicate<Class<?>> classFilter,
Predicate<String> classNameFilter) |
static List<Class<?>> |
findAllClassesInPackage(String basePackageName,
ClassFilter classFilter) |
static List<Class<?>> |
findAllClassesInPackage(String basePackageName,
Predicate<Class<?>> classFilter,
Predicate<String> classNameFilter) |
static List<Constructor<?>> |
findConstructors(Class<?> clazz,
Predicate<Constructor<?>> predicate)
Find all constructors in the supplied class that match the supplied predicate.
|
static List<Field> |
findFields(Class<?> clazz,
Predicate<Field> predicate,
ReflectionUtils.HierarchyTraversalMode traversalMode) |
static Optional<Method> |
findMethod(Class<?> clazz,
String methodName,
Class<?>... parameterTypes) |
static Optional<Method> |
findMethod(Class<?> clazz,
String methodName,
String parameterTypeNames) |
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) |
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,
Method method)
Build the fully qualified method name for the method described by the
supplied class and method.
|
static String |
getFullyQualifiedMethodName(Class<?> clazz,
String methodName,
Class<?>... parameterTypes)
Build the fully qualified method name for the method described by the
supplied class, method name, and parameter types.
|
static Optional<Object> |
getOutermostInstance(Object inner,
Class<?> requiredType)
Deprecated.
|
static Class<?> |
getWrapperType(Class<?> type)
Get the wrapper type for the supplied primitive type.
|
static Object |
invokeMethod(Method method,
Object target,
Object... args) |
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<?> targetType)
Determine if the supplied object can be assigned to the supplied target
type for the purpose of reflective method invocations.
|
static boolean |
isInnerClass(Class<?> clazz)
Determine if the supplied class is an inner class (i.e., a
non-static member class).
|
static boolean |
isMethodPresent(Class<?> clazz,
Predicate<Method> predicate)
|
static boolean |
isMultidimensionalArray(Object obj)
Determine if the supplied object is a multidimensional array.
|
static boolean |
isNotPrivate(Class<?> clazz) |
static boolean |
isNotPrivate(Member member) |
static boolean |
isNotStatic(Class<?> clazz) |
static boolean |
isNotStatic(Member member) |
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)
Deprecated.
Please use
tryToLoadClass(String) instead. |
static Optional<Class<?>> |
loadClass(String name,
ClassLoader classLoader)
Deprecated.
Please use
tryToLoadClass(String, ClassLoader)
instead. |
static <T extends AccessibleObject> |
makeAccessible(T object) |
static <T> T |
newInstance(Class<T> clazz,
Object... args) |
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 String[] |
parseFullyQualifiedMethodName(String fullyQualifiedMethodName)
Parse the supplied fully qualified method name into a 3-element
String[] with the following content. |
static <T> Optional<Object> |
readFieldValue(Class<T> clazz,
String fieldName,
T instance)
Deprecated.
Please use
tryToReadFieldValue(Class, String, Object)
instead. |
static Optional<Object> |
readFieldValue(Field field)
Deprecated.
Please use
tryToReadFieldValue(Field) instead. |
static Optional<Object> |
readFieldValue(Field field,
Object instance)
Deprecated.
Please use
tryToReadFieldValue(Field, Object)
instead. |
static List<Object> |
readFieldValues(List<Field> fields,
Object instance)
Read the values of the supplied fields, making each field accessible if
necessary and masking
any checked exception as an unchecked exception.
|
static List<Object> |
readFieldValues(List<Field> fields,
Object instance,
Predicate<Field> predicate)
Read the values of the supplied fields, making each field accessible if
necessary, masking
any checked exception as an unchecked exception, and filtering out fields
that do not pass the supplied
predicate. |
static boolean |
returnsVoid(Method method) |
static Try<Method> |
tryToGetMethod(Class<?> clazz,
String methodName,
Class<?>... parameterTypes)
Try to get the
Method in the specified class with the specified
name and parameter types. |
static Try<Class<?>> |
tryToLoadClass(String name) |
static Try<Class<?>> |
tryToLoadClass(String name,
ClassLoader classLoader)
Try to load a class by its primitive name or fully qualified
name, using the supplied
ClassLoader. |
static <T> Try<Object> |
tryToReadFieldValue(Class<T> clazz,
String fieldName,
T instance)
Try to read the value of a potentially inaccessible or nonexistent field.
|
static Try<Object> |
tryToReadFieldValue(Field field)
Try to read the value of a potentially inaccessible static field.
|
static Try<Object> |
tryToReadFieldValue(Field field,
Object instance) |
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 isNotPrivate(Class<?> clazz)
public static boolean isNotPrivate(Member member)
public static boolean isAbstract(Class<?> clazz)
public static boolean isAbstract(Member member)
public static boolean isStatic(Class<?> clazz)
public static boolean isNotStatic(Class<?> clazz)
public static boolean isStatic(Member member)
public static boolean isNotStatic(Member member)
public static boolean isInnerClass(Class<?> clazz)
Technically speaking (i.e., according to the Java Language
Specification), "an inner class may be a non-static member class, a
local class, or an anonymous class." However, this method does not
return true for a local or anonymous class.
clazz - the class to check; never nulltrue if the class is an inner classpublic static boolean returnsVoid(Method method)
public static boolean isArray(Object obj)
obj - the object to test; potentially nulltrue if the object is an array@API(status=INTERNAL,
since="1.3.2")
public static boolean isMultidimensionalArray(Object obj)
obj - the object to test; potentially nulltrue if the object is a multidimensional arraypublic static boolean isAssignableTo(Object obj, Class<?> targetType)
In contrast to Class.isInstance(Object), this method returns
true if the target type represents a primitive type whose
wrapper matches the supplied object's type. In addition, this method
also supports
widening conversions for primitive types and their corresponding
wrapper types.
If the supplied object is null and the supplied type does not
represent a primitive type, this method returns true.
obj - the object to test for assignment compatibility; potentially nulltargetType - 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(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)@API(status=DEPRECATED,
since="1.4")
@Deprecated
public static <T> Optional<Object> readFieldValue(Class<T> clazz,
String fieldName,
T instance)
tryToReadFieldValue(Class, String, Object)
instead.If the field does not exist or the value of the field is null,
an empty Optional will be 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 fieldreadFieldValue(Field),
readFieldValue(Field, Object)@API(status=INTERNAL,
since="1.4")
public static <T> Try<Object> tryToReadFieldValue(Class<T> clazz,
String fieldName,
T instance)
If the field does not exist or an exception occurs while reading it, a
failed Try is returned that contains the corresponding exception.
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 fieldtryToReadFieldValue(Field),
tryToReadFieldValue(Field, Object)@API(status=DEPRECATED,
since="1.4")
@Deprecated
public static Optional<Object> readFieldValue(Field field)
tryToReadFieldValue(Field) instead.If the value of the field is null, an empty Optional
will be returned.
field - the field to read; never nullreadFieldValue(Field, Object),
readFieldValue(Class, String, Object)@API(status=INTERNAL,
since="1.4")
public static Try<Object> tryToReadFieldValue(Field field)
If an exception occurs while reading the field, a failed Try
is returned that contains the corresponding exception.
field - the field to read; never nulltryToReadFieldValue(Field, Object),
tryToReadFieldValue(Class, String, Object)@API(status=DEPRECATED,
since="1.4")
@Deprecated
public static Optional<Object> readFieldValue(Field field,
Object instance)
tryToReadFieldValue(Field, Object)
instead.If the value of the field is null, an empty Optional
will be returned.
field - the field to read; never nullinstance - the instance from which the value is to be read; may
be null for a static fieldreadFieldValue(Field),
readFieldValue(Class, String, Object)@API(status=INTERNAL,
since="1.4")
public static Try<Object> tryToReadFieldValue(Field field,
Object instance)
public static List<Object> readFieldValues(List<Field> fields, Object instance)
fields - the list of fields to read; never nullinstance - the instance from which the values are to be read; may
be null for static fieldsnull but may be empty or contain null entriespublic static List<Object> readFieldValues(List<Field> fields, Object instance, Predicate<Field> predicate)
predicate.fields - the list of fields to read; never nullinstance - the instance from which the values are to be read; may
be null for static fieldspredicate - the field filter; never nullnull but may be empty or contain null entries@API(status=DEPRECATED,
since="1.4")
@Deprecated
public static Optional<Class<?>> loadClass(String name)
tryToLoadClass(String) instead.ReflectionSupport.loadClass(String)@API(status=INTERNAL,
since="1.4")
public static Try<Class<?>> tryToLoadClass(String name)
ReflectionSupport.tryToLoadClass(String)@API(status=DEPRECATED,
since="1.4")
@Deprecated
public static Optional<Class<?>> loadClass(String name,
ClassLoader classLoader)
tryToLoadClass(String, ClassLoader)
instead.ClassLoader.
See ReflectionSupport.loadClass(String)
for details on support for class names for arrays.
name - the name of the class to load; never null or blankclassLoader - the ClassLoader to use; never nullloadClass(String)@API(status=INTERNAL,
since="1.4")
public static Try<Class<?>> tryToLoadClass(String name,
ClassLoader classLoader)
ClassLoader.
See ReflectionSupport.tryToLoadClass(String)
for details on support for class names for arrays.
name - the name of the class to load; never null or blankclassLoader - the ClassLoader to use; never nulltryToLoadClass(String)public static String getFullyQualifiedMethodName(Class<?> clazz, Method method)
Note that the class is not necessarily the class in which the method is declared.
clazz - the class from which the method should be referenced; never nullmethod - the method; never nullnullgetFullyQualifiedMethodName(Class, String, Class...)public static String getFullyQualifiedMethodName(Class<?> clazz, String methodName, Class<?>... parameterTypes)
Note that the class is not necessarily the class in which the method is declared.
clazz - the class from which the method should be referenced; never nullmethodName - the name of the method; never null or blankparameterTypes - the parameter types of the method; may be null or emptynullgetFullyQualifiedMethodName(Class, Method)public static String[] parseFullyQualifiedMethodName(String fullyQualifiedMethodName)
String[] with the following content.
0: the fully qualified class name1: the name of the method2: a comma-separated list of parameter types, or a
blank string if the method does not declare any formal parametersfullyQualifiedMethodName - a fully qualified method name,
never null or blank@API(status=DEPRECATED,
since="1.4")
@Deprecated
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<?>> classFilter, Predicate<String> classNameFilter)
public static List<Class<?>> findAllClassesInClasspathRoot(URI root, ClassFilter classFilter)
public static List<Class<?>> findAllClassesInPackage(String basePackageName, Predicate<Class<?>> classFilter, Predicate<String> classNameFilter)
public static List<Class<?>> findAllClassesInPackage(String basePackageName, ClassFilter classFilter)
public static List<Class<?>> findAllClassesInModule(String moduleName, Predicate<Class<?>> classFilter, Predicate<String> classNameFilter)
ReflectionSupport.findAllClassesInModule(String, Predicate, Predicate)public static List<Class<?>> findAllClassesInModule(String moduleName, ClassFilter classFilter)
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 List<Constructor<?>> findConstructors(Class<?> clazz, Predicate<Constructor<?>> predicate)
clazz - the class in which to search for constructors; never nullpredicate - the predicate to use to test for a match; never nullnull
but potentially emptypublic static List<Field> findFields(Class<?> clazz, Predicate<Field> predicate, ReflectionUtils.HierarchyTraversalMode traversalMode)
public static boolean isMethodPresent(Class<?> clazz, Predicate<Method> predicate)
Method matching the supplied Predicate
is present within the type hierarchy of the specified class, 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.clazz - the class or interface in which to find the method; never
nullpredicate - the predicate to use to test for a match; never
nulltrue if such a method is presentfindMethod(Class, String, String),
findMethod(Class, String, Class...)@API(status=INTERNAL,
since="1.4")
public static Try<Method> tryToGetMethod(Class<?> clazz,
String methodName,
Class<?>... parameterTypes)
Method in the specified class with the specified
name and parameter types.
This method delegates to Class.getMethod(String, Class...) but
catches any exception thrown.
clazz - the class in which to search for the method; never nullmethodName - the name of the method to get; never null or blankparameterTypes - the parameter types of the method; may be null
or emptyTry containing the method or a failed
Try containing the NoSuchMethodException thrown by
Class#getMethod(); never nullpublic static Optional<Method> findMethod(Class<?> clazz, String methodName, String parameterTypeNames)
public static Optional<Method> findMethod(Class<?> clazz, String methodName, Class<?>... parameterTypes)
public static List<Method> findMethods(Class<?> clazz, Predicate<Method> predicate)
predicate, using top-down search semantics
within the type hierarchy.
The results will not contain instance methods that are overridden
or static methods that are hidden.
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)
public static <T extends AccessibleObject> T makeAccessible(T object)
public static Set<Class<?>> getAllAssignmentCompatibleClasses(Class<?> clazz)
Class, including itself.clazz - the Class to look upClass.isAssignableFrom(java.lang.Class<?>)Copyright © 2024. All rights reserved.