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;
/**//ww w . ja v a 2 s .  com
Copyright 2008, 2009 Mark Hooijkaas
    
This file is part of the RelayConnector framework.
    
The RelayConnector framework 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 RelayConnector framework 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 RelayConnector framework.  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, String name, Object[] args) {
        return invoke(o.getClass(), o, name, args);
    }

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

    public static Object invoke(Object o, Method meth, Object[] args) {
        try {
            meth.setAccessible(true);
            return meth.invoke(o, args);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException 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 method, Object... param)
  2. invoke(Object o, String m, Object... params)
  3. invoke(Object o, String method, Object... args)
  4. invoke(Object o, String name)
  5. invoke(Object o, String name, Object[] args)
  6. invoke(Object obj, Method method, Object[] args)
  7. invoke(Object obj, Method method, Object[] params)
  8. invoke(Object obj, String method, Class[] params, Object[] args)
  9. invoke(Object obj, String method, Class[] params, Object[] args)