Class ReflectionUtils
java.lang.Object
nonapi.io.github.classgraph.reflection.ReflectionUtils
Reflection utility methods that can be used by ClassLoaderHandlers.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate class
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionClass
<?> classForNameOrNull
(String className) Call Class.forName(className), but return null if any exception is thrown.<T> T
doPrivileged
(Callable<T> callable) Call a method in the AccessController.doPrivileged(PrivilegedAction) context, using reflection, if possible (AccessController is deprecated in JDK 17).getFieldVal
(boolean throwException, Object obj, Field field) Get the value of the field in the class of the given object or any of its superclasses.getFieldVal
(boolean throwException, Object obj, String fieldName) Get the value of the named field in the class of the given object or any of its superclasses.getStaticFieldVal
(boolean throwException, Class<?> cls, String fieldName) Get the value of the named field in the given class or any of its superclasses.invokeMethod
(boolean throwException, Object obj, String methodName) Invoke the named method in the given object or its superclasses.invokeMethod
(boolean throwException, Object obj, String methodName, Class<?> argType, Object param) Invoke the named method in the given object or its superclasses.invokeStaticMethod
(boolean throwException, Class<?> cls, String methodName) Invoke the named method.invokeStaticMethod
(boolean throwException, Class<?> cls, String methodName, Class<?> argType, Object param) Invoke the named method.staticMethodForNameOrNull
(String className, String staticMethodName) Get a method by name, but return null if any exception is thrown.
-
Field Details
-
reflectionDriver
The reflection driver to use. -
accessControllerClass
-
privilegedActionClass
-
accessControllerDoPrivileged
-
-
Constructor Details
-
ReflectionUtils
public ReflectionUtils()Call this if you change the value ofClassGraph.CIRCUMVENT_ENCAPSULATION
.
-
-
Method Details
-
getFieldVal
public Object getFieldVal(boolean throwException, Object obj, Field field) throws IllegalArgumentException Get the value of the field in the class of the given object or any of its superclasses. If an exception is thrown while trying to read the field, and throwException is true, then IllegalArgumentException is thrown wrapping the cause, otherwise this will return null. If passed a null object, returns null unless throwException is true, then throws IllegalArgumentException.- Parameters:
throwException
- If true, throw an exception if the field value could not be read.obj
- The object.field
- The field.- Returns:
- The field value.
- Throws:
IllegalArgumentException
- If the field value could not be read.
-
getFieldVal
public Object getFieldVal(boolean throwException, Object obj, String fieldName) throws IllegalArgumentException Get the value of the named field in the class of the given object or any of its superclasses. If an exception is thrown while trying to read the field, and throwException is true, then IllegalArgumentException is thrown wrapping the cause, otherwise this will return null. If passed a null object, returns null unless throwException is true, then throws IllegalArgumentException.- Parameters:
throwException
- If true, throw an exception if the field value could not be read.obj
- The object.fieldName
- The field name.- Returns:
- The field value.
- Throws:
IllegalArgumentException
- If the field value could not be read.
-
getStaticFieldVal
public Object getStaticFieldVal(boolean throwException, Class<?> cls, String fieldName) throws IllegalArgumentException Get the value of the named field in the given class or any of its superclasses. If an exception is thrown while trying to read the field value, and throwException is true, then IllegalArgumentException is thrown wrapping the cause, otherwise this will return null. If passed a null class reference, returns null unless throwException is true, then throws IllegalArgumentException.- Parameters:
throwException
- If true, throw an exception if the field value could not be read.cls
- The class.fieldName
- The field name.- Returns:
- The field value.
- Throws:
IllegalArgumentException
- If the field value could not be read.
-
invokeMethod
public Object invokeMethod(boolean throwException, Object obj, String methodName) throws IllegalArgumentException Invoke the named method in the given object or its superclasses. If an exception is thrown while trying to call the method, and throwException is true, then IllegalArgumentException is thrown wrapping the cause, otherwise this will return null. If passed a null object, returns null unless throwException is true, then throws IllegalArgumentException.- Parameters:
throwException
- If true, throw an exception if the field value could not be read.obj
- The object.methodName
- The method name.- Returns:
- The result of the method invocation.
- Throws:
IllegalArgumentException
- If the method could not be invoked.
-
invokeMethod
public Object invokeMethod(boolean throwException, Object obj, String methodName, Class<?> argType, Object param) throws IllegalArgumentException Invoke the named method in the given object or its superclasses. If an exception is thrown while trying to call the method, and throwException is true, then IllegalArgumentException is thrown wrapping the cause, otherwise this will return null. If passed a null object, returns null unless throwException is true, then throws IllegalArgumentException.- Parameters:
throwException
- Whether to throw an exception on failure.obj
- The object.methodName
- The method name.argType
- The type of the method argument.param
- The parameter value to use when invoking the method.- Returns:
- The result of the method invocation.
- Throws:
IllegalArgumentException
- If the method could not be invoked.
-
invokeStaticMethod
public Object invokeStaticMethod(boolean throwException, Class<?> cls, String methodName) throws IllegalArgumentException Invoke the named method. If an exception is thrown while trying to call the method, and throwException is true, then IllegalArgumentException is thrown wrapping the cause, otherwise this will return null. If passed a null class reference, returns null unless throwException is true, then throws IllegalArgumentException.- Parameters:
throwException
- Whether to throw an exception on failure.cls
- The class.methodName
- The method name.- Returns:
- The result of the method invocation.
- Throws:
IllegalArgumentException
- If the method could not be invoked.
-
invokeStaticMethod
public Object invokeStaticMethod(boolean throwException, Class<?> cls, String methodName, Class<?> argType, Object param) throws IllegalArgumentException Invoke the named method. If an exception is thrown while trying to call the method, and throwException is true, then IllegalArgumentException is thrown wrapping the cause, otherwise this will return null. If passed a null class reference, returns null unless throwException is true, then throws IllegalArgumentException.- Parameters:
throwException
- Whether to throw an exception on failure.cls
- The class.methodName
- The method name.argType
- The type of the method argument.param
- The parameter value to use when invoking the method.- Returns:
- The result of the method invocation.
- Throws:
IllegalArgumentException
- If the method could not be invoked.
-
classForNameOrNull
Call Class.forName(className), but return null if any exception is thrown.- Parameters:
className
- The class name to load.- Returns:
- The class of the requested name, or null if an exception was thrown while trying to load the class.
-
staticMethodForNameOrNull
Get a method by name, but return null if any exception is thrown.- Parameters:
className
- The class name to load.- Returns:
- The class of the requested name, or null if an exception was thrown while trying to load the class.
-
doPrivileged
Call a method in the AccessController.doPrivileged(PrivilegedAction) context, using reflection, if possible (AccessController is deprecated in JDK 17).- Throws:
Throwable
-