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.
org.junit.platform.commons.support.ReflectionSupport
| Modifier and Type | Class and Description |
|---|---|
| public static enum | |
| public static enum | ReflectionUtils.
Modes in which a hierarchy can be traversed — for example, when searching for methods or fields within a class hierarchy. |
| Modifier and Type | Field and Description |
|---|---|
| private static final Map | classNameToTypeMap
Internal cache of common class names mapped to their types. |
| private static final ClasspathScanner | |
| pack-priv static final Class | |
| private static final Map | interfaceMethodCache
Cache for equivalent methods on an interface implemented by the declaring class. |
| private static final Logger | |
| private static final Set | noCyclesDetectedCache
Set of fully qualified class names for which no cycles have been detected in inner class hierarchies. |
| private static final Map | primitiveToWrapperMap
Internal cache of primitive types mapped to their wrapper types. |
| private static final Pattern |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| private static Map | |
| 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 |
| private static int | defaultMethodSorter(Method method1, Method method2)
Method comparator based upon JUnit 4's |
| private static boolean | detectInnerClassCycle(Class<?> clazz, ReflectionUtils.
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 |
| public static List | |
| public static List | |
| public static List | |
| public static List | |
| public static List | |
| public static List | |
| private static List | |
| private static List | findAllMethodsInHierarchy(Class<?> clazz, ReflectionUtils.
Find all non-synthetic methods in the superclass and interface hierarchy, excluding Object. |
| public static List | |
| public static List | |
| public static List | |
| public static List | Returns: an immutable list of all such constructors found; nevernull
but potentially emptythe class in which to search for constructors; never clazz, Predicate<Constructor<?>> nullthe predicate to use to test for a match; never predicate)nullFind all constructors in the supplied class that match the supplied predicate. |
| public static List | findFields(Class<?> clazz, Predicate<Field> predicate, ReflectionUtils.
|
| private static Method | findInterfaceMethodIfPossible(Method method, Class<?>[] parameterTypes, Class<?> startClass, Class<?> endClass)
|
| public static Optional | |
| public static Optional | |
| private static Optional | |
| public static List | Returns: an immutable list of all such methods found; nevernullthe class or interface in which to find the methods; never clazz, Predicate<Method> nullthe method filter; never predicate)nullFind all methods of the supplied class or interface
that match the specified |
| public static List | findMethods(Class<?> clazz, Predicate<Method> predicate, ReflectionUtils.
|
| public static List | |
| public static List | findNestedClasses(Class<?> clazz, Predicate<Class<?>> predicate, ReflectionUtils.
|
| public static Set | getAllAssignmentCompatibleClasses(Class<?>
the clazz)Class to look upReturn all classes and interfaces that can be used as assignment types
for instances of the specified |
| private static void | |
| public static Set | |
| public static <T> Constructor | Returns: the sole declared constructor; nevernullthe class to get the constructor for clazz)Get the sole declared, non-synthetic |
| private static List | getDeclaredFields(Class<?> clazz)
Custom alternative to |
| private static List | getDeclaredMethods(Class<?> clazz, ReflectionUtils.
Custom alternative to |
| private static List | 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; nevernullthe class from which the method should be referenced; never clazz, Method nullthe method; never method)nullBuild the fully qualified method name for the method described by the supplied class and method. |
| public static String | Returns: fully qualified method name; nevernullthe class from which the method should be referenced; never clazz, String nullthe name of the method; never methodName, @Nullable Class<?>... null or blankthe parameter types of the method; may be parameterTypes)null or emptyBuild 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; nevernullthe name of the class from which the method should be referenced;
never className, String nullthe name of the method; never methodName, String null or blankthe parameter type names of the method; may be
empty but not parameterTypeNames)nullBuild the fully qualified method name for the method described by the supplied class name, method name, and parameter types. |
| private static List | |
| public static Method | Returns: the corresponding interface method, or the original method if none foundthe method to be invoked, potentially from an implementation class;
never method, @Nullable Class<?> nullthe target class to check for declared interfaces;
potentially targetClass)nullDetermine a corresponding interface method for the given method handle, if possible. |
| private static List | |
| private static List | getMethods(Class<?> clazz)
Custom alternative to |
| public static Method | Returns: theMethod found; never nullthe class or interface in which to find the method;
never clazz, String nullthe name of the method to find; never methodName, Class<?>... null
or emptythe types of parameters accepted by the method,
if any; never parameterTypes)nullFind the first |
| private static List | |
| private static List | |
| pack-priv static Throwable | |
| public static @Nullable Class | Returns: the corresponding wrapper type ornull if the
supplied type is not a primitive typethe 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 | |
| public static boolean | |
| public static boolean | |
| public static boolean | Returns: true if an object of the source type is assignment compatible
with the target typethe non-primitive target type; never sourceType, Class<?> nullthe target type; never targetType)nullDetermine 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 compatiblethe object to test for assignment compatibility; potentially obj, Class<?> nullthe type to check against; never targetType)nullDetermine 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 | |
| public static boolean | |
| pack-priv static boolean | |
| private static boolean | |
| public static boolean | Returns: true if the class is an inner classthe class to check; never clazz)nullDetermine 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 presentthe class or interface in which to find the method; never
clazz, Predicate<Method> nullthe predicate to use to test for a match; never
predicate)nullDetermine if a |
| public static boolean | Returns: true if the object is a multidimensional arraythe object to test; potentially obj)nullDetermine if the supplied object is a multidimensional array. |
| public static boolean | Returns: true if such a nested class is presentthe class to be searched; never clazz, Predicate<Class<?>> nullthe predicate against which the list of nested classes is
checked; never predicate, ReflectionUtils.nullDetermine 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 | |
| public static boolean | |
| public static boolean | |
| public static boolean | |
| private static boolean | |
| public static boolean | |
| public static boolean | |
| public static boolean | |
| public static boolean | |
| public static boolean | |
| public static boolean | |
| public static boolean | |
| public static boolean | |
| public static boolean | |
| public static boolean | Returns: whether the suppliedclazz is a record classReturns whether the supplied |
| public static boolean | Returns: whether the suppliedobject is an instance of a record classReturns whether the supplied |
| 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 |
| public static boolean | |
| public static boolean | |
| 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 | |
| public static Class | loadRequiredClass(String
the name of the class to load; never name, ClassLoader null or blankthe classLoader)ClassLoader to use; never nullLoad a class by its primitive name or fully qualified name,
using the supplied |
| private static Class | |
| public static <T extends Executable> T | |
| public static Field | |
| public static <T> T | |
| public static <T> T | Returns: the new instance; nevernullthe constructor to invoke; never constructor, @Nullable Object... nullthe arguments to pass to the constructor args)Create a new instance of type |
| public static String[] | Returns: a 2-element array of strings containing the parsed valuesa fully qualified field name,
never fullyQualifiedFieldName)null or blankParse the supplied fully qualified field name into a 2-element
|
| public static String[] | Returns: a 3-element array of strings containing the parsed valuesa fully qualified method name,
never fullyQualifiedMethodName)null or blankParse the supplied fully qualified method name into a 3-element
|
| public static List | Returns: an immutable list of the values of the specified fields; nevernull but may be empty or contain null entriesthe list of fields to read; never fields, @Nullable Object nullthe instance from which the values are to be read; may
be instance)null for static fieldsRead 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; nevernull but may be empty or contain null entriesthe list of fields to read; never fields, @Nullable Object nullthe instance from which the values are to be read; may
be instance, Predicate<Field> null for static fieldsthe field filter; never predicate)nullRead 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 |
| public static Class | |
| public static boolean | Returns: true if the method's return type is voidthe method to test; never method)nullDetermine if the return type of the supplied method is primitive |
| public static Stream | |
| public static Stream | |
| public static Stream | |
| public static Stream | |
| public static Stream | |
| public static Stream | |
| public static Stream | |
| public static Stream | |
| public static Stream | |
| public static Stream | streamFields(Class<?> clazz, Predicate<Field> predicate, ReflectionUtils.
|
| public static Stream | streamMethods(Class<?> clazz, Predicate<Method> predicate, ReflectionUtils.
|
| public static Stream | |
| public static Stream | streamNestedClasses(Class<?> clazz, Predicate<Class<?>> predicate, ReflectionUtils.
|
| private static List | |
| private static List | |
| private static List | |
| private static <T> List | |
| public static Try | Returns: a successfulTry containing the method or a failed
Try containing the NoSuchMethodException thrown by
Class#getMethod(); never nullthe class in which to search for the method; never clazz, String nullthe name of the method to get; never methodName, @Nullable Class<?>... null or blankthe parameter types of the method; may be parameterTypes)null
or emptyTry to get the |
| public static Try | |
| public static Try | |
| public static Try | |
| public static Try | tryToLoadClass(String
the name of the class to load; never name, ClassLoader null or blankthe classLoader)ClassLoader to use; never nullTry to load a class by its primitive name or fully qualified
name, using the supplied |
| public static <T> Try | tryToReadFieldValue(Class<T>
the class where the field is declared; never clazz, String nullthe name of the field; never fieldName, @Nullable T null or emptythe instance from where the value is to be read; may
be instance)null for a static fieldTry to read the value of a potentially inaccessible or nonexistent field. |
| public static Try | tryToReadFieldValue(Field
the field to read; never field)nullTry to read the value of a potentially inaccessible static field. |
| public static Try | |
| private static void | visitAllNestedClasses(Class<?> clazz, Predicate<Class<?>> predicate, Consumer<Class<?>> consumer, ReflectionUtils.
Visit all nested classes without support for short-circuiting in order to ensure all of them are checked for class cycles. |