Java Reflection Method Invoke invoke(Method method, Object obj, Object... args)

Here you can find the source of invoke(Method method, Object obj, Object... args)

Description

Invoke the method event if not accessible.

License

Apache License

Parameter

Parameter Description
method method to invoke
obj object to invoke
args arguments of the method

Exception

Parameter Description
IllegalAccessException if this Method objectis enforcing Java language access control and the underlyingmethod is inaccessible.
InvocationTargetException if the underlying methodthrows an exception.

Return

return value from the method invocation

Declaration

public static Object invoke(Method method, Object obj, Object... args)
        throws InvocationTargetException, IllegalAccessException 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Main {
    /**/*from www  .j ava 2s . c  o  m*/
     * Invoke the method event if not accessible.
     *
     * @param method method to invoke
     * @param obj    object to invoke
     * @param args   arguments of the method
     * @return return value from the method invocation
     * @throws IllegalAccessException    if this {@code Method} object
     *                                   is enforcing Java language access control and the underlying
     *                                   method is inaccessible.
     * @throws InvocationTargetException if the underlying method
     *                                   throws an exception.
     * @see Method#invoke(Object, Object...)
     */
    public static Object invoke(Method method, Object obj, Object... args)
            throws InvocationTargetException, IllegalAccessException {
        boolean accessible = method.isAccessible();
        if (accessible) {
            return method.invoke(obj, args);
        }
        method.setAccessible(true);
        try {
            return method.invoke(obj, args);
        } finally {
            method.setAccessible(accessible);
        }
    }
}

Related

  1. invoke(Method method, Object data, Object... arguments)
  2. invoke(Method method, Object instance, Object... parameters)
  3. invoke(Method method, Object it, Object... args)
  4. invoke(Method method, Object javaBean, Object value)
  5. invoke(Method method, Object obj, Object... args)
  6. invoke(Method method, Object object, Object... args)
  7. invoke(Method method, Object object, Object... arguments)
  8. invoke(Method method, Object object, Object... arguments)
  9. invoke(Method method, Object object, Object... parameters)