get Hidden Method - Android java.lang.reflect

Android examples for java.lang.reflect:Method Get

Description

get Hidden Method

Demo Code


import android.text.TextUtils;
import android.util.Log;
import java.lang.reflect.Method;

public class Main{
    private static final String TAG = "";
    /**/*  w  w  w.j  a  v a 2 s. co m*/
     * @author kquan
     * This method returns a method that is hidden using a @hide annotation.  It should
     * not be used to retrieve methods that you do not have visibility to (i.e., private, protected
     * or default visibility)
     *
     * @param fullyQualifiedClassName
     * @param methodName
     * @param parameters
     * @return
     */
    public static Method getHiddenMethod(String fullyQualifiedClassName,
            String methodName, Class[] parameters) {
        if (TextUtils.isEmpty(fullyQualifiedClassName)) {
            return null;
        }
        Class classType = null;
        try {
            classType = Class.forName(fullyQualifiedClassName);
        } catch (ClassNotFoundException cnfe) {
        }

        if (classType == null) {
            Log.w(TAG, "Requested class could not be found: "
                    + fullyQualifiedClassName);
            return null;
        }

        return getHiddenMethod(classType, methodName, parameters);
    }
    /**
     * @author kquan
     * This method returns a method that is hidden using a @hide annotation.  It should
     * not be used to retrieve methods that you do not have visibility to (i.e., private, protected
     * or default visibility)
     *
     * @param classType
     * @param methodName
     * @param parameters
     * @return
     */
    public static Method getHiddenMethod(Class classType,
            String methodName, Class[] parameters) {
        if (classType == null || TextUtils.isEmpty(methodName)) {
            return null;
        }

        Method method = null;
        try {
            method = classType.getMethod(methodName, parameters);
        } catch (NoSuchMethodException nsme) {
            /* This is just for debugging this method...
            Log.d(TAG, "No method of name "+methodName, nsme);
            Method[] allMethods = classType.getMethods();
            for (Method m : allMethods) {
                Log.v(TAG, "Method: "+m.getName());
                Class[] debugParams = m.getParameterTypes();
                if (debugParams != null) {
                    for (int i = 0; i < debugParams.length; i++) {
                        Log.v(TAG, "Parameter "+i+" is of type "+debugParams[i].getName());
                    }
                }
            }
             */
        }

        return method;
    }
}

Related Tutorials