Class ReflectionUtils
- java.lang.Object
-
- nonapi.io.github.classgraph.reflection.ReflectionUtils
-
public final class ReflectionUtils extends java.lang.Object
Reflection utility methods that can be used by ClassLoaderHandlers.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private class
ReflectionUtils.PrivilegedActionInvocationHandler<T>
-
Field Summary
Fields Modifier and Type Field Description private java.lang.Class<?>
accessControllerClass
private java.lang.reflect.Method
accessControllerDoPrivileged
private java.lang.Class<?>
privilegedActionClass
ReflectionDriver
reflectionDriver
The reflection driver to use.
-
Constructor Summary
Constructors Constructor Description ReflectionUtils()
Call this if you change the value ofClassGraph.CIRCUMVENT_ENCAPSULATION
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.Class<?>
classForNameOrNull(java.lang.String className)
Call Class.forName(className), but return null if any exception is thrown.<T> T
doPrivileged(java.util.concurrent.Callable<T> callable)
Call a method in the AccessController.doPrivileged(PrivilegedAction) context, using reflection, if possible (AccessController is deprecated in JDK 17).java.lang.Object
getFieldVal(boolean throwException, java.lang.Object obj, java.lang.reflect.Field field)
Get the value of the field in the class of the given object or any of its superclasses.java.lang.Object
getFieldVal(boolean throwException, java.lang.Object obj, java.lang.String fieldName)
Get the value of the named field in the class of the given object or any of its superclasses.java.lang.Object
getStaticFieldVal(boolean throwException, java.lang.Class<?> cls, java.lang.String fieldName)
Get the value of the named field in the given class or any of its superclasses.java.lang.Object
invokeMethod(boolean throwException, java.lang.Object obj, java.lang.String methodName)
Invoke the named method in the given object or its superclasses.java.lang.Object
invokeMethod(boolean throwException, java.lang.Object obj, java.lang.String methodName, java.lang.Class<?> argType, java.lang.Object param)
Invoke the named method in the given object or its superclasses.java.lang.Object
invokeStaticMethod(boolean throwException, java.lang.Class<?> cls, java.lang.String methodName)
Invoke the named method.java.lang.Object
invokeStaticMethod(boolean throwException, java.lang.Class<?> cls, java.lang.String methodName, java.lang.Class<?> argType, java.lang.Object param)
Invoke the named method.java.lang.reflect.Method
staticMethodForNameOrNull(java.lang.String className, java.lang.String staticMethodName)
Get a method by name, but return null if any exception is thrown.
-
-
-
Field Detail
-
reflectionDriver
public ReflectionDriver reflectionDriver
The reflection driver to use.
-
accessControllerClass
private java.lang.Class<?> accessControllerClass
-
privilegedActionClass
private java.lang.Class<?> privilegedActionClass
-
accessControllerDoPrivileged
private java.lang.reflect.Method accessControllerDoPrivileged
-
-
Constructor Detail
-
ReflectionUtils
public ReflectionUtils()
Call this if you change the value ofClassGraph.CIRCUMVENT_ENCAPSULATION
.
-
-
Method Detail
-
getFieldVal
public java.lang.Object getFieldVal(boolean throwException, java.lang.Object obj, java.lang.reflect.Field field) throws java.lang.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:
java.lang.IllegalArgumentException
- If the field value could not be read.
-
getFieldVal
public java.lang.Object getFieldVal(boolean throwException, java.lang.Object obj, java.lang.String fieldName) throws java.lang.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:
java.lang.IllegalArgumentException
- If the field value could not be read.
-
getStaticFieldVal
public java.lang.Object getStaticFieldVal(boolean throwException, java.lang.Class<?> cls, java.lang.String fieldName) throws java.lang.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:
java.lang.IllegalArgumentException
- If the field value could not be read.
-
invokeMethod
public java.lang.Object invokeMethod(boolean throwException, java.lang.Object obj, java.lang.String methodName) throws java.lang.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:
java.lang.IllegalArgumentException
- If the method could not be invoked.
-
invokeMethod
public java.lang.Object invokeMethod(boolean throwException, java.lang.Object obj, java.lang.String methodName, java.lang.Class<?> argType, java.lang.Object param) throws java.lang.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:
java.lang.IllegalArgumentException
- If the method could not be invoked.
-
invokeStaticMethod
public java.lang.Object invokeStaticMethod(boolean throwException, java.lang.Class<?> cls, java.lang.String methodName) throws java.lang.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:
java.lang.IllegalArgumentException
- If the method could not be invoked.
-
invokeStaticMethod
public java.lang.Object invokeStaticMethod(boolean throwException, java.lang.Class<?> cls, java.lang.String methodName, java.lang.Class<?> argType, java.lang.Object param) throws java.lang.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:
java.lang.IllegalArgumentException
- If the method could not be invoked.
-
classForNameOrNull
public java.lang.Class<?> classForNameOrNull(java.lang.String className)
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
public java.lang.reflect.Method staticMethodForNameOrNull(java.lang.String className, java.lang.String staticMethodName)
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
public <T> T doPrivileged(java.util.concurrent.Callable<T> callable) throws java.lang.Throwable
Call a method in the AccessController.doPrivileged(PrivilegedAction) context, using reflection, if possible (AccessController is deprecated in JDK 17).- Throws:
java.lang.Throwable
-
-