@API(value=Internal) public final class ReflectionUtils extends java.lang.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!
| Modifier and Type | Class and Description |
|---|---|
static class |
ReflectionUtils.MethodSortOrder |
| Modifier and Type | Method and Description |
|---|---|
static java.util.List<java.lang.Class<?>> |
findAllClassesInClasspathRoot(java.io.File root,
java.util.function.Predicate<java.lang.Class<?>> classTester) |
static java.util.List<java.lang.Class<?>> |
findAllClassesInPackage(java.lang.String basePackageName,
java.util.function.Predicate<java.lang.Class<?>> classTester) |
static java.util.Optional<java.lang.reflect.Method> |
findMethod(java.lang.Class<?> clazz,
java.lang.String methodName,
java.lang.Class<?>... parameterTypes) |
static java.util.List<java.lang.reflect.Method> |
findMethods(java.lang.Class<?> clazz,
java.util.function.Predicate<java.lang.reflect.Method> predicate) |
static java.util.List<java.lang.reflect.Method> |
findMethods(java.lang.Class<?> clazz,
java.util.function.Predicate<java.lang.reflect.Method> predicate,
ReflectionUtils.MethodSortOrder sortOrder) |
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 specified
Class, including itself. |
static java.util.Set<java.io.File> |
getAllClasspathRootDirectories() |
static <T> java.lang.reflect.Constructor<T> |
getDeclaredConstructor(java.lang.Class<T> clazz)
Get the sole declared
Constructor for the supplied class. |
static java.lang.ClassLoader |
getDefaultClassLoader() |
static java.util.Optional<java.lang.reflect.Method> |
getMethod(java.lang.Class<?> clazz,
java.lang.String methodName,
java.lang.Class<?>... parameterTypes) |
static java.util.Optional<java.lang.Object> |
getOuterInstance(java.lang.Object inner,
java.lang.Class<?> targetType) |
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)
Invoke the supplied method, making it accessible if necessary and
masking any
checked exception as an unchecked exception.
|
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 |
isPackage(java.lang.String packageName) |
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)
Load a class by its fully qualified name.
|
static java.util.Optional<java.lang.Class<?>> |
loadClass(java.lang.String name,
java.lang.ClassLoader classLoader)
Load a class by its fully qualified name, using the supplied
ClassLoader. |
static java.util.Optional<java.lang.reflect.Method> |
loadMethod(java.lang.String fullyQualifiedMethodName)
Load a method by its fully qualified name.
|
static <T> T |
newInstance(java.lang.Class<T> clazz,
java.lang.Object... args)
Create a new instance of the specified
Class by invoking
the constructor whose argument list matches the types of the supplied
arguments. |
static <T> T |
newInstance(java.lang.reflect.Constructor<T> constructor,
java.lang.Object... args)
Create a new instance of type
T 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 field.
|
public static java.lang.ClassLoader getDefaultClassLoader()
public static boolean isPublic(java.lang.Class<?> clazz)
public static boolean isPublic(java.lang.reflect.Member member)
public static boolean isPrivate(java.lang.Class<?> clazz)
public static boolean isPrivate(java.lang.reflect.Member member)
public static boolean isAbstract(java.lang.Class<?> clazz)
public static boolean isAbstract(java.lang.reflect.Member member)
public static boolean isStatic(java.lang.Class<?> clazz)
public static boolean isStatic(java.lang.reflect.Member member)
public static boolean isArray(java.lang.Object obj)
obj - the object to test; potentially nulltrue if the object is an arraypublic static boolean isAssignableTo(java.lang.Object obj,
java.lang.Class<?> type)
In contrast to Class.isInstance(Object), this method returns
true if the supplied type represents a primitive type whose
wrapper matches the supplied object's type.
Returns true if the supplied object is null and the
supplied type does not represent a primitive type.
obj - the object to test for assignment compatibility; potentially nulltype - the type to check against; never nulltrue if the object is assignment compatibleClass.isInstance(Object),
Class.isAssignableFrom(Class)public static java.lang.Class<?> getWrapperType(java.lang.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(java.lang.Class<T> clazz,
java.lang.Object... args)
Class by invoking
the constructor whose argument list matches the types of the supplied
arguments.
The constructor will be made accessible if necessary, and any checked exception will be masked as an unchecked exception.
clazz - the class to instantiate; never nullargs - the arguments to pass to the constructor none of which may be nullnewInstance(Constructor, Object...),
ExceptionUtils.throwAsUncheckedException(Throwable)public static <T> T newInstance(java.lang.reflect.Constructor<T> constructor,
java.lang.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)public static java.lang.Object invokeMethod(java.lang.reflect.Method method,
java.lang.Object target,
java.lang.Object... args)
method - the method to invoke; never nulltarget - the object on which to invoke the method; may be
null if the method is staticargs - the arguments to pass to the methodnull
if the return type is voidExceptionUtils.throwAsUncheckedException(Throwable)public static java.util.Optional<java.lang.Class<?>> loadClass(java.lang.String name)
name - the fully qualified name of the class to load; never
null or blankloadClass(String, ClassLoader)public static java.util.Optional<java.lang.Class<?>> loadClass(java.lang.String name,
java.lang.ClassLoader classLoader)
ClassLoader.name - the fully qualified name of the class to load; never
null or blankclassLoader - the ClassLoader to use; never nullloadClass(String)public static java.util.Optional<java.lang.reflect.Method> loadMethod(java.lang.String fullyQualifiedMethodName)
The supported format is [fully qualified class name]#[methodName].
For example, the fully qualified name for the chars() method in
java.lang.String is java.lang.String#chars.
fullyQualifiedMethodName - the fully qualified name of the method to load;
never null or blankpublic static java.util.Optional<java.lang.Object> getOuterInstance(java.lang.Object inner,
java.lang.Class<?> targetType)
public static boolean isPackage(java.lang.String packageName)
public static java.util.Set<java.io.File> getAllClasspathRootDirectories()
public static java.util.List<java.lang.Class<?>> findAllClassesInClasspathRoot(java.io.File root,
java.util.function.Predicate<java.lang.Class<?>> classTester)
public static java.util.List<java.lang.Class<?>> findAllClassesInPackage(java.lang.String basePackageName,
java.util.function.Predicate<java.lang.Class<?>> classTester)
public static java.util.List<java.lang.Class<?>> findNestedClasses(java.lang.Class<?> clazz,
java.util.function.Predicate<java.lang.Class<?>> predicate)
public static <T> java.lang.reflect.Constructor<T> getDeclaredConstructor(java.lang.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 java.util.Optional<java.lang.reflect.Method> getMethod(java.lang.Class<?> clazz,
java.lang.String methodName,
java.lang.Class<?>... parameterTypes)
public static java.util.Optional<java.lang.reflect.Method> findMethod(java.lang.Class<?> clazz,
java.lang.String methodName,
java.lang.Class<?>... parameterTypes)
public static java.util.List<java.lang.reflect.Method> findMethods(java.lang.Class<?> clazz,
java.util.function.Predicate<java.lang.reflect.Method> predicate)
public static java.util.List<java.lang.reflect.Method> findMethods(java.lang.Class<?> clazz,
java.util.function.Predicate<java.lang.reflect.Method> predicate,
ReflectionUtils.MethodSortOrder sortOrder)
public static <T> java.util.Optional<java.lang.Object> readFieldValue(java.lang.Class<T> clazz,
java.lang.String fieldName,
T instance)
If the field does not exist, an exception occurs while reading it, or
the value of the field is null, an empty Optional is
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 fieldpublic static java.util.Set<java.lang.Class<?>> getAllAssignmentCompatibleClasses(java.lang.Class<?> clazz)
Class, including itself.clazz - the Class to lookupClass.isAssignableFrom(java.lang.Class<?>)