On this page

MethodUtils

public class org.apache.commons.lang3.reflect.MethodUtils[source]

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 java.lang.reflect.AccessibleObject#setAccessible(boolean) 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

Constructors

MethodUtils()

Deprecated. MethodUtils instances should NOT be constructed in standard programming.

@Deprecated public MethodUtils()[source]

Deprecated: TODO Make private in 4.0.

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.

Methods

getAccessibleMethod(Class, String, Class[])

Gets an accessible method (that is, one that can be invoked via reflection) with given name and parameters.

getAccessibleMethod(Class, Method)

Gets an accessible method (that is, one that can be invoked via reflection) that implements the specified Method.

getAccessibleMethod(Method)

Gets an accessible method (that is, one that can be invoked via reflection) that implements the specified Method.

getAnnotation(Method, Class, boolean, boolean)

Gets the annotation object with the given annotation type that is present on the given method or optionally on any equivalent method in super classes and interfaces.

getMatchingAccessibleMethod(Class, String, Class[])

Gets an accessible method that matches the given name and has compatible parameters.

getMatchingMethod(Class, String, Class[])

Gets a method whether or not it’s accessible.

getMethodObject(Class, String, Class[])

Gets a Method, or null if a checked Class#getMethod(String, Class...) exception is thrown.

getMethodsListWithAnnotation(Class, Class)

Gets all class level public methods of the given class that are annotated with the given annotation.

getMethodsListWithAnnotation(Class, Class, boolean, boolean)

Gets all methods of the given class that are annotated with the given annotation.

getMethodsWithAnnotation(Class, Class)

Gets all class level public methods of the given class that are annotated with the given annotation.

getMethodsWithAnnotation(Class, Class, boolean, boolean)

Gets all methods of the given class that are annotated with the given annotation.

getOverrideHierarchy(Method, Interfaces)

Gets the hierarchy of overridden methods down to result respecting generics.

invokeExactMethod(Object, String)

Invokes a method whose parameter types match exactly the object type.

invokeExactMethod(Object, String, Object[])

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

invokeExactMethod(Object, String, Object[], Class[])

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

invokeExactStaticMethod(Class, String, Object[])

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

invokeExactStaticMethod(Class, String, Object[], Class[])

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

invokeMethod(Object, boolean, String)

Invokes a named method without parameters.

invokeMethod(Object, boolean, String, Object[])

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

invokeMethod(Object, boolean, String, Object[], Class[])

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

invokeMethod(Object, String)

Invokes a named method without parameters.

invokeMethod(Object, String, Object[])

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

invokeMethod(Object, String, Object[], Class[])

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

invokeStaticMethod(Class, String, Object[])

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

invokeStaticMethod(Class, String, Object[], Class[])

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

public static Method getAccessibleMethod(Class<?> cls, String methodName, Class<?>... parameterTypes)[source]

Gets 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 convenience wrapper for #getAccessibleMethod(Method).

Parameters:
  • cls – get method from this class.

  • methodName – get method with this name.

  • parameterTypes – with these parameters types.

Returns:

The accessible method.

public static Method getAccessibleMethod(Class<?> cls, Method method)[source]

Gets 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:
  • cls – The implementing class, may be null.

  • method – The method that we wish to call, may be null.

Returns:

The accessible method or null.

Since:

3.19.0

public static Method getAccessibleMethod(Method method)[source]

Gets 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, may be null.

Returns:

The accessible method

public static <A extends Annotation> A getAnnotation(Method method, Class<A> annotationCls, boolean searchSupers, boolean ignoreAccess)[source]

Gets the annotation object with the given annotation type that is present on the given method or optionally on any equivalent method in super classes and interfaces. Returns null if the annotation type was not present.

Stops searching for an annotation once the first annotation of the specified type has been found. Additional annotations of the specified type will be silently ignored.

Type parameters:

<A> – the annotation type.

Parameters:
  • method – The Method to query, may be null.

  • annotationCls – The Annotation to check if is present on the method.

  • searchSupers – determines if a lookup in the entire inheritance hierarchy of the given class is performed if the annotation was not directly present.

  • ignoreAccess – determines if underlying method has to be accessible.

Returns:

The first matching annotation, or null if not found.

Throws:
  • java.lang.NullPointerException – Thrown if either the method or annotation class is null.

  • java.lang.SecurityException – Thrown if an underlying accessible object’s method denies the request.

See also:

SecurityManager#checkPermission

Since:

3.6

public static Method getMatchingAccessibleMethod(Class<?> cls, String methodName, Class<?>... requestTypes)[source]

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

  • requestTypes – find method with most compatible parameters.

Returns:

The accessible method or null.

Throws:

java.lang.SecurityException – Thrown if an underlying accessible object’s method denies the request.

See also:

SecurityManager#checkPermission

public static Method getMatchingMethod(Class<?> cls, String methodName, Class<?>... parameterTypes)[source]

Gets a method whether or not it’s accessible. If no such method can be found, return null.

Parameters:
  • cls – The class that will be subjected to the method search.

  • methodName – The method that we wish to call.

  • parameterTypes – Argument class types.

Throws:
  • java.lang.IllegalStateException – Thrown if there is no unique result.

  • java.lang.NullPointerException – Thrown if the class is null.

Returns:

The method.

Since:

3.5

public static Method getMethodObject(Class<?> cls, String name, Class<?>... parameterTypes)[source]

Gets a Method, or null if a checked Class#getMethod(String, Class...) exception is thrown.

Parameters:
Returns:

A Method or null.

See also:
Since:

3.15.0

public static List<Method> getMethodsListWithAnnotation(Class<?> cls, Class<? extends Annotation> annotationCls)[source]

Gets all class level public methods of the given class that are annotated with the given annotation.

Parameters:
  • cls – The Class to query.

  • annotationCls – The Annotation that must be present on a method to be matched.

Returns:

A list of Methods (possibly empty).

Throws:

java.lang.NullPointerException – Thrown if the class or annotation are null.

Since:

3.4

public static List<Method> getMethodsListWithAnnotation(Class<?> cls, Class<? extends Annotation> annotationCls, boolean searchSupers, boolean ignoreAccess)[source]

Gets all methods of the given class that are annotated with the given annotation.

Parameters:
  • cls – The Class to query.

  • annotationCls – The Annotation that must be present on a method to be matched.

  • searchSupers – determines if a lookup in the entire inheritance hierarchy of the given class should be performed.

  • ignoreAccess – determines if non-public methods should be considered.

Returns:

A list of Methods (possibly empty).

Throws:

java.lang.NullPointerException – Thrown if either the class or annotation class is null.

Since:

3.6

public static Method[] getMethodsWithAnnotation(Class<?> cls, Class<? extends Annotation> annotationCls)[source]

Gets all class level public methods of the given class that are annotated with the given annotation.

Parameters:
Returns:

An array of Methods (possibly empty).

Throws:

java.lang.NullPointerException – Thrown if the class or annotation are null.

Since:

3.4

public static Method[] getMethodsWithAnnotation(Class<?> cls, Class<? extends Annotation> annotationCls, boolean searchSupers, boolean ignoreAccess)[source]

Gets all methods of the given class that are annotated with the given annotation.

Parameters:
  • cls – The Class to query.

  • annotationCls – The java.lang.annotation.Annotation that must be present on a method to be matched.

  • searchSupers – determines if a lookup in the entire inheritance hierarchy of the given class should be performed.

  • ignoreAccess – determines if non-public methods should be considered.

Returns:

An array of Methods (possibly empty).

Throws:

java.lang.NullPointerException – Thrown if the class or annotation are null.

Since:

3.6

public static Set<Method> getOverrideHierarchy(Method method, ClassUtils.Interfaces interfacesBehavior)[source]

Gets the hierarchy of overridden methods down to result respecting generics.

Parameters:
  • method – lowest to consider.

  • interfacesBehavior – whether to search interfaces, null implies false.

Returns:

A Set<Method> in ascending order from subclass to superclass.

Throws:
  • java.lang.NullPointerException – Thrown if the specified method is null.

  • java.lang.SecurityException – Thrown if an underlying accessible object’s method denies the request.

See also:

SecurityManager#checkPermission

Since:

3.2

public static Object invokeExactMethod(Object object, String methodName) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException[source]

Invokes a method whose parameter types match exactly the object type.

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

Parameters:
  • object – invoke method on this object.

  • methodName – get method with this name.

Returns:

The value returned by the invoked method.

Throws:
  • java.lang.NoSuchMethodException – Thrown if there is no such accessible method.

  • java.lang.IllegalAccessException – Thrown if this found Method is enforcing Java language access control and the underlying method is inaccessible.

  • java.lang.IllegalArgumentException – Thrown if:

    • the found Method is an instance method and the specified object argument is not an instance of the class or interface declaring the underlying method (or of a subclass or interface implementor);

    • the number of actual and formal parameters differ;

  • java.lang.reflect.InvocationTargetException – Thrown if the underlying method throws an exception.

  • java.lang.NullPointerException – Thrown if the specified object is null.

  • java.lang.ExceptionInInitializerError – Thrown if the initialization provoked by this method fails.

Since:

3.4

public static Object invokeExactMethod(Object object, String methodName, Object... args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException[source]

Invokes a 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:
  • 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 – Thrown if there is no such accessible method.

  • java.lang.IllegalAccessException – Thrown if this found Method is enforcing Java language access control and the underlying method is inaccessible.

  • java.lang.IllegalArgumentException – Thrown if:

    • the found Method is an instance method and the specified object argument is not an instance of the class or interface declaring the underlying method (or of a subclass or interface implementor);

    • the number of actual and formal parameters differ;

    • an unwrapping conversion for primitive arguments fails; or

    • after possible unwrapping, a parameter value can’t be converted to the corresponding formal parameter type by a method invocation conversion.

  • java.lang.reflect.InvocationTargetException – Thrown if the underlying method throws an exception.

  • java.lang.NullPointerException – Thrown if the specified object is null.

  • java.lang.ExceptionInInitializerError – Thrown if the initialization provoked by this method fails.

public static Object invokeExactMethod(Object object, String methodName, Object[] args, Class<?>[] parameterTypes) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException[source]

Invokes a 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:
  • object – Invokes a method on this object.

  • methodName – Gets a method with this name.

  • args – Method 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 – Thrown if there is no such accessible method.

  • java.lang.IllegalAccessException – Thrown if this found Method is enforcing Java language access control and the underlying method is inaccessible.

  • java.lang.IllegalArgumentException – Thrown if:

    • the found Method is an instance method and the specified object argument is not an instance of the class or interface declaring the underlying method (or of a subclass or interface implementor);

    • the number of actual and formal parameters differ;

    • an unwrapping conversion for primitive arguments fails; or

    • after possible unwrapping, a parameter value can’t be converted to the corresponding formal parameter type by a method invocation conversion.

  • java.lang.reflect.InvocationTargetException – Thrown if the underlying method throws an exception.

  • java.lang.NullPointerException – Thrown if the specified object is null.

  • java.lang.ExceptionInInitializerError – Thrown if the initialization provoked by this method fails.

public static Object invokeExactStaticMethod(Class<?> cls, String methodName, Object... args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException[source]

Invokes 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 – Thrown if there is no such accessible method.

  • java.lang.IllegalAccessException – Thrown if this found Method is enforcing Java language access control and the underlying method is inaccessible.

  • java.lang.IllegalArgumentException – Thrown if:

    • the found Method is an instance method and the specified object argument is not an instance of the class or interface declaring the underlying method (or of a subclass or interface implementor);

    • the number of actual and formal parameters differ;

    • an unwrapping conversion for primitive arguments fails; or

    • after possible unwrapping, a parameter value can’t be converted to the corresponding formal parameter type by a method invocation conversion.

  • java.lang.reflect.InvocationTargetException – Thrown if the underlying method throws an exception.

  • java.lang.ExceptionInInitializerError – Thrown if the initialization provoked by this method fails.

public static Object invokeExactStaticMethod(Class<?> cls, String methodName, Object[] args, Class<?>[] parameterTypes) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException[source]

Invokes 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 – Thrown if there is no such accessible method.

  • java.lang.IllegalAccessException – Thrown if this found Method is enforcing Java language access control and the underlying method is inaccessible.

  • java.lang.IllegalArgumentException – Thrown if:

    • the found Method is an instance method and the specified object argument is not an instance of the class or interface declaring the underlying method (or of a subclass or interface implementor);

    • the number of actual and formal parameters differ;

    • an unwrapping conversion for primitive arguments fails; or

    • after possible unwrapping, a parameter value can’t be converted to the corresponding formal parameter type by a method invocation conversion.

  • java.lang.reflect.InvocationTargetException – Thrown if the underlying method throws an exception.

  • java.lang.ExceptionInInitializerError – Thrown if the initialization provoked by this method fails.

public static Object invokeMethod(Object object, boolean forceAccess, String methodName) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException[source]

Invokes a named method without parameters.

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

Parameters:
  • object – invoke method on this object.

  • forceAccess – force access to invoke method even if it’s not accessible.

  • methodName – get method with this name.

Returns:

The value returned by the invoked method.

Throws:
  • java.lang.NoSuchMethodException – Thrown if there is no such accessible method.

  • java.lang.IllegalAccessException – Thrown if this found Method is enforcing Java language access control and the underlying method is inaccessible.

  • java.lang.IllegalArgumentException – Thrown if:

    • the found Method is an instance method and the specified object argument is not an instance of the class or interface declaring the underlying method (or of a subclass or interface implementor);

    • the number of actual and formal parameters differ;

    • an unwrapping conversion for primitive arguments fails; or

    • after possible unwrapping, a parameter value can’t be converted to the corresponding formal parameter type by a method invocation conversion.

  • java.lang.reflect.InvocationTargetException – Thrown if the underlying method throws an exception.

  • java.lang.NullPointerException – Thrown if the specified object is null.

  • java.lang.ExceptionInInitializerError – Thrown if the initialization provoked by this method fails.

See also:

SecurityManager#checkPermission

Since:

3.5

public static Object invokeMethod(Object object, boolean forceAccess, String methodName, Object... args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException[source]

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

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, boolean forceAccess, String methodName, Object[] args, Class[] parameterTypes).

Parameters:
  • object – invoke method on this object.

  • forceAccess – force access to invoke method even if it’s not accessible.

  • 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 – Thrown if there is no such accessible method.

  • java.lang.IllegalAccessException – Thrown if this found Method is enforcing Java language access control and the underlying method is inaccessible.

  • java.lang.IllegalArgumentException – Thrown if:

    • the found Method is an instance method and the specified object argument is not an instance of the class or interface declaring the underlying method (or of a subclass or interface implementor);

    • the number of actual and formal parameters differ;

    • an unwrapping conversion for primitive arguments fails; or

    • after possible unwrapping, a parameter value can’t be converted to the corresponding formal parameter type by a method invocation conversion.

  • java.lang.reflect.InvocationTargetException – Thrown if the underlying method throws an exception.

  • java.lang.NullPointerException – Thrown if the specified object is null.

  • java.lang.ExceptionInInitializerError – Thrown if the initialization provoked by this method fails.

See also:

SecurityManager#checkPermission

Since:

3.5

public static Object invokeMethod(Object object, boolean forceAccess, String methodName, Object[] args, Class<?>[] parameterTypes) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException[source]

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

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.

  • forceAccess – force access to invoke method even if it’s not accessible.

  • 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 – Thrown if there is no such accessible method.

  • java.lang.IllegalAccessException – Thrown if this found Method is enforcing Java language access control and the underlying method is inaccessible.

  • java.lang.IllegalArgumentException – Thrown if:

    • the found Method is an instance method and the specified object argument is not an instance of the class or interface declaring the underlying method (or of a subclass or interface implementor);

    • the number of actual and formal parameters differ;

    • an unwrapping conversion for primitive arguments fails; or

    • after possible unwrapping, a parameter value can’t be converted to the corresponding formal parameter type by a method invocation conversion.

  • java.lang.reflect.InvocationTargetException – Thrown if the underlying method throws an exception.

  • java.lang.NullPointerException – Thrown if the specified object is null.

  • java.lang.ExceptionInInitializerError – Thrown if the initialization provoked by this method fails.

See also:

SecurityManager#checkPermission

Since:

3.5

public static Object invokeMethod(Object object, String methodName) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException[source]

Invokes a named method without parameters.

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

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.

Returns:

The value returned by the invoked method.

Throws:
  • java.lang.NoSuchMethodException – Thrown if there is no such accessible method.

  • java.lang.reflect.InvocationTargetException – Thrown to wrap an exception thrown by the method invoked.

  • java.lang.IllegalAccessException – Thrown if the requested method is not accessible via reflection.

  • java.lang.SecurityException – Thrown if an underlying accessible object’s method denies the request.

See also:

SecurityManager#checkPermission

Since:

3.4

public static Object invokeMethod(Object object, String methodName, Object... args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException[source]

Invokes 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 – Thrown if there is no such accessible method.

  • java.lang.reflect.InvocationTargetException – Thrown to wrap an exception thrown by the method invoked.

  • java.lang.IllegalAccessException – Thrown if the requested method is not accessible via reflection.

  • java.lang.NullPointerException – Thrown if the object or method name are null.

  • java.lang.SecurityException – Thrown if an underlying accessible object’s method denies the request.

See also:

SecurityManager#checkPermission

public static Object invokeMethod(Object object, String methodName, Object[] args, Class<?>[] parameterTypes) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException[source]

Invokes 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 – Thrown if there is no such accessible method.

  • java.lang.IllegalAccessException – Thrown if this found Method is enforcing Java language access control and the underlying method is inaccessible.

  • java.lang.IllegalArgumentException – Thrown if:

    • the found Method is an instance method and the specified object argument is not an instance of the class or interface declaring the underlying method (or of a subclass or interface implementor);

    • the number of actual and formal parameters differ;

    • an unwrapping conversion for primitive arguments fails; or

    • after possible unwrapping, a parameter value can’t be converted to the corresponding formal parameter type by a method invocation conversion.

  • java.lang.reflect.InvocationTargetException – Thrown if the underlying method throws an exception.

  • java.lang.NullPointerException – Thrown if the specified object is null.

  • java.lang.ExceptionInInitializerError – Thrown if the initialization provoked by this method fails.

See also:

SecurityManager#checkPermission

public static Object invokeStaticMethod(Class<?> cls, String methodName, Object... args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException[source]

Invokes 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, String, Object[], 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 – Thrown if there is no such accessible method.

  • java.lang.IllegalAccessException – Thrown if this found Method is enforcing Java language access control and the underlying method is inaccessible.

  • java.lang.IllegalArgumentException – Thrown if:

    • the found Method is an instance method and the specified object argument is not an instance of the class or interface declaring the underlying method (or of a subclass or interface implementor);

    • the number of actual and formal parameters differ;

    • an unwrapping conversion for primitive arguments fails; or

    • after possible unwrapping, a parameter value can’t be converted to the corresponding formal parameter type by a method invocation conversion.

  • java.lang.reflect.InvocationTargetException – Thrown if the underlying method throws an exception.

  • java.lang.ExceptionInInitializerError – Thrown if the initialization provoked by this method fails.

See also:

SecurityManager#checkPermission

public static Object invokeStaticMethod(Class<?> cls, String methodName, Object[] args, Class<?>[] parameterTypes) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException[source]

Invokes 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 – Thrown if there is no such accessible method.

  • java.lang.IllegalAccessException – Thrown if this found Method is enforcing Java language access control and the underlying method is inaccessible.

  • java.lang.IllegalArgumentException – Thrown if:

    • the found Method is an instance method and the specified object argument is not an instance of the class or interface declaring the underlying method (or of a subclass or interface implementor);

    • the number of actual and formal parameters differ;

    • an unwrapping conversion for primitive arguments fails; or

    • after possible unwrapping, a parameter value can’t be converted to the corresponding formal parameter type by a method invocation conversion.

  • java.lang.reflect.InvocationTargetException – Thrown if the underlying method throws an exception.

  • java.lang.ExceptionInInitializerError – Thrown if the initialization provoked by this method fails.

See also:

SecurityManager#checkPermission