Class MethodUtils


  • @Deprecated(since="2021-04-30")
    public class MethodUtils
    extends java.lang.Object
    Deprecated.
    Commons Lang 2 is in maintenance mode. Commons Lang 3 should be used instead.

    Utility reflection methods focused on methods, originally from Commons BeanUtils. Differences from the BeanUtils version may be noted, especially where similar functionality already existed within Lang.

    Known Limitations

    Accessing Public Methods In A Default Access Superclass

    There is an issue when invoking public methods contained in a default access superclass on JREs prior to 1.4. Reflection locates these methods fine and correctly assigns them as public. However, an IllegalAccessException is thrown if the method is invoked.

    MethodUtils contains a workaround for this situation. It will attempt to call setAccessible on this method. If this call succeeds, then the method can be invoked as normal. This call will only succeed when the application has sufficient security privileges. If this call fails then the method may fail.

    Since:
    2.5
    • Constructor Summary

      Constructors 
      Constructor Description
      MethodUtils()
      Deprecated.
      MethodUtils instances should NOT be constructed in standard programming.
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static java.lang.reflect.Method getAccessibleMethod​(java.lang.Class cls, java.lang.String methodName, java.lang.Class parameterType)
      Deprecated.
      Return an accessible method (that is, one that can be invoked via reflection) with given name and a single parameter.
      static java.lang.reflect.Method getAccessibleMethod​(java.lang.Class cls, java.lang.String methodName, java.lang.Class[] parameterTypes)
      Deprecated.
      Return an accessible method (that is, one that can be invoked via reflection) with given name and parameters.
      static java.lang.reflect.Method getAccessibleMethod​(java.lang.reflect.Method method)
      Deprecated.
      Return an accessible method (that is, one that can be invoked via reflection) that implements the specified Method.
      static java.lang.reflect.Method getMatchingAccessibleMethod​(java.lang.Class cls, java.lang.String methodName, java.lang.Class[] parameterTypes)
      Deprecated.
      Find an accessible method that matches the given name and has compatible parameters.
      static java.lang.Object invokeExactMethod​(java.lang.Object object, java.lang.String methodName, java.lang.Object arg)
      Deprecated.
      Invoke a method whose parameter type matches exactly the object type.
      static java.lang.Object invokeExactMethod​(java.lang.Object object, java.lang.String methodName, java.lang.Object[] args)
      Deprecated.
      Invoke a method whose parameter types match exactly the object types.
      static java.lang.Object invokeExactMethod​(java.lang.Object object, java.lang.String methodName, java.lang.Object[] args, java.lang.Class[] parameterTypes)
      Deprecated.
      Invoke a method whose parameter types match exactly the parameter types given.
      static java.lang.Object invokeExactStaticMethod​(java.lang.Class cls, java.lang.String methodName, java.lang.Object arg)
      Deprecated.
      Invoke a static method whose parameter type matches exactly the object type.
      static java.lang.Object invokeExactStaticMethod​(java.lang.Class cls, java.lang.String methodName, java.lang.Object[] args)
      Deprecated.
      Invoke a static method whose parameter types match exactly the object types.
      static java.lang.Object invokeExactStaticMethod​(java.lang.Class cls, java.lang.String methodName, java.lang.Object[] args, java.lang.Class[] parameterTypes)
      Deprecated.
      Invoke a static method whose parameter types match exactly the parameter types given.
      static java.lang.Object invokeMethod​(java.lang.Object object, java.lang.String methodName, java.lang.Object arg)
      Deprecated.
      Invoke a named method whose parameter type matches the object type.
      static java.lang.Object invokeMethod​(java.lang.Object object, java.lang.String methodName, java.lang.Object[] args)
      Deprecated.
      Invoke a named method whose parameter type matches the object type.
      static java.lang.Object invokeMethod​(java.lang.Object object, java.lang.String methodName, java.lang.Object[] args, java.lang.Class[] parameterTypes)
      Deprecated.
      Invoke a named method whose parameter type matches the object type.
      static java.lang.Object invokeStaticMethod​(java.lang.Class cls, java.lang.String methodName, java.lang.Object arg)
      Deprecated.
      Invoke a named static method whose parameter type matches the object type.
      static java.lang.Object invokeStaticMethod​(java.lang.Class cls, java.lang.String methodName, java.lang.Object[] args)
      Deprecated.
      Invoke a named static method whose parameter type matches the object type.
      static java.lang.Object invokeStaticMethod​(java.lang.Class cls, java.lang.String methodName, java.lang.Object[] args, java.lang.Class[] parameterTypes)
      Deprecated.
      Invoke a named static method whose parameter type matches the object type.
      • Methods inherited from class java.lang.Object

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

      • MethodUtils

        public MethodUtils()
        Deprecated.

        MethodUtils instances should NOT be constructed in standard programming. Instead, the class should be used as MethodUtils.getAccessibleMethod(method).

        This constructor is public to permit tools that require a JavaBean instance to operate.

    • Method Detail

      • invokeMethod

        public static java.lang.Object invokeMethod​(java.lang.Object object,
                                                    java.lang.String methodName,
                                                    java.lang.Object arg)
                                             throws java.lang.NoSuchMethodException,
                                                    java.lang.IllegalAccessException,
                                                    java.lang.reflect.InvocationTargetException
        Deprecated.

        Invoke a named method whose parameter type matches the object type.

        This method delegates the method search to getMatchingAccessibleMethod(Class, String, Class[]).

        This method supports calls to methods taking primitive parameters via passing in wrapping classes. So, for example, a Boolean object would match a boolean primitive.

        This is a convenient wrapper for invokeMethod(Object object, String methodName, Object[] args).

        Parameters:
        object - invoke method on this object
        methodName - get method with this name
        arg - use this argument
        Returns:
        The value returned by the invoked method
        Throws:
        java.lang.NoSuchMethodException - if there is no such accessible method
        java.lang.reflect.InvocationTargetException - wraps an exception thrown by the method invoked
        java.lang.IllegalAccessException - if the requested method is not accessible via reflection
      • invokeMethod

        public static java.lang.Object invokeMethod​(java.lang.Object object,
                                                    java.lang.String methodName,
                                                    java.lang.Object[] args)
                                             throws java.lang.NoSuchMethodException,
                                                    java.lang.IllegalAccessException,
                                                    java.lang.reflect.InvocationTargetException
        Deprecated.

        Invoke a named method whose parameter type matches the object type.

        This method delegates the method search to getMatchingAccessibleMethod(Class, String, Class[]).

        This method supports calls to methods taking primitive parameters via passing in wrapping classes. So, for example, a Boolean object would match a boolean primitive.

        This is a convenient wrapper for invokeMethod(Object object,String methodName, Object[] args, Class[] parameterTypes).

        Parameters:
        object - invoke method on this object
        methodName - get method with this name
        args - use these arguments - treat null as empty array
        Returns:
        The value returned by the invoked method
        Throws:
        java.lang.NoSuchMethodException - if there is no such accessible method
        java.lang.reflect.InvocationTargetException - wraps an exception thrown by the method invoked
        java.lang.IllegalAccessException - if the requested method is not accessible via reflection
      • invokeMethod

        public static java.lang.Object invokeMethod​(java.lang.Object object,
                                                    java.lang.String methodName,
                                                    java.lang.Object[] args,
                                                    java.lang.Class[] parameterTypes)
                                             throws java.lang.NoSuchMethodException,
                                                    java.lang.IllegalAccessException,
                                                    java.lang.reflect.InvocationTargetException
        Deprecated.

        Invoke a named method whose parameter type matches the object type.

        This method delegates the method search to getMatchingAccessibleMethod(Class, String, Class[]).

        This method supports calls to methods taking primitive parameters via passing in wrapping classes. So, for example, a Boolean object would match a boolean primitive.

        Parameters:
        object - invoke method on this object
        methodName - get method with this name
        args - use these arguments - treat null as empty array
        parameterTypes - match these parameters - treat null as empty array
        Returns:
        The value returned by the invoked method
        Throws:
        java.lang.NoSuchMethodException - if there is no such accessible method
        java.lang.reflect.InvocationTargetException - wraps an exception thrown by the method invoked
        java.lang.IllegalAccessException - if the requested method is not accessible via reflection
      • invokeExactMethod

        public static java.lang.Object invokeExactMethod​(java.lang.Object object,
                                                         java.lang.String methodName,
                                                         java.lang.Object arg)
                                                  throws java.lang.NoSuchMethodException,
                                                         java.lang.IllegalAccessException,
                                                         java.lang.reflect.InvocationTargetException
        Deprecated.

        Invoke a method whose parameter type matches exactly the object type.

        This is a convenient wrapper for invokeExactMethod(Object object,String methodName,Object [] args).

        Parameters:
        object - invoke method on this object
        methodName - get method with this name
        arg - use this argument
        Returns:
        The value returned by the invoked method
        Throws:
        java.lang.NoSuchMethodException - if there is no such accessible method
        java.lang.reflect.InvocationTargetException - wraps an exception thrown by the method invoked
        java.lang.IllegalAccessException - if the requested method is not accessible via reflection
      • invokeExactMethod

        public static java.lang.Object invokeExactMethod​(java.lang.Object object,
                                                         java.lang.String methodName,
                                                         java.lang.Object[] args)
                                                  throws java.lang.NoSuchMethodException,
                                                         java.lang.IllegalAccessException,
                                                         java.lang.reflect.InvocationTargetException
        Deprecated.

        Invoke a method whose parameter types match exactly the object types.

        This uses reflection to invoke the method obtained from a call to getAccessibleMethod().

        Parameters:
        object - invoke method on this object
        methodName - get method with this name
        args - use these arguments - treat null as empty array
        Returns:
        The value returned by the invoked method
        Throws:
        java.lang.NoSuchMethodException - if there is no such accessible method
        java.lang.reflect.InvocationTargetException - wraps an exception thrown by the method invoked
        java.lang.IllegalAccessException - if the requested method is not accessible via reflection
      • invokeExactMethod

        public static java.lang.Object invokeExactMethod​(java.lang.Object object,
                                                         java.lang.String methodName,
                                                         java.lang.Object[] args,
                                                         java.lang.Class[] parameterTypes)
                                                  throws java.lang.NoSuchMethodException,
                                                         java.lang.IllegalAccessException,
                                                         java.lang.reflect.InvocationTargetException
        Deprecated.

        Invoke a method whose parameter types match exactly the parameter types given.

        This uses reflection to invoke the method obtained from a call to getAccessibleMethod().

        Parameters:
        object - invoke method on this object
        methodName - get method with this name
        args - use these arguments - treat null as empty array
        parameterTypes - match these parameters - treat null as empty array
        Returns:
        The value returned by the invoked method
        Throws:
        java.lang.NoSuchMethodException - if there is no such accessible method
        java.lang.reflect.InvocationTargetException - wraps an exception thrown by the method invoked
        java.lang.IllegalAccessException - if the requested method is not accessible via reflection
      • invokeExactStaticMethod

        public static java.lang.Object invokeExactStaticMethod​(java.lang.Class cls,
                                                               java.lang.String methodName,
                                                               java.lang.Object[] args,
                                                               java.lang.Class[] parameterTypes)
                                                        throws java.lang.NoSuchMethodException,
                                                               java.lang.IllegalAccessException,
                                                               java.lang.reflect.InvocationTargetException
        Deprecated.

        Invoke a static method whose parameter types match exactly the parameter types given.

        This uses reflection to invoke the method obtained from a call to getAccessibleMethod(Class, String, Class[]).

        Parameters:
        cls - invoke static method on this class
        methodName - get method with this name
        args - use these arguments - treat null as empty array
        parameterTypes - match these parameters - treat null as empty array
        Returns:
        The value returned by the invoked method
        Throws:
        java.lang.NoSuchMethodException - if there is no such accessible method
        java.lang.reflect.InvocationTargetException - wraps an exception thrown by the method invoked
        java.lang.IllegalAccessException - if the requested method is not accessible via reflection
      • invokeStaticMethod

        public static java.lang.Object invokeStaticMethod​(java.lang.Class cls,
                                                          java.lang.String methodName,
                                                          java.lang.Object arg)
                                                   throws java.lang.NoSuchMethodException,
                                                          java.lang.IllegalAccessException,
                                                          java.lang.reflect.InvocationTargetException
        Deprecated.

        Invoke a named static method whose parameter type matches the object type.

        This method delegates the method search to getMatchingAccessibleMethod(Class, String, Class[]).

        This method supports calls to methods taking primitive parameters via passing in wrapping classes. So, for example, a Boolean class would match a boolean primitive.

        This is a convenient wrapper for invokeStaticMethod(Class objectClass,String methodName,Object [] args).

        Parameters:
        cls - invoke static method on this class
        methodName - get method with this name
        arg - use this argument
        Returns:
        The value returned by the invoked method
        Throws:
        java.lang.NoSuchMethodException - if there is no such accessible method
        java.lang.reflect.InvocationTargetException - wraps an exception thrown by the method invoked
        java.lang.IllegalAccessException - if the requested method is not accessible via reflection
      • invokeStaticMethod

        public static java.lang.Object invokeStaticMethod​(java.lang.Class cls,
                                                          java.lang.String methodName,
                                                          java.lang.Object[] args)
                                                   throws java.lang.NoSuchMethodException,
                                                          java.lang.IllegalAccessException,
                                                          java.lang.reflect.InvocationTargetException
        Deprecated.

        Invoke a named static method whose parameter type matches the object type.

        This method delegates the method search to getMatchingAccessibleMethod(Class, String, Class[]).

        This method supports calls to methods taking primitive parameters via passing in wrapping classes. So, for example, a Boolean class would match a boolean primitive.

        This is a convenient wrapper for invokeStaticMethod(Class objectClass,String methodName,Object [] args,Class[] parameterTypes).

        Parameters:
        cls - invoke static method on this class
        methodName - get method with this name
        args - use these arguments - treat null as empty array
        Returns:
        The value returned by the invoked method
        Throws:
        java.lang.NoSuchMethodException - if there is no such accessible method
        java.lang.reflect.InvocationTargetException - wraps an exception thrown by the method invoked
        java.lang.IllegalAccessException - if the requested method is not accessible via reflection
      • invokeStaticMethod

        public static java.lang.Object invokeStaticMethod​(java.lang.Class cls,
                                                          java.lang.String methodName,
                                                          java.lang.Object[] args,
                                                          java.lang.Class[] parameterTypes)
                                                   throws java.lang.NoSuchMethodException,
                                                          java.lang.IllegalAccessException,
                                                          java.lang.reflect.InvocationTargetException
        Deprecated.

        Invoke a named static method whose parameter type matches the object type.

        This method delegates the method search to getMatchingAccessibleMethod(Class, String, Class[]).

        This method supports calls to methods taking primitive parameters via passing in wrapping classes. So, for example, a Boolean class would match a boolean primitive.

        Parameters:
        cls - invoke static method on this class
        methodName - get method with this name
        args - use these arguments - treat null as empty array
        parameterTypes - match these parameters - treat null as empty array
        Returns:
        The value returned by the invoked method
        Throws:
        java.lang.NoSuchMethodException - if there is no such accessible method
        java.lang.reflect.InvocationTargetException - wraps an exception thrown by the method invoked
        java.lang.IllegalAccessException - if the requested method is not accessible via reflection
      • invokeExactStaticMethod

        public static java.lang.Object invokeExactStaticMethod​(java.lang.Class cls,
                                                               java.lang.String methodName,
                                                               java.lang.Object arg)
                                                        throws java.lang.NoSuchMethodException,
                                                               java.lang.IllegalAccessException,
                                                               java.lang.reflect.InvocationTargetException
        Deprecated.

        Invoke a static method whose parameter type matches exactly the object type.

        This is a convenient wrapper for invokeExactStaticMethod(Class objectClass,String methodName,Object [] args).

        Parameters:
        cls - invoke static method on this class
        methodName - get method with this name
        arg - use this argument
        Returns:
        The value returned by the invoked method
        Throws:
        java.lang.NoSuchMethodException - if there is no such accessible method
        java.lang.reflect.InvocationTargetException - wraps an exception thrown by the method invoked
        java.lang.IllegalAccessException - if the requested method is not accessible via reflection
      • invokeExactStaticMethod

        public static java.lang.Object invokeExactStaticMethod​(java.lang.Class cls,
                                                               java.lang.String methodName,
                                                               java.lang.Object[] args)
                                                        throws java.lang.NoSuchMethodException,
                                                               java.lang.IllegalAccessException,
                                                               java.lang.reflect.InvocationTargetException
        Deprecated.

        Invoke a static method whose parameter types match exactly the object types.

        This uses reflection to invoke the method obtained from a call to getAccessibleMethod(Class, String, Class[]).

        Parameters:
        cls - invoke static method on this class
        methodName - get method with this name
        args - use these arguments - treat null as empty array
        Returns:
        The value returned by the invoked method
        Throws:
        java.lang.NoSuchMethodException - if there is no such accessible method
        java.lang.reflect.InvocationTargetException - wraps an exception thrown by the method invoked
        java.lang.IllegalAccessException - if the requested method is not accessible via reflection
      • getAccessibleMethod

        public static java.lang.reflect.Method getAccessibleMethod​(java.lang.Class cls,
                                                                   java.lang.String methodName,
                                                                   java.lang.Class parameterType)
        Deprecated.

        Return an accessible method (that is, one that can be invoked via reflection) with given name and a single parameter. If no such method can be found, return null. Basically, a convenience wrapper that constructs a Class array for you.

        Parameters:
        cls - get method from this class
        methodName - get method with this name
        parameterType - taking this type of parameter
        Returns:
        The accessible method
      • getAccessibleMethod

        public static java.lang.reflect.Method getAccessibleMethod​(java.lang.Class cls,
                                                                   java.lang.String methodName,
                                                                   java.lang.Class[] parameterTypes)
        Deprecated.

        Return an accessible method (that is, one that can be invoked via reflection) with given name and parameters. If no such method can be found, return null. This is just a convenient wrapper for getAccessibleMethod(Method method).

        Parameters:
        cls - get method from this class
        methodName - get method with this name
        parameterTypes - with these parameters types
        Returns:
        The accessible method
      • getAccessibleMethod

        public static java.lang.reflect.Method getAccessibleMethod​(java.lang.reflect.Method method)
        Deprecated.

        Return an accessible method (that is, one that can be invoked via reflection) that implements the specified Method. If no such method can be found, return null.

        Parameters:
        method - The method that we wish to call
        Returns:
        The accessible method
      • getMatchingAccessibleMethod

        public static java.lang.reflect.Method getMatchingAccessibleMethod​(java.lang.Class cls,
                                                                           java.lang.String methodName,
                                                                           java.lang.Class[] parameterTypes)
        Deprecated.

        Find an accessible method that matches the given name and has compatible parameters. Compatible parameters mean that every method parameter is assignable from the given parameters. In other words, it finds a method with the given name that will take the parameters given.

        This method is used by invokeMethod(Object object, String methodName, Object[] args, Class[] parameterTypes).

        This method can match primitive parameter by passing in wrapper classes. For example, a Boolean will match a primitive boolean parameter.

        Parameters:
        cls - find method in this class
        methodName - find method with this name
        parameterTypes - find method with most compatible parameters
        Returns:
        The accessible method