get Method from class by method name - Android java.lang.reflect

Android examples for java.lang.reflect:Method Get

Description

get Method from class by method name

Demo Code


import android.content.Context;
import android.util.Log;
import java.lang.reflect.Method;

public class Main{
    private static final String TAG = Util.getTag(ReflectionHelper.class);
    public static Method getMethod(Context context,
            String packageAndClassName, String methodName,
            Class... methodArgumentypes) throws ReflectionHelperException {
        try {//from w w  w  .  j  ava2  s  . com
            Class clazz = loadClass(context, packageAndClassName);
            return clazz.getMethod(methodName, methodArgumentypes);
        } catch (NoSuchMethodException e) {
            Log.e(TAG, "Could not find method:'" + methodName
                    + "' in class:'" + packageAndClassName + "'", e);
            throw new ReflectionHelperException(e);
        }
    }
    private static Class loadClass(Context context,
            String packageAndClassName) throws ReflectionHelperException {
        try {
            return context.getClassLoader().loadClass(packageAndClassName);
        } catch (ClassNotFoundException e) {
            Log.e(TAG, "Could not load class", e);
            throw new ReflectionHelperException(e);
        }
    }
}

Related Tutorials