Java Reflection Method Invoke invokeMethod(Object destination, String methodName, Object argument)

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

Description

Invokes the given method with the given argument.

License

Open Source License

Parameter

Parameter Description
destination the object upon which the method should be invoked, cannnot be <code>null</code>.
methodName the name of the method to invoke, cannot be <code>null</code>.
argument the argument for the method, can be <code>null</code>.

Exception

Parameter Description
Exception if the call to the method failed for any reason.

Declaration

private static void invokeMethod(Object destination, String methodName, Object argument) throws Exception 

Method Source Code


//package com.java2s;

import java.lang.reflect.Method;

public class Main {
    /**/*from   w w w .  j  a  v a  2 s  .c  o m*/
     * Invokes the given method with the given argument.
     *
     * @param destination
     *    the object upon which the method should be invoked, cannnot be <code>null</code>.
     *
     * @param methodName
     *    the name of the method to invoke, cannot be <code>null</code>.
     *
     * @param argument
     *    the argument for the method, can be <code>null</code>.
     *
     * @throws Exception
     *    if the call to the method failed for any reason.
     */
    private static void invokeMethod(Object destination, String methodName, Object argument) throws Exception {
        Class argumentClass = argument.getClass();
        Class[] argsClasses = { argumentClass };
        if (argument instanceof Boolean) {
            try {
                destination.getClass().getMethod(methodName, argsClasses);
            } catch (NoSuchMethodException nsmex) {
                argumentClass = Boolean.TYPE;
            }
        } else if (argument instanceof Number) {
            try {
                destination.getClass().getMethod(methodName, argsClasses);
            } catch (NoSuchMethodException nsmex) {
                argumentClass = (Class) argumentClass.getDeclaredField("TYPE").get(argument);
            }
        }
        Class[] argsClasses2 = { argumentClass };
        Object[] args = { argument };
        Method setMethod = destination.getClass().getMethod(methodName, argsClasses2);
        setMethod.invoke(destination, args);
    }
}

Related

  1. invokeMethod(Object bean, Method method, Object[] args)
  2. invokeMethod(Object caller, Object classInstance, String className, String methodName, String parameterClasses[], Object[] parameters)
  3. invokeMethod(Object caller, String methodName, Object[] params)
  4. invokeMethod(Object classInstance, Method method, Object... args)
  5. invokeMethod(Object cls, String methodName, Class paramClass, String paramValue)
  6. invokeMethod(Object handle, String methodName, Class[] parameterClasses, Object... args)
  7. invokeMethod(Object handler, String strMethod, Class[] cls, Object... params)
  8. invokeMethod(Object instance, String methodName, Class expectedReturnType)
  9. invokeMethod(Object o, String fieldName)