Package io.github.classgraph.utils
Class ReflectionUtils
- java.lang.Object
-
- io.github.classgraph.utils.ReflectionUtils
-
public class ReflectionUtils extends Object
Reflection utility methods that can be used by ClassLoaderHandlers.
-
-
Constructor Summary
Constructors Constructor Description ReflectionUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Class<?>
classForNameOrNull(String className)
Call Class.forName(className), but return null if any exception is thrown.static Object
getFieldVal(Object obj, String fieldName, boolean throwException)
Get the value of the named field in the class of the given object or any of its superclasses.static Object
getStaticFieldVal(Class<?> cls, String fieldName, boolean throwException)
Get the value of the named static field in the given class or any of its superclasses.static Object
invokeDefaultMethod(Class<?> cls, String methodName, Class<?> returnType, ClassLoader classLoader, boolean throwException)
Invoke the named default interface method.static Object
invokeMethod(Object obj, String methodName, boolean throwException)
Invoke the named method in the given object or its superclasses.static Object
invokeMethod(Object obj, String methodName, Class<?> argType, Object arg, boolean throwException)
Invoke the named method in the given object or its superclasses.static Object
invokeStaticMethod(Class<?> cls, String methodName, boolean throwException)
Invoke the named static method.static Object
invokeStaticMethod(Class<?> cls, String methodName, Class<?> argType, Object arg, boolean throwException)
Invoke the named static method.
-
-
-
Method Detail
-
getFieldVal
public static Object getFieldVal(Object obj, String fieldName, boolean throwException) 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:
obj
- The object.fieldName
- The field name.throwException
- If true, throw an exception if the field value could not be read.- Returns:
- The field value.
- Throws:
IllegalArgumentException
- If the field value could not be read.
-
getStaticFieldVal
public static Object getStaticFieldVal(Class<?> cls, String fieldName, boolean throwException) throws IllegalArgumentException
Get the value of the named static 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:
cls
- The class.fieldName
- The field name.throwException
- If true, throw an exception if the field value could not be read.- Returns:
- The field value.
- Throws:
IllegalArgumentException
- If the field value could not be read.
-
invokeMethod
public static Object invokeMethod(Object obj, String methodName, boolean throwException) 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:
obj
- The object.methodName
- The method name.throwException
- If true, throw an exception if the field value could not be read.- Returns:
- The field value.
- Throws:
IllegalArgumentException
- If the field value could not be read.
-
invokeMethod
public static Object invokeMethod(Object obj, String methodName, Class<?> argType, Object arg, boolean throwException) 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:
obj
- The object.methodName
- The method name.argType
- The type of the parameter.arg
- The argument value.throwException
- Whether to throw an exception on failure.- Returns:
- The result of the method invocation.
- Throws:
IllegalArgumentException
- If the method could not be invoked.
-
invokeStaticMethod
public static Object invokeStaticMethod(Class<?> cls, String methodName, boolean throwException) throws IllegalArgumentException
Invoke the named static 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:
cls
- The class.methodName
- The method name.throwException
- Whether to throw an exception on failure.- Returns:
- The result of the method invocation.
- Throws:
IllegalArgumentException
- If the method could not be invoked.
-
invokeStaticMethod
public static Object invokeStaticMethod(Class<?> cls, String methodName, Class<?> argType, Object arg, boolean throwException) throws IllegalArgumentException
Invoke the named static 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:
cls
- The class.methodName
- The method name.argType
- The type of the parameter.arg
- The argument value.throwException
- Whether to throw an exception on failure.- Returns:
- The result of the method invocation.
- Throws:
IllegalArgumentException
- If the method could not be invoked.
-
invokeDefaultMethod
public static Object invokeDefaultMethod(Class<?> cls, String methodName, Class<?> returnType, ClassLoader classLoader, boolean throwException) throws IllegalArgumentException
Invoke the named default interface 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. Uses the solution in https://stackoverflow.com/a/49532492/3950982 TODO: This is not tested...- Parameters:
cls
- The object.methodName
- The method name.returnType
- The return type of the method.classLoader
- The ClassLoader.throwException
- Whether to throw an exception on failure.- Returns:
- The result of the method invokation.
- Throws:
IllegalArgumentException
- If the method could not be invoked.
-
classForNameOrNull
public static Class<?> classForNameOrNull(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.
-
-