Java Method Call invoke(Object o, String method, Object... args)

Here you can find the source of invoke(Object o, String method, Object... args)

Description

invoke

License

Apache License

Declaration

public static Object invoke(Object o, String method, Object... args) throws Exception 

Method Source Code

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

import java.lang.reflect.Method;

public class Main {
    public static Object invoke(Object o, String method, Object... args) throws Exception {
        Class[] argTypes = new Class[args.length];

        for (int i = 0; i < args.length; i++) {
            argTypes[i] = args.getClass();
        }/*from  ww  w .  j  av  a  2s . c o m*/
        Method mthd = method(method, o.getClass());
        mthd.setAccessible(true);
        return mthd.invoke(o, args);
    }

    private static Method method(String name, Class klass) throws NoSuchFieldException {
        if (klass == null) {
            return null;
        }
        Method[] fields = klass.getDeclaredMethods();
        for (Method field : fields) {
            if (field.getName().equals(name)) {
                field.setAccessible(true);
                return field;
            }
        }
        return method(name, klass.getSuperclass());
    }
}

Related

  1. invoke(Object invoker, String cmd, Map params)
  2. invoke(Object methodHostInstance, String methodName, Class[] parameterTypes, Object[] args)
  3. invoke(Object o, Method m)
  4. invoke(Object o, Method method, Object... param)
  5. invoke(Object o, String m, Object... params)
  6. invoke(Object o, String name)
  7. invoke(Object o, String name, Object[] args)
  8. invoke(Object o, String name, Object[] args)
  9. invoke(Object obj, Method method, Object[] args)