Java Method Call invoke(Object methodHostInstance, String methodName, Class[] parameterTypes, Object[] args)

Here you can find the source of invoke(Object methodHostInstance, String methodName, Class[] parameterTypes, Object[] args)

Description

invoke

License

Open Source License

Parameter

Parameter Description
methodHostInstance a parameter
methodName a parameter
parameterTypes a parameter
args a parameter

Return

Object

Declaration

public static Object invoke(Object methodHostInstance, String methodName, Class<?>[] parameterTypes,
        Object[] args) 

Method Source Code


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

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

public class Main {

    public static Object invoke(Object methodHostInstance, String methodName, Object arg) {
        Class<?>[] parameterTypes = { arg.getClass() };
        Object[] args = { arg };/*from  w  w w  . j  ava  2s .  c  o  m*/
        return invoke(methodHostInstance, methodName, parameterTypes, args);
    }

    public static Object invoke(Object methodHostInstance, String methodName, Class<?>[] parameterTypes,
            Object[] args) {
        try {
            Method method = methodHostInstance.getClass().getDeclaredMethod(methodName, parameterTypes);
            method.setAccessible(true);
            try {
                Object result = method.invoke(methodHostInstance, args);
                return result;
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } finally {
                method.setAccessible(false);
            }
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        return null;
    }
}

Related

  1. invoke(Object host, String method, Object[] args)
  2. invoke(Object instance, java.lang.reflect.Method method, Object... args)
  3. invoke(Object instance, Method method, Object... params)
  4. invoke(Object instance, String method, Class[] paramTypes, Object... parameters)
  5. invoke(Object invoker, String cmd, Map params)
  6. invoke(Object o, Method m)
  7. invoke(Object o, Method method, Object... param)
  8. invoke(Object o, String m, Object... params)
  9. invoke(Object o, String method, Object... args)