Java Method Call invoke(Object o, String name, Object[] args)

Here you can find the source of invoke(Object o, String name, Object[] args)

Description

invoke

License

Open Source License

Declaration

public static Object invoke(Object o, String name, Object[] args) 

Method Source Code

//package com.java2s;
/**/*from  ww  w . j av  a 2s  .co m*/
 * Copyright 2008, 2009 Mark Hooijkaas This file is part of the Caas tool. The Caas tool is free software: you can redistribute it
 * and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version
 * 3 of the License, or (at your option) any later version. The Caas tool is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details. You should have received a copy of the GNU General Public License along with the Caas
 * tool. If not, see <http://www.gnu.org/licenses/>.
 */

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

public class Main {
    public static Object invoke(Object o, Method m, Object[] args) {
        try {
            m.setAccessible(true);
            return m.invoke(o, args);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            e.getCause().printStackTrace();
            throw new RuntimeException(e.getCause());
        }
    }

    public static Object invoke(Object o, String name, Object[] args) {
        return invoke(o.getClass(), o, name, args);
    }

    public static Object invoke(Class<?> c, Object o, String name, Object[] args) {
        try {
            return invoke(o, c.getDeclaredMethod(name, getSignature(args)), args);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        }
    }

    private static Class<?>[] getSignature(Object[] args) {
        Class<?>[] signature = new Class<?>[args.length];
        for (int i = 0; i < args.length; i++)
            signature[i] = args[i].getClass();
        return signature;
    }
}

Related

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