invoke Method via reflection - Android java.lang.reflect

Android examples for java.lang.reflect:Method Invoke

Description

invoke Method via reflection

Demo Code


import java.lang.reflect.Field;
import java.lang.reflect.Method;
import android.util.Log;

public class Main{
    private static final String TAG = "";
    public static Object invokeMethod(Class<?> methodClass,
            String methodName, Class<?>[] parameters, Object instance,
            Object... arguments) {
        try {//from   www .  ja  v a 2 s. co m
            final Method method = methodClass.getDeclaredMethod(methodName,
                    parameters);
            method.setAccessible(true);
            return method.invoke(instance, arguments);
        } catch (Exception e) {
            Log.w(TAG, Log.getStackTraceString(e));
        }
        return null;
    }
}

Related Tutorials