invoke object's method with a single param - Android java.lang.reflect

Android examples for java.lang.reflect:Method Invoke

Description

invoke object's method with a single param

Demo Code


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

public class Main{
    /**/*from   w  w w.ja  v a2 s.  c om*/
     * invoke object's method with a single param   
     * @param Object: Object to be use
     * @param methodName: method to be invoke
     * @param argsClass: parameter's type
     * @param args: arguments
     * by lixl
     */
    public static Object invokeObjectMethod(Object o, String methodName,
            Class[] argsClass, Object[] args) {
        Object returnValue = null;
        try {
            Class<?> c = o.getClass();
            Method method;
            method = c.getMethod(methodName, argsClass);
            returnValue = method.invoke(o, args);
        } catch (Exception e) {
            // TODO Auto-generated catch block
          //  ExceptionHandler.processFatalException(e);
        }

        return returnValue;
    }
}

Related Tutorials