Class ReflectionUtils
- java.lang.Object
-
- io.github.lukehutch.fastclasspathscanner.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, NullPointerException
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 NullPointerException.
-
getStaticFieldVal
public static Object getStaticFieldVal(Class<?> cls, String fieldName, boolean throwException) throws IllegalArgumentException, NullPointerException
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 NullPointerException.
-
invokeMethod
public static Object invokeMethod(Object obj, String methodName, boolean throwException) throws IllegalArgumentException, NullPointerException
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 NullPointerException.
-
invokeMethod
public static Object invokeMethod(Object obj, String methodName, Class<?> argType, Object arg, boolean throwException) throws IllegalArgumentException, NullPointerException
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 NullPointerException.
-
invokeStaticMethod
public static Object invokeStaticMethod(Class<?> cls, String methodName, boolean throwException) throws IllegalArgumentException, NullPointerException
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 NullPointerException.
-
invokeStaticMethod
public static Object invokeStaticMethod(Class<?> cls, String methodName, Class<?> argType, Object arg, boolean throwException) throws IllegalArgumentException, NullPointerException
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 NullPointerException.
-
invokeDefaultMethod
public static Object invokeDefaultMethod(Class<?> cls, String methodName, Class<?> returnType, ClassLoader classLoader, boolean throwException) throws IllegalArgumentException, NullPointerException
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 NullPointerException. Uses the solution in https://stackoverflow.com/a/49532492/3950982 N.B. This is not completely tested...
-
-