Java Reflection Method Invoke invokeMethod2(Class cls, Object obj, String methodName, Object[] args)

Here you can find the source of invokeMethod2(Class cls, Object obj, String methodName, Object[] args)

Description

invoke Method

License

MIT License

Declaration

public static Object invokeMethod2(Class cls, Object obj, String methodName, Object[] args) throws Exception 

Method Source Code

//package com.java2s;
/*******************************************************************************
 *  Copyright ? 2012-2015 eBay Software Foundation
 *  This program is dual licensed under the MIT and Apache 2.0 licenses.
 *  Please see LICENSE for more information.
 *******************************************************************************/

import java.lang.reflect.Method;

public class Main {
    public static Object invokeMethod2(Class cls, Object obj, String methodName, Object[] args) throws Exception {
        Method[] methods = cls.getDeclaredMethods();
        for (Method method : methods) {
            if (method.getName().equals(methodName)) {
                method.setAccessible(true);
                return method.invoke(obj, args);
            }//w  ww  .  j av a 2 s  . c o  m
        }
        return null;
    }
}

Related

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