Class ReflectionUtils


  • public class ReflectionUtils
    extends Object
    Reflection utility methods that can be used by ClassLoaderHandlers.
    • Constructor Detail

      • ReflectionUtils

        public ReflectionUtils()
    • 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.
        Throws:
        IllegalArgumentException
        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.
        Throws:
        IllegalArgumentException
        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.
        Throws:
        IllegalArgumentException
        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.
        Throws:
        IllegalArgumentException
        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.
        Throws:
        IllegalArgumentException
        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.
        Throws:
        IllegalArgumentException
        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...
        Throws:
        IllegalArgumentException
        NullPointerException
      • classForNameOrNull

        public static Class<?> classForNameOrNull​(String className)
        Call Class.forName(className), but return null if any exception is thrown.