@API(status=MAINTAINED, since="1.0") public final class ReflectionSupport extends java.lang.Object
Modifier and Type | Method | Description |
---|---|---|
static java.util.List<java.lang.Class<?>> |
findAllClassesInClasspathRoot(java.net.URI root,
java.util.function.Predicate<java.lang.Class<?>> classTester,
java.util.function.Predicate<java.lang.String> classNameFilter) |
Find all classes of the supplied
root
URI that match the specified classTester and
classNameFilter predicates. |
static java.util.List<java.lang.Class<?>> |
findAllClassesInPackage(java.lang.String basePackageName,
java.util.function.Predicate<java.lang.Class<?>> classTester,
java.util.function.Predicate<java.lang.String> classNameFilter) |
Find all classes of the supplied
basePackageName
that match the specified classTester and classNameFilter
predicates. |
static java.util.Optional<java.lang.reflect.Method> |
findMethod(java.lang.Class<?> clazz,
java.lang.String methodName,
java.lang.Class<?>... 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. |
static java.util.Optional<java.lang.reflect.Method> |
findMethod(java.lang.Class<?> clazz,
java.lang.String methodName,
java.lang.String parameterTypeNames) |
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. |
static java.util.List<java.lang.reflect.Method> |
findMethods(java.lang.Class<?> clazz,
java.util.function.Predicate<java.lang.reflect.Method> predicate,
HierarchyTraversalMode traversalMode) |
Find all methods of the supplied class or interface
that match the specified
predicate . |
static java.util.List<java.lang.Class<?>> |
findNestedClasses(java.lang.Class<?> clazz,
java.util.function.Predicate<java.lang.Class<?>> predicate) |
Find all nested classes of the given class conforming to the given predicate.
|
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 java.util.Optional<java.lang.Class<?>> |
loadClass(java.lang.String name) |
Load a class by its primitive name or fully qualified name,
using the default
ClassLoader . |
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. |
public static java.util.Optional<java.lang.Class<?>> loadClass(java.lang.String name)
ClassLoader
.
Class names for arrays may be specified using either the JVM's internal
String representation (e.g., [[I
for int[][]
,
[Lava.lang.String;
for java.lang.String[]
, etc.) or
source code syntax (e.g., int[][]
, java.lang.String[]
,
etc.).
name
- the name of the class to load; never null
or blankOptional
containing the loaded class; never null
but potentially empty if no such class could be loadedpublic static java.util.List<java.lang.Class<?>> findAllClassesInClasspathRoot(java.net.URI root, java.util.function.Predicate<java.lang.Class<?>> classTester, java.util.function.Predicate<java.lang.String> classNameFilter)
root
URI that match the specified classTester
and
classNameFilter
predicates.root
- the root URI to start scanningclassTester
- the class type filter; never null
classNameFilter
- the class name filter; never null
null
but potentially emptypublic static java.util.List<java.lang.Class<?>> findAllClassesInPackage(java.lang.String basePackageName, java.util.function.Predicate<java.lang.Class<?>> classTester, java.util.function.Predicate<java.lang.String> classNameFilter)
basePackageName
that match the specified classTester
and classNameFilter
predicates.basePackageName
- the base package name to start scanningclassTester
- the class type filter; never null
classNameFilter
- the class name filter; never null
null
but potentially emptypublic 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 null
args
- the arguments to pass to the constructor none of which may be null
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 null
target
- the object on which to invoke the method; may be
null
if the method is static
args
- the arguments to pass to the methodnull
if the return type is void
ExceptionUtils.throwAsUncheckedException(Throwable)
public static java.util.Optional<java.lang.reflect.Method> findMethod(java.lang.Class<?> clazz, java.lang.String methodName, java.lang.String parameterTypeNames)
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.clazz
- the class or interface in which to find the method; never null
methodName
- the name of the method to find; never null
or emptyparameterTypeNames
- the fully qualified names of the types of parameters
accepted by the method, if any, provided as a comma-separated listOptional
containing the method found; never null
but potentially empty if no such method could be foundfindMethod(Class, String, Class...)
public static java.util.Optional<java.lang.reflect.Method> findMethod(java.lang.Class<?> clazz, java.lang.String methodName, java.lang.Class<?>... parameterTypes)
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.clazz
- the class or interface in which to find the method; never null
methodName
- the name of the method to find; never null
or emptyparameterTypes
- the types of parameters accepted by the method, if any;
never null
Optional
containing the method found; never null
but potentially empty if no such method could be foundfindMethod(Class, String, String)
public static java.util.List<java.lang.reflect.Method> findMethods(java.lang.Class<?> clazz, java.util.function.Predicate<java.lang.reflect.Method> predicate, HierarchyTraversalMode traversalMode)
predicate
.
The results will not contain instance methods that are overridden
or static
methods that are hidden.
If you're are looking for methods annotated with a certain annotation type, consider using AnnotationSupport.findAnnotatedMethods(Class, Class, HierarchyTraversalMode).
clazz
- the class or interface in which to find the methods; never null
predicate
- the method filter; never null
traversalMode
- the hierarchy traversal mode; never null
null
but potentially emptypublic static java.util.List<java.lang.Class<?>> findNestedClasses(java.lang.Class<?> clazz, java.util.function.Predicate<java.lang.Class<?>> predicate)
clazz
- the class to be searched; never null
predicate
- the predicate against which the list of nested classes is
checked; never null
null
but potentially empty