Java Reflection Method Invoke invokeMethodAndGet(Object object, String methodName, Object... args)

Here you can find the source of invokeMethodAndGet(Object object, String methodName, Object... args)

Description

Invokes and returns result of method with given name and arguments of target object's class.

License

Open Source License

Parameter

Parameter Description
object target object.
methodName name of the method.
args arguments.

Exception

Parameter Description
Exception an exception

Return

result of method invokation.

Declaration

public static Object invokeMethodAndGet(Object object, String methodName, Object... args) throws Exception 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.lang.reflect.Method;

public class Main {
    /**/*  w w  w.j av  a  2s .c  om*/
     * Invokes and returns result of method with given name and arguments of target object's class.
     * @param object target object.
     * @param methodName name of the method.
     * @param args arguments.
     * @return result of method invokation.
     * @throws Exception 
     */
    public static Object invokeMethodAndGet(Object object, String methodName, Object... args) throws Exception {
        Class[] params = new Class[args.length];
        for (int i = 0; i < args.length; ++i)
            params[i] = args[i].getClass();
        Method m = object.getClass().getDeclaredMethod(methodName, params);
        m.setAccessible(true);
        try {
            return m.invoke(object, args);
        } finally {
            m.setAccessible(false);
        }
    }
}

Related

  1. invokeMethod(Object theObject, String methodName, Object... parametersObject)
  2. invokeMethod(String className, String method, Class[] paramTypes, Object obj, Object[] args)
  3. invokeMethod(String methodName, Object gameCommand)
  4. invokeMethod(String name, Object target)
  5. invokeMethod2(Class cls, Object obj, String methodName, Object[] args)
  6. invokeMethodAndGetRecursively(Object object, String methodName, Object... args)
  7. invokeMethodAndReturn(Class clazz, String method, Object object)
  8. invokeMethodByName(@Nonnull T object, @Nonnull String name, Class typeArg, Object arg)
  9. invokeMethodByName(final Object obj, final String methodName, final Object[] args)