Package 

Class MethodUtils


  • 
    public class MethodUtils
    
                        

    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 LimitationsAccessing 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.

    • Constructor Detail

      • MethodUtils

        MethodUtils()
        MethodUtils instances should NOT be constructed in standard programming.
    • Method Detail

      • invokeMethod

         static Object invokeMethod(Object object, String methodName, Array<Object> args)

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

        This method delegates the method search to getMatchingAccessibleMethod.

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

        This is a convenient wrapper for invokeMethod.

        Parameters:
        object - invoke method on this object
        methodName - get method with this name
        args - use these arguments - treat null as empty array
      • invokeMethod

         static Object invokeMethod(Object object, String methodName, Array<Object> args, Array<Class<out Object>> parameterTypes)

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

        This method delegates the method search to getMatchingAccessibleMethod.

        This method supports calls to methods taking primitive parameters via passing in wrapping classes. So, for example, a Boolean objectwould 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
      • invokeExactMethod

         static Object invokeExactMethod(Object object, String methodName, Array<Object> args)

        Invokes a method whose parameter types match exactly the objecttypes.

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

        Parameters:
        object - invoke method on this object
        methodName - get method with this name
        args - use these arguments - treat null as empty array
      • invokeExactMethod

         static Object invokeExactMethod(Object object, String methodName, Array<Object> args, Array<Class<out Object>> parameterTypes)

        Invokes a method whose parameter types match exactly the parametertypes given.

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

        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
      • invokeExactStaticMethod

         static Object invokeExactStaticMethod(Class<out Object> cls, String methodName, Array<Object> args, Array<Class<out Object>> parameterTypes)

        Invokes a static method whose parameter types match exactly the parametertypes given.

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

        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
      • invokeStaticMethod

         static Object invokeStaticMethod(Class<out Object> cls, String methodName, Array<Object> args)

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

        This method delegates the method search to getMatchingAccessibleMethod.

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

        This is a convenient wrapper for invokeStaticMethod.

        Parameters:
        cls - invoke static method on this class
        methodName - get method with this name
        args - use these arguments - treat null as empty array
      • invokeStaticMethod

         static Object invokeStaticMethod(Class<out Object> cls, String methodName, Array<Object> args, Array<Class<out Object>> parameterTypes)

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

        This method delegates the method search to getMatchingAccessibleMethod.

        This method supports calls to methods taking primitive parameters via passing in wrapping classes. So, for example, a Boolean classwould 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
      • invokeExactStaticMethod

         static Object invokeExactStaticMethod(Class<out Object> cls, String methodName, Array<Object> args)

        Invokes a static method whose parameter types match exactly the objecttypes.

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

        Parameters:
        cls - invoke static method on this class
        methodName - get method with this name
        args - use these arguments - treat null as empty array
      • getAccessibleMethod

         static Method getAccessibleMethod(Class<out Object> cls, String methodName, Array<Class<out Object>> parameterTypes)

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

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

         static Method getAccessibleMethod(Method method)

        Returns an accessible method (that is, one that can be invoked viareflection) that implements the specified Method. If no such methodcan be found, return null.

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

         static Method getMatchingAccessibleMethod(Class<out Object> cls, String methodName, Array<Class<out Object>> parameterTypes)

        Finds 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 .

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

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