Java Reflection Method Invoke invokeMethod(Object owner, String methodName)

Here you can find the source of invokeMethod(Object owner, String methodName)

Description

invoke Method

License

Open Source License

Declaration

public static Object invokeMethod(Object owner, String methodName) throws Exception 

Method Source Code

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

import java.lang.reflect.Method;

public class Main {

    public static Object invokeMethod(Object owner, String methodName) throws Exception {
        return invokeMethod(owner, methodName, new Object[] {});
    }//  w w  w .j  av  a 2  s .com

    public static Object invokeMethod(Object owner, String methodName, Object... args) throws Exception {

        Class<?> ownerClass = owner.getClass();
        int argsNum = 0;
        Class<?>[] argsClass;
        if (args == null) {
            argsClass = new Class[0];
        } else {
            argsNum = args.length;
            argsClass = new Class[argsNum];
        }
        for (int i = 0, j = argsNum; i < j; i++) {
            argsClass[i] = args[i].getClass();
        }
        Method method = ownerClass.getMethod(methodName, argsClass);
        return method.invoke(owner, args);
    }
}

Related

  1. invokeMethod(Object object, String methodName, Object[] args, Class[] parameterTypes)
  2. invokeMethod(Object object, String methodName, Object[] params, Object[] result)
  3. invokeMethod(Object object, String name, Object... arguments)
  4. invokeMethod(Object object, String propertyName)
  5. invokeMethod(Object objectInstance, String methodToInvoke, Class[] parameterTypes, Object[] instanceParameters)
  6. invokeMethod(Object owner, String methodName, Object[] args)
  7. invokeMethod(Object parent, String method, Class[] paramTypes, Object[] args)
  8. invokeMethod(Object source, String method, Collection arguments)
  9. invokeMethod(Object sourceObject, String methodName, Object... arguments)

  10. HOME | Copyright © www.java2s.com 2016