Class ReflectionUtils


  • public class ReflectionUtils
    extends java.lang.Object
    Utility class to use reflection to call methods.
    • Constructor Summary

      Constructors 
      Constructor Description
      ReflectionUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void call​(java.lang.Object thisObject, java.lang.String methodName)
      Helper method to call a no argument, no return method.
      static java.lang.Object callAny​(java.lang.Object thisObject, java.lang.String methodName, java.lang.Class<?>[] argTypes, java.lang.Object[] args)
      Helper method to call a multi-argument method having a return.
      static java.lang.Object callAny​(java.lang.Object thisObject, java.lang.String methodName, java.lang.Object[] args)
      Helper method to call a multi-argument method having a return.
      static java.lang.Object callAnyWithoutException​(java.lang.Object thisObject, java.lang.Class<?> objectClass, java.lang.String methodName, java.lang.Class<?>[] argTypes, java.lang.Object[] args)
      Helper method to call a multi-argument method having a return without throwing an exception.
      static java.lang.Object callConstructor​(java.lang.Class<?> clazz, java.lang.Class<?>[] argTypes, java.lang.Object[] args)
      Helper method to call a constructor.
      static java.lang.Object callGet​(java.lang.Object thisObject, java.lang.String methodName)
      Helper method to call a get method with no argument.
      static void callSet​(java.lang.Object thisObject, java.lang.String methodName, java.lang.Object value)
      Helper method to call a set boolean method.
      static void callSetBoolean​(java.lang.Object thisObject, java.lang.String methodName, boolean value)
      Helper method to call a set boolean method.
      static void callSetInt​(java.lang.Object thisObject, java.lang.String methodName, int value)
      Helper method to call a set boolean method.
      static java.lang.Object callStatic​(java.lang.Class<?> thisClass, java.lang.String methodName, java.lang.Class<?>[] argTypes, java.lang.Object[] args)
      Helper method to call a multi-argument static class method having a return.
      static java.lang.Object createInstance​(java.lang.String className, java.lang.Class<?>[] types, java.lang.Object[] args)
      Instantiate an object based on the class name.
      static boolean isSubClassOf​(java.lang.Object o, java.lang.String className)
      Checks if the instance o is an instance of a subclass of the designated class name.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ReflectionUtils

        public ReflectionUtils()
    • Method Detail

      • callSetBoolean

        public static void callSetBoolean​(java.lang.Object thisObject,
                                          java.lang.String methodName,
                                          boolean value)
                                   throws java.lang.Exception
        Helper method to call a set boolean method.
        Parameters:
        thisObject - the instance
        methodName - the method name
        value - true or false
        Throws:
        java.lang.Exception - if the method is not found or invocation to the method fails.
      • callSetInt

        public static void callSetInt​(java.lang.Object thisObject,
                                      java.lang.String methodName,
                                      int value)
                               throws java.lang.Exception
        Helper method to call a set boolean method.
        Parameters:
        thisObject - the instance
        methodName - the method name
        value - the value
        Throws:
        java.lang.Exception - if the method is not found or invocation to the method fails.
      • callSet

        public static void callSet​(java.lang.Object thisObject,
                                   java.lang.String methodName,
                                   java.lang.Object value)
                            throws java.lang.Exception
        Helper method to call a set boolean method.
        Parameters:
        thisObject - the instance
        methodName - the method name
        value - the value
        Throws:
        java.lang.Exception - if the method is not found or invocation to the method fails.
      • callGet

        public static java.lang.Object callGet​(java.lang.Object thisObject,
                                               java.lang.String methodName)
                                        throws java.lang.Exception
        Helper method to call a get method with no argument.
        Parameters:
        thisObject - the instance
        methodName - the method name
        Returns:
        the value the method returns.
        Throws:
        java.lang.Exception - if the method is not found or invocation to the method fails.
      • call

        public static void call​(java.lang.Object thisObject,
                                java.lang.String methodName)
                         throws java.lang.Exception
        Helper method to call a no argument, no return method.
        Parameters:
        thisObject - the instance
        methodName - the method name
        Throws:
        java.lang.Exception - if the method is not found or invocation to the method fails.
      • callConstructor

        public static java.lang.Object callConstructor​(java.lang.Class<?> clazz,
                                                       java.lang.Class<?>[] argTypes,
                                                       java.lang.Object[] args)
                                                throws java.lang.Exception
        Helper method to call a constructor.
        Parameters:
        clazz - the class
        argTypes - argument Classes for constructor lookup. Must not be null.
        args - the argument array
        Returns:
        the value the method returns.
        Throws:
        java.lang.Exception - if the method is not found or invocation to the method fails.
      • callAny

        public static java.lang.Object callAny​(java.lang.Object thisObject,
                                               java.lang.String methodName,
                                               java.lang.Object[] args)
                                        throws java.lang.Exception
        Helper method to call a multi-argument method having a return. The class types will be derived from the input values. This call is usually successful with primitive types or Strings as arguments, but care should be used with other kinds of values. The constructor lookup is not polymorphic.

        Calls callAny(Object, methodName, argTypes, args).

        Parameters:
        thisObject - the instance
        methodName - the method name
        args - the argument array, must not contain null.
        Returns:
        the value the method returns.
        Throws:
        java.lang.Exception - if the method is not found or invocation to the method fails.
      • callAny

        public static java.lang.Object callAny​(java.lang.Object thisObject,
                                               java.lang.String methodName,
                                               java.lang.Class<?>[] argTypes,
                                               java.lang.Object[] args)
                                        throws java.lang.Exception
        Helper method to call a multi-argument method having a return.
        Parameters:
        thisObject - the instance
        methodName - the method name
        argTypes - argument Classes for constructor lookup. Must not be null.
        args - the argument array
        Returns:
        the value the method returns.
        Throws:
        java.lang.Exception - if the method is not found or invocation to the method fails.
      • callAnyWithoutException

        public static java.lang.Object callAnyWithoutException​(java.lang.Object thisObject,
                                                               java.lang.Class<?> objectClass,
                                                               java.lang.String methodName,
                                                               java.lang.Class<?>[] argTypes,
                                                               java.lang.Object[] args)
        Helper method to call a multi-argument method having a return without throwing an exception.
        Parameters:
        thisObject - the instance
        objectClass - the class which could find the method name
        methodName - the method name
        argTypes - argument Classes for constructor lookup. Must not be null.
        args - the argument array
        Returns:
        the value the method returns.
      • callStatic

        public static java.lang.Object callStatic​(java.lang.Class<?> thisClass,
                                                  java.lang.String methodName,
                                                  java.lang.Class<?>[] argTypes,
                                                  java.lang.Object[] args)
                                           throws java.lang.Exception
        Helper method to call a multi-argument static class method having a return.
        Parameters:
        thisClass - the class
        methodName - the method name
        argTypes - argument Classes for constructor lookup. Must not be null.
        args - the argument array
        Returns:
        the value the method returns.
        Throws:
        java.lang.Exception - if the method is not found or invocation to the method fails.
      • createInstance

        public static java.lang.Object createInstance​(java.lang.String className,
                                                      java.lang.Class<?>[] types,
                                                      java.lang.Object[] args)
        Instantiate an object based on the class name.
        Parameters:
        className - the class name
        types - the class types for the constructor
        args - the constructor values
        Returns:
        the object instance. null if any exception occurs.
      • isSubClassOf

        public static boolean isSubClassOf​(java.lang.Object o,
                                           java.lang.String className)
        Checks if the instance o is an instance of a subclass of the designated class name.
        Parameters:
        o - the instance to check
        className - the class name to check
        Returns:
        true if the instance o is an instance of a subclass of the class name. Otherwise false.
        Since:
        3.4.9