Top Description Inners Fields Constructors Methods
org.junit.platform.commons.util

public final Class ReflectionUtils

extends Object
Class Inheritance
Annotations
@API
status:INTERNAL
since:1.0
Static Imports
java.util.Collections.synchronizedMap, java.util.stream.Collectors.toCollection, .Collectors.toSet, org.apiguardian.api.API.Status.INTERNAL, org.junit.platform.commons.util.ReflectionUtils.HierarchyTraversalMode.BOTTOM_UP, .ReflectionUtils.HierarchyTraversalMode.TOP_DOWN

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
org.junit.platform.commons.support.ReflectionSupport

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static enum
public static enum
ReflectionUtils.HierarchyTraversalMode

Modes in which a hierarchy can be traversed — for example, when searching for methods or fields within a class hierarchy.

Field Summary

Modifier and TypeField and Description
private static final Map<String, Class<?>>
classNameToTypeMap

Internal cache of common class names mapped to their types.

private static final ClasspathScanner
pack-priv static final Class<?>[]
private static final Map<Method, Method>
interfaceMethodCache

Cache for equivalent methods on an interface implemented by the declaring class.

private static final Logger
private static final Set<String>
noCyclesDetectedCache

Set of fully qualified class names for which no cycles have been detected in inner class hierarchies.

private static final Map<Class<?>, Class<?>>
primitiveToWrapperMap

Internal cache of primitive types mapped to their wrapper types.

private static final Pattern

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
private static Map<Class<?>, Class<?>>
private static int
defaultClassSorter(Class<?> class1, Class<?> class2)

Class comparator to achieve deterministic but nonobvious order.

private static int
defaultFieldSorter(Field field1, Field field2)

Field comparator inspired by JUnit 4's org.junit.internal.MethodSorter implementation.

private static int
defaultMethodSorter(Method method1, Method method2)

Method comparator based upon JUnit 4's org.junit.internal.MethodSorter implementation.

private static boolean
detectInnerClassCycle(Class<?> clazz, ReflectionUtils.CycleErrorHandling errorHandling)

Detect a cycle in the inner class hierarchy in which the supplied class resides — from the supplied class up to the outermost enclosing class — and throw a JUnitException if a cycle is detected.

public static List<Class<?>>
findAllClassesInClasspathRoot(URI root, Predicate<Class<?>> classFilter, Predicate<String> classNameFilter)

public static List<Class<?>>
public static List<Class<?>>
findAllClassesInModule(String moduleName, Predicate<Class<?>> classFilter, Predicate<String> classNameFilter)

public static List<Class<?>>
findAllClassesInModule(String moduleName, ClassFilter classFilter)

public static List<Class<?>>
findAllClassesInPackage(String basePackageName, Predicate<Class<?>> classFilter, Predicate<String> classNameFilter)

public static List<Class<?>>
findAllClassesInPackage(String basePackageName, ClassFilter classFilter)

private static List<Field>
private static List<Method>
findAllMethodsInHierarchy(Class<?> clazz, ReflectionUtils.HierarchyTraversalMode traversalMode)

Find all non-synthetic methods in the superclass and interface hierarchy, excluding Object.

public static List<Resource>
public static List<Resource>
findAllResourcesInModule(String moduleName, ResourceFilter resourceFilter)

public static List<Resource>
findAllResourcesInPackage(String basePackageName, ResourceFilter resourceFilter)

public static List<Constructor<?>>

Returns:

an immutable list of all such constructors found; never null but potentially empty
findConstructors
(Class<?>
the class in which to search for constructors; never null
clazz
,
Predicate<Constructor<?>>
the predicate to use to test for a match; never null
predicate
)

Find all constructors in the supplied class that match the supplied predicate.

public static List<Field>
private static Method
findInterfaceMethodIfPossible(Method method, Class<?>[] parameterTypes, Class<?> startClass, Class<?> endClass)

public static Optional<Method>
findMethod(Class<?> clazz, String methodName, @Nullable String parameterTypeNames)

public static Optional<Method>
findMethod(Class<?> clazz, String methodName, Class<?>... parameterTypes)

private static Optional<Method>
findMethod(Class<?> clazz, Predicate<Method> predicate)

public static List<Method>

Returns:

an immutable list of all such methods found; never null
findMethods
(Class<?>
the class or interface in which to find the methods; never null
clazz
,
Predicate<Method>
the method filter; never null
predicate
)

Find all methods of the supplied class or interface that match the specified predicate, using top-down search semantics within the type hierarchy.

public static List<Method>
public static List<Class<?>>
findNestedClasses(Class<?> clazz, Predicate<Class<?>> predicate)

public static List<Class<?>>
findNestedClasses(Class<?> clazz, Predicate<Class<?>> predicate, ReflectionUtils.CycleErrorHandling errorHandling)

public static Set<Class<?>>
getAllAssignmentCompatibleClasses(Class<?>
the Class to look up
clazz
)

Return all classes and interfaces that can be used as assignment types for instances of the specified Class, including itself.

private static void
public static Set<Path>
public static <T> Constructor<T>

Returns:

the sole declared constructor; never null
getDeclaredConstructor
(Class<T>
the class to get the constructor for
clazz
)

Get the sole declared, non-synthetic Constructor for the supplied class.

private static List<Field>
getDeclaredFields(Class<?> clazz)

Custom alternative to Class#getDeclaredFields() that sorts the fields and converts them to a mutable list.

private static List<Method>
getDeclaredMethods(Class<?> clazz, ReflectionUtils.HierarchyTraversalMode traversalMode)

Custom alternative to Class#getDeclaredMethods() that sorts the methods and converts them to a mutable list.

private static List<Method>
getDefaultMethods(Class<?> clazz)

Get a sorted, mutable list of all default methods present in interfaces implemented by the supplied class which are also visible within the supplied class.

public static String

Returns:

fully qualified method name; never null
getFullyQualifiedMethodName
(Class<?>
the class from which the method should be referenced; never null
clazz
,
Method
the method; never null
method
)

Build the fully qualified method name for the method described by the supplied class and method.

public static String

Returns:

fully qualified method name; never null
getFullyQualifiedMethodName
(Class<?>
the class from which the method should be referenced; never null
clazz
,
String
the name of the method; never null or blank
methodName
,
@Nullable Class<?>...
the parameter types of the method; may be null or empty
parameterTypes
)

Build the fully qualified method name for the method described by the supplied class, method name, and parameter types.

public static String

Returns:

fully qualified method name; never null
getFullyQualifiedMethodName
(String
the name of the class from which the method should be referenced; never null
className
,
String
the name of the method; never null or blank
methodName
,
String
the parameter type names of the method; may be empty but not null
parameterTypeNames
)

Build the fully qualified method name for the method described by the supplied class name, method name, and parameter types.

private static List<Field>
public static Method

Returns:

the corresponding interface method, or the original method if none found
getInterfaceMethodIfPossible
(Method
the method to be invoked, potentially from an implementation class; never null
method
,
@Nullable Class<?>
the target class to check for declared interfaces; potentially null
targetClass
)

Determine a corresponding interface method for the given method handle, if possible.

private static List<Method>
private static List<Method>
getMethods(Class<?> clazz)

Custom alternative to Class#getMethods() that sorts the methods and converts them to a mutable list.

public static Method

Returns:

the Method found; never null
getRequiredMethod
(Class<?>
the class or interface in which to find the method; never null
clazz
,
String
the name of the method to find; never null or empty
methodName
,
Class<?>...
the types of parameters accepted by the method, if any; never null
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.

private static List<Field>
private static List<Method>
pack-priv static Throwable
getUnderlyingCause(Throwable t)

Get the underlying cause of the supplied Throwable.

public static @Nullable Class<?>

Returns:

the corresponding wrapper type or null if the supplied type is not a primitive type
getWrapperType
(Class<?>
the primitive type for which to retrieve the wrapper type
type
)

Get the wrapper type for the supplied primitive type.

private static boolean
hasCompatibleSignature(Method candidate, String methodName, Class<?>[] parameterTypes)

Determine if the supplied candidate method (typically a method higher in the type hierarchy) has a signature that is compatible with a method that has the supplied name and parameter types, taking method sub-signatures and generics into account.

public static @Nullable Object
public static boolean
isAbstract(Class<?> clazz)

public static boolean
public static boolean

Returns:

true if the object is an array
isArray
(@Nullable Object
the object to test; potentially null
obj
)

Determine if the supplied object is an array.

public static boolean

Returns:

true if an object of the source type is assignment compatible with the target type
isAssignableTo
(Class<?>
the non-primitive target type; never null
sourceType
,
Class<?>
the target type; never null
targetType
)

Determine if an object of the supplied source type can be assigned to the supplied target type for the purpose of reflective method invocations.

public static boolean

Returns:

true if the object is assignment compatible
isAssignableTo
(@Nullable Object
the object to test for assignment compatibility; potentially null
obj
,
Class<?>
the type to check against; never null
targetType
)

Determine if the supplied object can be assigned to the supplied target type for the purpose of reflective method invocations.

private static boolean
public static boolean
public static boolean
isFinal(Class<?> clazz)

public static boolean
isFinal(Member member)

pack-priv static boolean
isGeneric(Method method)

private static boolean
public static boolean

Returns:

true if the class is an inner class
isInnerClass
(Class<?>
the class to check; never null
clazz
)

Determine if the supplied class is an inner class (i.e., a non-static member class).

private static boolean
public static boolean

Returns:

true if such a method is present
isMethodPresent
(Class<?>
the class or interface in which to find the method; never null
clazz
,
Predicate<Method>
the predicate to use to test for a match; never null
predicate
)

Determine if a 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.

public static boolean

Returns:

true if the object is a multidimensional array
isMultidimensionalArray
(@Nullable Object
the object to test; potentially null
obj
)

Determine if the supplied object is a multidimensional array.

public static boolean

Returns:

true if such a nested class is present
isNestedClassPresent
(Class<?>
the class to be searched; never null
clazz
,
Predicate<Class<?>>
the predicate against which the list of nested classes is checked; never null
predicate
,
ReflectionUtils.CycleErrorHandling errorHandling)

Determine if a nested class within the supplied class, or inherited by the supplied class, that conforms to the supplied predicate is present.

public static boolean
isNotAbstract(Class<?> clazz)

public static boolean
public static boolean
isNotFinal(Class<?> clazz)

public static boolean
private static boolean
public static boolean
isNotPrivate(Class<?> clazz)

public static boolean
public static boolean
isNotStatic(Class<?> clazz)

public static boolean
public static boolean
public static boolean
isPrivate(Class<?> clazz)

public static boolean
isPrivate(Member member)

public static boolean
isPublic(Class<?> clazz)

public static boolean
isPublic(Member member)

public static boolean

Returns:

whether the supplied clazz is a record class
isRecordClass
(Class<?> clazz)

Returns whether the supplied clazz is a record class.

public static boolean

Returns:

whether the supplied object is an instance of a record class
isRecordObject
(@Nullable Object object)

Returns whether the supplied object is an instance of a record class.

private static boolean
isSearchable(@Nullable Class<?> clazz)

Determine if the supplied class is searchable: is non-null and is not equal to the class reference for java.lang.Object.

public static boolean
isStatic(Class<?> clazz)

public static boolean
isStatic(Member member)

pack-priv static boolean
isWideningConversion(Class<?> sourceType, Class<?> targetType)

Determine if Java supports a widening primitive conversion from the supplied source type to the supplied primitive target type.

private static Class<?>
loadArrayType(ClassLoader classLoader, String componentTypeName, int dimensions)

public static Class<?>
loadRequiredClass(String
the name of the class to load; never null or blank
name
,
ClassLoader
the ClassLoader to use; never null
classLoader
)

Load a class by its primitive name or fully qualified name, using the supplied ClassLoader.

private static Class<?>
loadRequiredParameterType(Class<?> clazz, String methodName, String typeName)

public static <T extends Executable> T
makeAccessible(T executable)

public static Field
public static <T> T
newInstance(Class<T> clazz, Object... args)

public static <T> T

Returns:

the new instance; never null
newInstance
(Constructor<T>
the constructor to invoke; never null
constructor
,
@Nullable Object...
the arguments to pass to the constructor
args
)

Create a new instance of type T by invoking the supplied constructor with the supplied arguments.

public static String[]

Returns:

a 2-element array of strings containing the parsed values
parseFullyQualifiedFieldName
(String
a fully qualified field name, never null or blank
fullyQualifiedFieldName
)

Parse the supplied fully qualified field name into a 2-element String[] with the following content.

public static String[]

Returns:

a 3-element array of strings containing the parsed values
parseFullyQualifiedMethodName
(String
a fully qualified method name, never null or blank
fullyQualifiedMethodName
)

Parse the supplied fully qualified method name into a 3-element String[] with the following content.

public static List<?>

Returns:

an immutable list of the values of the specified fields; never null but may be empty or contain null entries
readFieldValues
(List<Field>
the list of fields to read; never null
fields
,
@Nullable Object
the instance from which the values are to be read; may be null for static fields
instance
)

Read the values of the supplied fields, making each field accessible if necessary and masking any checked exception as an unchecked exception.

public static List<?>

Returns:

an immutable list of the values of the specified fields; never null but may be empty or contain null entries
readFieldValues
(List<Field>
the list of fields to read; never null
fields
,
@Nullable Object
the instance from which the values are to be read; may be null for static fields
instance
,
Predicate<Field>
the field filter; never null
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.

public static Class<?>[]
resolveParameterTypes(Class<?> clazz, String methodName, @Nullable String parameterTypeNames)

public static boolean

Returns:

true if the method's return type is void
returnsPrimitiveVoid
(Method
the method to test; never null
method
)

Determine if the return type of the supplied method is primitive void.

public static Stream<Class<?>>
streamAllClassesInClasspathRoot(URI root, Predicate<Class<?>> classFilter, Predicate<String> classNameFilter)

public static Stream<Class<?>>
public static Stream<Class<?>>
streamAllClassesInModule(String moduleName, Predicate<Class<?>> classFilter, Predicate<String> classNameFilter)

public static Stream<Class<?>>
streamAllClassesInModule(String moduleName, ClassFilter classFilter)

public static Stream<Class<?>>
streamAllClassesInPackage(String basePackageName, Predicate<Class<?>> classFilter, Predicate<String> classNameFilter)

since 1.10

public static Stream<Class<?>>
streamAllClassesInPackage(String basePackageName, ClassFilter classFilter)

public static Stream<Resource>
public static Stream<Resource>
streamAllResourcesInModule(String moduleName, ResourceFilter resourceFilter)

public static Stream<Resource>
streamAllResourcesInPackage(String basePackageName, ResourceFilter resourceFilter)

public static Stream<Field>
public static Stream<Method>
public static Stream<Class<?>>
streamNestedClasses(Class<?> clazz, Predicate<Class<?>> predicate)

public static Stream<Class<?>>
private static List<Field>
private static List<Method>
private static List<Class<?>>
toSortedMutableList(Class<?>[] classes)

private static <T> List<T>
toSortedMutableList(T[] items, Comparator<? super T> comparator)

public static Try<Method>

Returns:

a successful Try containing the method or a failed Try containing the NoSuchMethodException thrown by Class#getMethod(); never null
tryToGetMethod
(Class<?>
the class in which to search for the method; never null
clazz
,
String
the name of the method to get; never null or blank
methodName
,
@Nullable Class<?>...
the parameter types of the method; may be null or empty
parameterTypes
)

Try to get the Method in the specified class with the specified name and parameter types.

public static Try<Set<Resource>>
tryToGetResources(String classpathResourceName)

public static Try<Set<Resource>>
tryToGetResources(String classpathResourceName, ClassLoader classLoader)

public static Try<Class<?>>
public static Try<Class<?>>
tryToLoadClass(String
the name of the class to load; never null or blank
name
,
ClassLoader
the ClassLoader to use; never null
classLoader
)

Try to load a class by its primitive name or fully qualified name, using the supplied ClassLoader.

public static <T> Try<Object>
tryToReadFieldValue(Class<T>
the class where the field is declared; never null
clazz
,
String
the name of the field; never null or empty
fieldName
,
@Nullable T
the instance from where the value is to be read; may be null for a static field
instance
)

Try to read the value of a potentially inaccessible or nonexistent field.

public static Try<@Nullable Object>
tryToReadFieldValue(Field
the field to read; never null
field
)

Try to read the value of a potentially inaccessible static field.

public static Try<@Nullable Object>
private static void
visitAllNestedClasses(Class<?> clazz, Predicate<Class<?>> predicate, Consumer<Class<?>> consumer, ReflectionUtils.CycleErrorHandling errorHandling)

Visit all nested classes without support for short-circuiting in order to ensure all of them are checked for class cycles.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait