Class ReflectionUtils
- java.lang.Object
-
- org.junit.platform.commons.util.ReflectionUtils
-
@API(status=INTERNAL, since="1.0") public final class ReflectionUtils extends java.lang.Object
Collection of utilities for working with the Java reflection APIs.DISCLAIMER
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.- Since:
- 1.0
- See Also:
ReflectionSupport
-
-
Nested Class Summary
Nested Classes Modifier and Type Class 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.
-
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)
static java.util.List<java.lang.Class<?>>
findAllClassesInClasspathRoot(java.net.URI root, ClassFilter classFilter)
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)
static java.util.List<java.lang.Class<?>>
findAllClassesInModule(java.lang.String moduleName, ClassFilter classFilter)
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)
static java.util.List<java.lang.Class<?>>
findAllClassesInPackage(java.lang.String basePackageName, ClassFilter classFilter)
static java.util.List<java.lang.reflect.Constructor<?>>
findConstructors(java.lang.Class<?> clazz, java.util.function.Predicate<java.lang.reflect.Constructor<?>> predicate)
Find all constructors in the supplied class that match the supplied predicate.static java.util.List<java.lang.reflect.Field>
findFields(java.lang.Class<?> clazz, java.util.function.Predicate<java.lang.reflect.Field> predicate, ReflectionUtils.HierarchyTraversalMode traversalMode)
Find all 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)
static java.util.Optional<java.lang.reflect.Method>
findMethod(java.lang.Class<?> clazz, java.lang.String methodName, java.lang.String parameterTypeNames)
static java.util.List<java.lang.reflect.Method>
findMethods(java.lang.Class<?> clazz, java.util.function.Predicate<java.lang.reflect.Method> predicate)
Find all methods of the supplied class or interface that match the specifiedpredicate
, using top-down search semantics within the type hierarchy.static java.util.List<java.lang.reflect.Method>
findMethods(java.lang.Class<?> clazz, java.util.function.Predicate<java.lang.reflect.Method> predicate, ReflectionUtils.HierarchyTraversalMode traversalMode)
static java.util.List<java.lang.Class<?>>
findNestedClasses(java.lang.Class<?> clazz, java.util.function.Predicate<java.lang.Class<?>> predicate)
static java.util.Set<java.lang.Class<?>>
getAllAssignmentCompatibleClasses(java.lang.Class<?> clazz)
Return all classes and interfaces that can be used as assignment types for instances of the specifiedClass
, including itself.static java.util.Set<java.nio.file.Path>
getAllClasspathRootDirectories()
static <T> java.lang.reflect.Constructor<T>
getDeclaredConstructor(java.lang.Class<T> clazz)
Get the sole declaredConstructor
for the supplied class.static java.lang.String
getFullyQualifiedMethodName(java.lang.Class<?> clazz, java.lang.String methodName, java.lang.Class<?>... parameterTypes)
Build the fully qualified method name for the method described by the supplied class, method name, and parameter types.static java.util.Optional<java.lang.Object>
getOutermostInstance(java.lang.Object inner, java.lang.Class<?> requiredType)
Get the outermost instance of the required type, searching recursively through enclosing instances.static java.lang.Class<?>
getWrapperType(java.lang.Class<?> type)
Get the wrapper type for the supplied primitive type.static java.lang.Object
invokeMethod(java.lang.reflect.Method method, java.lang.Object target, java.lang.Object... args)
static boolean
isAbstract(java.lang.Class<?> clazz)
static boolean
isAbstract(java.lang.reflect.Member member)
static boolean
isArray(java.lang.Object obj)
Determine if the supplied object is an array.static boolean
isAssignableTo(java.lang.Object obj, java.lang.Class<?> type)
Determine if the supplied object can be assigned to the supplied type for the purpose of reflective method invocations.static boolean
isInnerClass(java.lang.Class<?> clazz)
static boolean
isMethodPresent(java.lang.Class<?> clazz, java.util.function.Predicate<java.lang.reflect.Method> predicate)
Determine if aMethod
matching the suppliedPredicate
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.static boolean
isNotPrivate(java.lang.reflect.Member member)
static boolean
isNotStatic(java.lang.reflect.Member member)
static boolean
isPrivate(java.lang.Class<?> clazz)
static boolean
isPrivate(java.lang.reflect.Member member)
static boolean
isPublic(java.lang.Class<?> clazz)
static boolean
isPublic(java.lang.reflect.Member member)
static boolean
isStatic(java.lang.Class<?> clazz)
static boolean
isStatic(java.lang.reflect.Member member)
static java.util.Optional<java.lang.Class<?>>
loadClass(java.lang.String name)
static java.util.Optional<java.lang.Class<?>>
loadClass(java.lang.String name, java.lang.ClassLoader classLoader)
Load a class by its primitive name or fully qualified name, using the suppliedClassLoader
.static <T extends java.lang.reflect.AccessibleObject>
TmakeAccessible(T object)
static <T> T
newInstance(java.lang.Class<T> clazz, java.lang.Object... args)
static <T> T
newInstance(java.lang.reflect.Constructor<T> constructor, java.lang.Object... args)
Create a new instance of typeT
by invoking the supplied constructor with the supplied arguments.static <T> java.util.Optional<java.lang.Object>
readFieldValue(java.lang.Class<T> clazz, java.lang.String fieldName, T instance)
Read the value of a potentially inaccessible or nonexistent field.static java.util.Optional<java.lang.Object>
readFieldValue(java.lang.reflect.Field field)
Read the value of a potentially inaccessible static field.static <T> java.util.Optional<java.lang.Object>
readFieldValue(java.lang.reflect.Field field, T instance)
Read the value of a potentially inaccessible field.static boolean
returnsVoid(java.lang.reflect.Method method)
-
-
-
Method Detail
-
isPublic
public static boolean isPublic(java.lang.Class<?> clazz)
-
isPublic
public static boolean isPublic(java.lang.reflect.Member member)
-
isPrivate
public static boolean isPrivate(java.lang.Class<?> clazz)
-
isPrivate
public static boolean isPrivate(java.lang.reflect.Member member)
-
isNotPrivate
public static boolean isNotPrivate(java.lang.reflect.Member member)
-
isAbstract
public static boolean isAbstract(java.lang.Class<?> clazz)
-
isAbstract
public static boolean isAbstract(java.lang.reflect.Member member)
-
isStatic
public static boolean isStatic(java.lang.Class<?> clazz)
-
isStatic
public static boolean isStatic(java.lang.reflect.Member member)
-
isNotStatic
public static boolean isNotStatic(java.lang.reflect.Member member)
-
isInnerClass
public static boolean isInnerClass(java.lang.Class<?> clazz)
-
returnsVoid
public static boolean returnsVoid(java.lang.reflect.Method method)
-
isArray
public static boolean isArray(java.lang.Object obj)
Determine if the supplied object is an array.- Parameters:
obj
- the object to test; potentiallynull
- Returns:
true
if the object is an array
-
isAssignableTo
public static boolean isAssignableTo(java.lang.Object obj, java.lang.Class<?> type)
Determine if the supplied object can be assigned to the supplied type for the purpose of reflective method invocations.In contrast to
Class.isInstance(Object)
, this method returnstrue
if the supplied type represents a primitive type whose wrapper matches the supplied object's type.Returns
true
if the supplied object isnull
and the supplied type does not represent a primitive type.- Parameters:
obj
- the object to test for assignment compatibility; potentiallynull
type
- the type to check against; nevernull
- Returns:
true
if the object is assignment compatible- See Also:
Class.isInstance(Object)
,Class.isAssignableFrom(Class)
-
getWrapperType
public static java.lang.Class<?> getWrapperType(java.lang.Class<?> type)
Get the wrapper type for the supplied primitive type.- Parameters:
type
- the primitive type for which to retrieve the wrapper type- Returns:
- the corresponding wrapper type or
null
if the supplied type isnull
or not a primitive type
-
newInstance
public static <T> T newInstance(java.lang.Class<T> clazz, java.lang.Object... args)
-
newInstance
public static <T> T newInstance(java.lang.reflect.Constructor<T> constructor, java.lang.Object... args)
Create a new instance of typeT
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.
- Parameters:
constructor
- the constructor to invoke; nevernull
args
- the arguments to pass to the constructor- Returns:
- the new instance; never
null
- See Also:
newInstance(Class, Object...)
,ExceptionUtils.throwAsUncheckedException(Throwable)
-
readFieldValue
public static <T> java.util.Optional<java.lang.Object> readFieldValue(java.lang.Class<T> clazz, java.lang.String fieldName, T instance)
Read the value of a potentially inaccessible or nonexistent field.If the field does not exist, an exception occurs while reading it, or the value of the field is
null
, an emptyOptional
is returned.- Parameters:
clazz
- the class where the field is declared; nevernull
fieldName
- the name of the field; nevernull
or emptyinstance
- the instance from where the value is to be read; may benull
for a static field- See Also:
readFieldValue(Field)
,readFieldValue(Field, Object)
-
readFieldValue
public static java.util.Optional<java.lang.Object> readFieldValue(java.lang.reflect.Field field)
Read the value of a potentially inaccessible static field.If an exception occurs while reading the field or if the value of the field is
null
, an emptyOptional
is returned.- Parameters:
field
- the field to read; nevernull
- See Also:
readFieldValue(Field, Object)
,readFieldValue(Class, String, Object)
-
readFieldValue
public static <T> java.util.Optional<java.lang.Object> readFieldValue(java.lang.reflect.Field field, T instance)
Read the value of a potentially inaccessible field.If an exception occurs while reading the field or if the value of the field is
null
, an emptyOptional
is returned.- Parameters:
field
- the field to read; nevernull
instance
- the instance from which the value is to be read; may benull
for a static field- See Also:
readFieldValue(Field)
,readFieldValue(Class, String, Object)
-
invokeMethod
public static java.lang.Object invokeMethod(java.lang.reflect.Method method, java.lang.Object target, java.lang.Object... args)
-
loadClass
public static java.util.Optional<java.lang.Class<?>> loadClass(java.lang.String name)
- See Also:
ReflectionSupport.loadClass(String)
-
loadClass
public static java.util.Optional<java.lang.Class<?>> loadClass(java.lang.String name, java.lang.ClassLoader classLoader)
Load a class by its primitive name or fully qualified name, using the suppliedClassLoader
.See
ReflectionSupport.loadClass(String)
for details on support for class names for arrays.- Parameters:
name
- the name of the class to load; nevernull
or blankclassLoader
- theClassLoader
to use; nevernull
- See Also:
loadClass(String)
-
getFullyQualifiedMethodName
public static java.lang.String getFullyQualifiedMethodName(java.lang.Class<?> clazz, java.lang.String methodName, java.lang.Class<?>... parameterTypes)
Build the fully qualified method name for the method described by the supplied class, method name, and parameter types.- Parameters:
clazz
- the class that declares the method; nevernull
methodName
- the name of the method; nevernull
or blankparameterTypes
- the parameter types of the method; may benull
or empty- Returns:
- fully qualified method name; never
null
-
getOutermostInstance
public static java.util.Optional<java.lang.Object> getOutermostInstance(java.lang.Object inner, java.lang.Class<?> requiredType)
Get the outermost instance of the required type, searching recursively through enclosing instances.If the supplied inner object is of the required type, it will simply be returned.
- Parameters:
inner
- the inner object from which to begin the search; nevernull
requiredType
- the required type of the outermost instance; nevernull
- Returns:
- an
Optional
containing the outermost instance; nevernull
but potentially empty
-
getAllClasspathRootDirectories
public static java.util.Set<java.nio.file.Path> getAllClasspathRootDirectories()
-
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)
-
findAllClassesInClasspathRoot
public static java.util.List<java.lang.Class<?>> findAllClassesInClasspathRoot(java.net.URI root, ClassFilter classFilter)
- Since:
- 1.1
-
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)
-
findAllClassesInPackage
public static java.util.List<java.lang.Class<?>> findAllClassesInPackage(java.lang.String basePackageName, ClassFilter classFilter)
- Since:
- 1.1
-
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)
- Since:
- 1.1.1
- See Also:
ReflectionSupport.findAllClassesInModule(String, Predicate, Predicate)
-
findAllClassesInModule
public static java.util.List<java.lang.Class<?>> findAllClassesInModule(java.lang.String moduleName, ClassFilter classFilter)
- Since:
- 1.1.1
-
findNestedClasses
public static java.util.List<java.lang.Class<?>> findNestedClasses(java.lang.Class<?> clazz, java.util.function.Predicate<java.lang.Class<?>> predicate)
-
getDeclaredConstructor
public static <T> java.lang.reflect.Constructor<T> getDeclaredConstructor(java.lang.Class<T> clazz)
Get the sole declaredConstructor
for the supplied class.Throws a
PreconditionViolationException
if the supplied class declares more than one constructor.- Parameters:
clazz
- the class to get the constructor for- Returns:
- the sole declared constructor; never
null
- See Also:
Class.getDeclaredConstructors()
-
findConstructors
public static java.util.List<java.lang.reflect.Constructor<?>> findConstructors(java.lang.Class<?> clazz, java.util.function.Predicate<java.lang.reflect.Constructor<?>> predicate)
Find all constructors in the supplied class that match the supplied predicate.- Parameters:
clazz
- the class in which to search for constructors; nevernull
predicate
- the predicate to use to test for a match; nevernull
- Returns:
- an immutable list of all such constructors found; never
null
but potentially empty
-
findFields
public static java.util.List<java.lang.reflect.Field> findFields(java.lang.Class<?> clazz, java.util.function.Predicate<java.lang.reflect.Field> predicate, ReflectionUtils.HierarchyTraversalMode traversalMode)
Find all fields of the supplied class or interface that match the specifiedpredicate
.The results will not contain fields that are hidden.
- Parameters:
clazz
- the class or interface in which to find the fields; nevernull
predicate
- the field filter; nevernull
traversalMode
- the hierarchy traversal mode; nevernull
- Returns:
- an immutable list of all such fields found; never
null
but potentially empty
-
isMethodPresent
public static boolean isMethodPresent(java.lang.Class<?> clazz, java.util.function.Predicate<java.lang.reflect.Method> predicate)
Determine if aMethod
matching the suppliedPredicate
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.- Parameters:
clazz
- the class or interface in which to find the method; nevernull
predicate
- the predicate to use to test for a match; nevernull
- Returns:
true
if such a method is present- See Also:
findMethod(Class, String, String)
,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.String parameterTypeNames)
-
findMethod
public static java.util.Optional<java.lang.reflect.Method> findMethod(java.lang.Class<?> clazz, java.lang.String methodName, java.lang.Class<?>... parameterTypes)
-
findMethods
public static java.util.List<java.lang.reflect.Method> findMethods(java.lang.Class<?> clazz, java.util.function.Predicate<java.lang.reflect.Method> predicate)
Find all methods of the supplied class or interface that match the specifiedpredicate
, 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.- Parameters:
clazz
- the class or interface in which to find the methods; nevernull
predicate
- the method filter; nevernull
- Returns:
- an immutable list of all such methods found; never
null
- See Also:
ReflectionUtils.HierarchyTraversalMode.TOP_DOWN
,findMethods(Class, Predicate, HierarchyTraversalMode)
-
findMethods
public static java.util.List<java.lang.reflect.Method> findMethods(java.lang.Class<?> clazz, java.util.function.Predicate<java.lang.reflect.Method> predicate, ReflectionUtils.HierarchyTraversalMode traversalMode)
-
makeAccessible
public static <T extends java.lang.reflect.AccessibleObject> T makeAccessible(T object)
-
getAllAssignmentCompatibleClasses
public static java.util.Set<java.lang.Class<?>> getAllAssignmentCompatibleClasses(java.lang.Class<?> clazz)
Return all classes and interfaces that can be used as assignment types for instances of the specifiedClass
, including itself.- Parameters:
clazz
- theClass
to look up- See Also:
Class.isAssignableFrom(java.lang.Class<?>)
-
-