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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enum
Modes in which a hierarchy can be traversed — for example, when searching for methods or fields within a class hierarchy. -
Field Summary
FieldsModifier and TypeFieldDescriptionInternal cache of common class names mapped to their types.private static final ClasspathScanner
private static final Class<?>[]
private static final Logger
Set of fully qualified class names for which no cycles have been detected in inner class hierarchies.Internal cache of primitive types mapped to their wrapper types.private static final Pattern
private static final Pattern
private static final Pattern
Pattern: "[x", "[[[[x", etc., where x is Z, B, C, D, F, I, J, S, etc. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static int
defaultFieldSorter
(Field field1, Field field2) Field comparator inspired by JUnit 4'sorg.junit.internal.MethodSorter
implementation.private static int
defaultMethodSorter
(Method method1, Method method2) Method comparator based upon JUnit 4'sorg.junit.internal.MethodSorter
implementation.private static void
detectInnerClassCycle
(Class<?> clazz) 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 aJUnitException
if a cycle is detected.findAllClassesInClasspathRoot
(URI root, Predicate<Class<?>> classFilter, Predicate<String> classNameFilter) findAllClassesInClasspathRoot
(URI root, ClassFilter classFilter) findAllClassesInModule
(String moduleName, Predicate<Class<?>> classFilter, Predicate<String> classNameFilter) findAllClassesInModule
(String moduleName, ClassFilter classFilter) findAllClassesInPackage
(String basePackageName, Predicate<Class<?>> classFilter, Predicate<String> classNameFilter) findAllClassesInPackage
(String basePackageName, ClassFilter classFilter) findAllFieldsInHierarchy
(Class<?> clazz, ReflectionUtils.HierarchyTraversalMode traversalMode) findAllMethodsInHierarchy
(Class<?> clazz, ReflectionUtils.HierarchyTraversalMode traversalMode) Find all non-synthetic methods in the superclass and interface hierarchy, excluding Object.static List
<Constructor<?>> findConstructors
(Class<?> clazz, Predicate<Constructor<?>> predicate) Find all constructors in the supplied class that match the supplied predicate.findFields
(Class<?> clazz, Predicate<Field> predicate, ReflectionUtils.HierarchyTraversalMode traversalMode) findMethod
(Class<?> clazz, String methodName, Class<?>... parameterTypes) findMethod
(Class<?> clazz, String methodName, String parameterTypeNames) findMethod
(Class<?> clazz, Predicate<Method> predicate) findMethods
(Class<?> clazz, Predicate<Method> predicate) Find all methods of the supplied class or interface that match the specifiedpredicate
, using top-down search semantics within the type hierarchy.findMethods
(Class<?> clazz, Predicate<Method> predicate, ReflectionUtils.HierarchyTraversalMode traversalMode) findNestedClasses
(Class<?> clazz, Predicate<Class<?>> predicate) private static void
getAllAssignmentCompatibleClasses
(Class<?> clazz) Return all classes and interfaces that can be used as assignment types for instances of the specifiedClass
, including itself.private static void
getAllAssignmentCompatibleClasses
(Class<?> clazz, Set<Class<?>> result) static <T> Constructor
<T> getDeclaredConstructor
(Class<T> clazz) Get the sole declared, non-syntheticConstructor
for the supplied class.getDeclaredFields
(Class<?> clazz) Custom alternative toClass.getDeclaredFields()
that sorts the fields and converts them to a mutable list.getDeclaredMethods
(Class<?> clazz, ReflectionUtils.HierarchyTraversalMode traversalMode) Custom alternative toClass.getDeclaredMethods()
that sorts the methods and converts them to a mutable 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.Custom alternative toClass.getFields()
that sorts the fields and converts them to a mutable list.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.getInterfaceFields
(Class<?> clazz, ReflectionUtils.HierarchyTraversalMode traversalMode) getInterfaceMethods
(Class<?> clazz, ReflectionUtils.HierarchyTraversalMode traversalMode) Deprecated.getMethods
(Class<?> clazz) Custom alternative toClass.getMethods()
that sorts the methods and converts them to a mutable list.getOuterInstance
(Object inner) getOutermostInstance
(Object inner, Class<?> requiredType) Deprecated.Please discontinue use of this method since it relies on internal implementation details of the JDK that may not work in the future.static Method
getRequiredMethod
(Class<?> clazz, String methodName, Class<?>... parameterTypes) Find the firstMethod
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.getSuperclassFields
(Class<?> clazz, ReflectionUtils.HierarchyTraversalMode traversalMode) getSuperclassMethods
(Class<?> clazz, ReflectionUtils.HierarchyTraversalMode traversalMode) private static Throwable
Get the underlying cause of the suppliedThrowable
.static Class
<?> getWrapperType
(Class<?> 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.static Object
invokeMethod
(Method method, Object target, Object... args) static boolean
isAbstract
(Class<?> clazz) static boolean
isAbstract
(Member member) static boolean
Determine if the supplied object is an array.static boolean
isAssignableTo
(Class<?> sourceType, Class<?> 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.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.private static boolean
isFieldShadowedByLocalFields
(Field field, List<Field> localFields) static boolean
static boolean
(package private) static boolean
private static boolean
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) private static boolean
isMethodShadowedBy
(Method upper, Method lower) private static boolean
isMethodShadowedByLocalMethods
(Method method, List<Method> localMethods) static boolean
Determine if the supplied object is a multidimensional array.static boolean
isNotFinal
(Class<?> clazz) static boolean
isNotFinal
(Member member) static boolean
isNotPrivate
(Class<?> clazz) static boolean
isNotPrivate
(Member member) static boolean
isNotStatic
(Class<?> clazz) static boolean
isNotStatic
(Member member) static boolean
static boolean
static boolean
static boolean
private static boolean
isSearchable
(Class<?> clazz) Determine if the supplied class is searchable: is non-null and is not equal to the class reference forjava.lang.Object
.static boolean
static boolean
(package private) 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) Deprecated.Please usetryToLoadClass(String)
instead.loadClass
(String name, ClassLoader classLoader) Deprecated.Please usetryToLoadClass(String, ClassLoader)
instead.private static Class
<?> loadRequiredParameterType
(Class<?> clazz, String methodName, String typeName) static <T extends AccessibleObject>
TmakeAccessible
(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 typeT
by invoking the supplied constructor with the supplied arguments.static String[]
parseFullyQualifiedMethodName
(String fullyQualifiedMethodName) Parse the supplied fully qualified method name into a 3-elementString[]
with the following content.readFieldValue
(Class<T> clazz, String fieldName, T instance) Deprecated.Please usetryToReadFieldValue(Class, String, Object)
instead.readFieldValue
(Field field) Deprecated.Please usetryToReadFieldValue(Field)
instead.readFieldValue
(Field field, Object instance) Deprecated.Please usetryToReadFieldValue(Field, Object)
instead.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.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 suppliedpredicate
.static Class<?>[]
resolveParameterTypes
(Class<?> clazz, String methodName, String parameterTypeNames) static boolean
returnsVoid
(Method method) streamAllClassesInClasspathRoot
(URI root, Predicate<Class<?>> classFilter, Predicate<String> classNameFilter) streamAllClassesInClasspathRoot
(URI root, ClassFilter classFilter) streamAllClassesInModule
(String moduleName, Predicate<Class<?>> classFilter, Predicate<String> classNameFilter) streamAllClassesInModule
(String moduleName, ClassFilter classFilter) streamAllClassesInPackage
(String basePackageName, Predicate<Class<?>> classFilter, Predicate<String> classNameFilter) since 1.10streamAllClassesInPackage
(String basePackageName, ClassFilter classFilter) streamFields
(Class<?> clazz, Predicate<Field> predicate, ReflectionUtils.HierarchyTraversalMode traversalMode) streamMethods
(Class<?> clazz, Predicate<Method> predicate, ReflectionUtils.HierarchyTraversalMode traversalMode) streamNestedClasses
(Class<?> clazz, Predicate<Class<?>> predicate) since 1.10toSortedMutableList
(Field[] fields) toSortedMutableList
(Method[] methods) tryToGetMethod
(Class<?> clazz, String methodName, Class<?>... parameterTypes) Try to get theMethod
in the specified class with the specified name and parameter types.tryToLoadClass
(String name) tryToLoadClass
(String name, ClassLoader classLoader) Try to load a class by its primitive name or fully qualified name, using the suppliedClassLoader
.tryToReadFieldValue
(Class<T> clazz, String fieldName, T instance) Try to read the value of a potentially inaccessible or nonexistent field.tryToReadFieldValue
(Field field) Try to read the value of a potentially inaccessible static field.tryToReadFieldValue
(Field field, Object instance)
-
Field Details
-
logger
-
VM_INTERNAL_OBJECT_ARRAY_PATTERN
-
VM_INTERNAL_PRIMITIVE_ARRAY_PATTERN
Pattern: "[x", "[[[[x", etc., where x is Z, B, C, D, F, I, J, S, etc.The pattern intentionally captures the last bracket with the capital letter so that the combination can be looked up via
classNameToTypeMap
. For example, the last matched group will contain"[I"
instead of"I"
.- See Also:
-
SOURCE_CODE_SYNTAX_ARRAY_PATTERN
-
EMPTY_CLASS_ARRAY
-
classpathScanner
-
noCyclesDetectedCache
Set of fully qualified class names for which no cycles have been detected in inner class hierarchies.This serves as a cache to avoid repeated cycle detection for classes that have already been checked.
- Since:
- 1.6
- See Also:
-
classNameToTypeMap
Internal cache of common class names mapped to their types. -
primitiveToWrapperMap
Internal cache of primitive types mapped to their wrapper types.
-
-
Constructor Details
-
ReflectionUtils
private ReflectionUtils()
-
-
Method Details
-
isPublic
-
isPublic
-
isPrivate
-
isPrivate
-
isNotPrivate
-
isNotPrivate
-
isAbstract
-
isAbstract
-
isStatic
-
isNotStatic
-
isStatic
-
isNotStatic
-
isFinal
- Since:
- 1.5
-
isNotFinal
- Since:
- 1.5
-
isFinal
- Since:
- 1.5
-
isNotFinal
- Since:
- 1.5
-
isInnerClass
Determine if the supplied class is an inner class (i.e., a non-static member class).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.- Parameters:
clazz
- the class to check; nevernull
- Returns:
true
if the class is an inner class
-
returnsVoid
-
isArray
Determine if the supplied object is an array.- Parameters:
obj
- the object to test; potentiallynull
- Returns:
true
if the object is an array
-
isMultidimensionalArray
Determine if the supplied object is a multidimensional array.- Parameters:
obj
- the object to test; potentiallynull
- Returns:
true
if the object is a multidimensional array- Since:
- 1.3.2
-
isAssignableTo
Determine if an object of the supplied source type can be assigned to the supplied target type for the purpose of reflective method invocations.In contrast to
Class.isAssignableFrom(Class)
, this method returnstrue
if the target type represents a primitive type whose wrapper matches the supplied source type. In addition, this method also supports widening conversions for primitive target types.- Parameters:
sourceType
- the non-primitive target type; nevernull
targetType
- the target type; nevernull
- Returns:
true
if an object of the source type is assignment compatible with the target type- Since:
- 1.8
- See Also:
-
isAssignableTo
Determine if the supplied object can be assigned to the supplied target type for the purpose of reflective method invocations.In contrast to
Class.isInstance(Object)
, this method returnstrue
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 returnstrue
.- Parameters:
obj
- the object to test for assignment compatibility; potentiallynull
targetType
- the type to check against; nevernull
- Returns:
true
if the object is assignment compatible- See Also:
-
isWideningConversion
Determine if Java supports a widening primitive conversion from the supplied source type to the supplied primitive target type. -
getWrapperType
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
- See Also:
-
newInstance
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:
-
readFieldValue
@API(status=DEPRECATED, since="1.4") @Deprecated public static <T> Optional<Object> readFieldValue(Class<T> clazz, String fieldName, T instance) Deprecated.Please usetryToReadFieldValue(Class, String, Object)
instead.Read the value of a potentially inaccessible or nonexistent field.If the field does not exist or the value of the field is
null
, an emptyOptional
will be 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:
-
tryToReadFieldValue
@API(status=INTERNAL, since="1.4") public static <T> Try<Object> tryToReadFieldValue(Class<T> clazz, String fieldName, T instance) Try to read the value of a potentially inaccessible or nonexistent field.If the field does not exist or an exception occurs while reading it, a failed
Try
is returned that contains the corresponding exception.- 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- Since:
- 1.4
- See Also:
-
readFieldValue
@API(status=DEPRECATED, since="1.4") @Deprecated public static Optional<Object> readFieldValue(Field field) Deprecated.Please usetryToReadFieldValue(Field)
instead.Read the value of the supplied static field, making it accessible if necessary and masking any checked exception as an unchecked exception.If the value of the field is
null
, an emptyOptional
will be returned.- Parameters:
field
- the field to read; nevernull
- See Also:
-
tryToReadFieldValue
Try to read the value of a potentially inaccessible static field.If an exception occurs while reading the field, a failed
Try
is returned that contains the corresponding exception.- Parameters:
field
- the field to read; nevernull
- Since:
- 1.4
- See Also:
-
readFieldValue
@API(status=DEPRECATED, since="1.4") @Deprecated public static Optional<Object> readFieldValue(Field field, Object instance) Deprecated.Please usetryToReadFieldValue(Field, Object)
instead.Read the value of the supplied field, making it accessible if necessary and masking any checked exception as an unchecked exception.If the value of the field is
null
, an emptyOptional
will be 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:
-
tryToReadFieldValue
@API(status=INTERNAL, since="1.4") public static Try<Object> tryToReadFieldValue(Field field, Object instance) - Since:
- 1.4
- See Also:
-
readFieldValues
Read the values of the supplied fields, making each field accessible if necessary and masking any checked exception as an unchecked exception.- Parameters:
fields
- the list of fields to read; nevernull
instance
- the instance from which the values are to be read; may benull
for static fields- Returns:
- an immutable list of the values of the specified fields; never
null
but may be empty or containnull
entries
-
readFieldValues
public 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 suppliedpredicate
.- Parameters:
fields
- the list of fields to read; nevernull
instance
- the instance from which the values are to be read; may benull
for static fieldspredicate
- the field filter; nevernull
- Returns:
- an immutable list of the values of the specified fields; never
null
but may be empty or containnull
entries
-
invokeMethod
- See Also:
-
loadClass
@API(status=DEPRECATED, since="1.4") @Deprecated public static Optional<Class<?>> loadClass(String name) Deprecated.Please usetryToLoadClass(String)
instead.- See Also:
-
tryToLoadClass
- Since:
- 1.4
- See Also:
-
loadClass
@API(status=DEPRECATED, since="1.4") @Deprecated public static Optional<Class<?>> loadClass(String name, ClassLoader classLoader) Deprecated.Please usetryToLoadClass(String, ClassLoader)
instead.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:
-
tryToLoadClass
@API(status=INTERNAL, since="1.4") public static Try<Class<?>> tryToLoadClass(String name, ClassLoader classLoader) Try to load a class by its primitive name or fully qualified name, using the suppliedClassLoader
.See
ReflectionSupport.tryToLoadClass(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
- Since:
- 1.4
- See Also:
-
loadArrayType
private static Class<?> loadArrayType(ClassLoader classLoader, String componentTypeName, int dimensions) throws ClassNotFoundException - Throws:
ClassNotFoundException
-
getFullyQualifiedMethodName
Build the fully qualified method name for the method described by the supplied class and method.Note that the class is not necessarily the class in which the method is declared.
- Parameters:
clazz
- the class from which the method should be referenced; nevernull
method
- the method; nevernull
- Returns:
- fully qualified method name; never
null
- Since:
- 1.4
- See Also:
-
getFullyQualifiedMethodName
public 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.Note that the class is not necessarily the class in which the method is declared.
- Parameters:
clazz
- the class from which the method should be referenced; 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
- See Also:
-
parseFullyQualifiedMethodName
Parse the supplied fully qualified method name into a 3-elementString[]
with the following content.- index
0
: the fully qualified class name - index
1
: the name of the method - index
2
: a comma-separated list of parameter types, or a blank string if the method does not declare any formal parameters
- Parameters:
fullyQualifiedMethodName
- a fully qualified method name, nevernull
or blank- Returns:
- a 3-element array of strings containing the parsed values
- index
-
getOutermostInstance
@API(status=DEPRECATED, since="1.4") @Deprecated public static Optional<Object> getOutermostInstance(Object inner, Class<?> requiredType) Deprecated.Please discontinue use of this method since it relies on internal implementation details of the JDK that may not work in the future.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 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
-
getOuterInstance
-
getAllClasspathRootDirectories
-
findAllClassesInClasspathRoot
public static List<Class<?>> findAllClassesInClasspathRoot(URI root, Predicate<Class<?>> classFilter, Predicate<String> classNameFilter) - See Also:
-
streamAllClassesInClasspathRoot
public static Stream<Class<?>> streamAllClassesInClasspathRoot(URI root, Predicate<Class<?>> classFilter, Predicate<String> classNameFilter) - Since:
- 1.10
- See Also:
-
findAllClassesInClasspathRoot
- Since:
- 1.1
-
streamAllClassesInClasspathRoot
- Since:
- 1.10
-
findAllClassesInPackage
public static List<Class<?>> findAllClassesInPackage(String basePackageName, Predicate<Class<?>> classFilter, Predicate<String> classNameFilter) - See Also:
-
streamAllClassesInPackage
public static Stream<Class<?>> streamAllClassesInPackage(String basePackageName, Predicate<Class<?>> classFilter, Predicate<String> classNameFilter) since 1.10- See Also:
-
findAllClassesInPackage
public static List<Class<?>> findAllClassesInPackage(String basePackageName, ClassFilter classFilter) - Since:
- 1.1
-
streamAllClassesInPackage
public static Stream<Class<?>> streamAllClassesInPackage(String basePackageName, ClassFilter classFilter) - Since:
- 1.10
-
findAllClassesInModule
public static List<Class<?>> findAllClassesInModule(String moduleName, Predicate<Class<?>> classFilter, Predicate<String> classNameFilter) - Since:
- 1.1.1
- See Also:
-
streamAllClassesInModule
public static Stream<Class<?>> streamAllClassesInModule(String moduleName, Predicate<Class<?>> classFilter, Predicate<String> classNameFilter) - Since:
- 1.10
- See Also:
-
findAllClassesInModule
- Since:
- 1.1.1
-
streamAllClassesInModule
- Since:
- 1.10
-
findNestedClasses
- See Also:
-
streamNestedClasses
since 1.10- See Also:
-
findNestedClasses
-
detectInnerClassCycle
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 aJUnitException
if a cycle is detected.This method does not detect cycles within inner class hierarchies below the supplied class.
If the supplied class is not an inner class and does not have a searchable superclass, this method is effectively a no-op.
- Since:
- 1.6
- See Also:
-
getDeclaredConstructor
Get the sole declared, non-syntheticConstructor
for the supplied class.Throws a
PreconditionViolationException
if the supplied class declares more than one non-synthetic constructor.- Parameters:
clazz
- the class to get the constructor for- Returns:
- the sole declared constructor; never
null
- See Also:
-
findConstructors
public static List<Constructor<?>> findConstructors(Class<?> clazz, Predicate<Constructor<?>> predicate) Find all constructors in the supplied class that match the supplied predicate.Note that this method may return synthetic constructors. If you wish to ignore synthetic constructors, you may filter them out with the supplied
predicate
or filter them out of the list returned by this method.- 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 - See Also:
-
findFields
public static List<Field> findFields(Class<?> clazz, Predicate<Field> predicate, ReflectionUtils.HierarchyTraversalMode traversalMode) - See Also:
-
streamFields
public static Stream<Field> streamFields(Class<?> clazz, Predicate<Field> predicate, ReflectionUtils.HierarchyTraversalMode traversalMode) - Since:
- 1.10
- See Also:
-
findAllFieldsInHierarchy
private static List<Field> findAllFieldsInHierarchy(Class<?> clazz, ReflectionUtils.HierarchyTraversalMode traversalMode) -
isMethodPresent
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:
-
getMethod
@API(status=DEPRECATED, since="1.4") @Deprecated static Optional<Method> getMethod(Class<?> clazz, String methodName, Class<?>... parameterTypes) Deprecated.Please usetryToGetMethod(Class, String, Class[])
instead.Get theMethod
in the specified class with the specified name and parameter types.This method delegates to
Class.getMethod(String, Class...)
but swallows any exception thrown.- Parameters:
clazz
- the class in which to search for the method; nevernull
methodName
- the name of the method to get; nevernull
or blankparameterTypes
- the parameter types of the method; may benull
or empty- Returns:
- an
Optional
containing the method; nevernull
but empty if the invocation ofClass#getMethod()
throws aNoSuchMethodException
-
tryToGetMethod
@API(status=INTERNAL, since="1.4") public static Try<Method> tryToGetMethod(Class<?> clazz, String methodName, Class<?>... parameterTypes) Try to get theMethod
in the specified class with the specified name and parameter types.This method delegates to
Class.getMethod(String, Class...)
but catches any exception thrown.- Parameters:
clazz
- the class in which to search for the method; nevernull
methodName
- the name of the method to get; nevernull
or blankparameterTypes
- the parameter types of the method; may benull
or empty- Returns:
- a successful
Try
containing the method or a failedTry
containing theNoSuchMethodException
thrown byClass#getMethod()
; nevernull
- Since:
- 1.4
-
findMethod
public static Optional<Method> findMethod(Class<?> clazz, String methodName, String parameterTypeNames) - See Also:
-
resolveParameterTypes
-
loadRequiredParameterType
-
findMethod
public static Optional<Method> findMethod(Class<?> clazz, String methodName, Class<?>... parameterTypes) - See Also:
-
findMethod
-
getRequiredMethod
@API(status=STABLE, since="1.7") public static Method getRequiredMethod(Class<?> clazz, String methodName, Class<?>... parameterTypes) Find the firstMethod
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.Use this method as an alternative to
findMethod(Class, String, Class...)
for use cases in which the method is required to be present.- Parameters:
clazz
- the class or interface in which to find the method; nevernull
methodName
- the name of the method to find; nevernull
or emptyparameterTypes
- the types of parameters accepted by the method, if any; nevernull
- Returns:
- the
Method
found; nevernull
- Throws:
JUnitException
- if no method is found- Since:
- 1.7
- See Also:
-
findMethods
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:
-
findMethods
public static List<Method> findMethods(Class<?> clazz, Predicate<Method> predicate, ReflectionUtils.HierarchyTraversalMode traversalMode) - See Also:
-
streamMethods
public static Stream<Method> streamMethods(Class<?> clazz, Predicate<Method> predicate, ReflectionUtils.HierarchyTraversalMode traversalMode) - Since:
- 1.10
- See Also:
-
findAllMethodsInHierarchy
private static List<Method> findAllMethodsInHierarchy(Class<?> clazz, ReflectionUtils.HierarchyTraversalMode traversalMode) Find all non-synthetic methods in the superclass and interface hierarchy, excluding Object. -
getFields
Custom alternative toClass.getFields()
that sorts the fields and converts them to a mutable list. -
getDeclaredFields
Custom alternative toClass.getDeclaredFields()
that sorts the fields and converts them to a mutable list. -
getMethods
Custom alternative toClass.getMethods()
that sorts the methods and converts them to a mutable list. -
getDeclaredMethods
private static List<Method> getDeclaredMethods(Class<?> clazz, ReflectionUtils.HierarchyTraversalMode traversalMode) Custom alternative toClass.getDeclaredMethods()
that sorts the methods and converts them to a mutable list.In addition, the list returned by this method includes interface default methods which are either prepended or appended to the list of declared methods depending on the supplied traversal mode.
-
getDefaultMethods
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.- See Also:
-
toSortedMutableList
-
toSortedMutableList
-
defaultFieldSorter
Field comparator inspired by JUnit 4'sorg.junit.internal.MethodSorter
implementation. -
defaultMethodSorter
Method comparator based upon JUnit 4'sorg.junit.internal.MethodSorter
implementation. -
getInterfaceMethods
private static List<Method> getInterfaceMethods(Class<?> clazz, ReflectionUtils.HierarchyTraversalMode traversalMode) -
getInterfaceFields
private static List<Field> getInterfaceFields(Class<?> clazz, ReflectionUtils.HierarchyTraversalMode traversalMode) -
getSuperclassFields
private static List<Field> getSuperclassFields(Class<?> clazz, ReflectionUtils.HierarchyTraversalMode traversalMode) -
isFieldShadowedByLocalFields
-
getSuperclassMethods
private static List<Method> getSuperclassMethods(Class<?> clazz, ReflectionUtils.HierarchyTraversalMode traversalMode) -
isMethodShadowedByLocalMethods
-
isMethodShadowedBy
-
hasCompatibleSignature
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. -
isGeneric
-
isGeneric
-
makeAccessible
-
getAllAssignmentCompatibleClasses
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:
-
getAllAssignmentCompatibleClasses
-
isSearchable
Determine if the supplied class is searchable: is non-null and is not equal to the class reference forjava.lang.Object
.This method is often used to determine if a superclass should be searched but may be applicable for other use cases as well.
- Since:
- 1.6
-
getUnderlyingCause
Get the underlying cause of the suppliedThrowable
.If the supplied
Throwable
is an instance ofInvocationTargetException
, this method will be invoked recursively with the underlying target exception; otherwise, this method returns the suppliedThrowable
.
-
tryToGetMethod(Class, String, Class[])
instead.