Example usage for java.lang.reflect Method setAccessible

List of usage examples for java.lang.reflect Method setAccessible

Introduction

In this page you can find the example usage for java.lang.reflect Method setAccessible.

Prototype

@Override
@CallerSensitive
public void setAccessible(boolean flag) 

Source Link

Usage

From source file:Main.java

public static void removeSysKey(Activity context, EditText paramEditText) {
    if (Build.VERSION.SDK_INT <= 10) {
        paramEditText.setInputType(0);/*from w  w  w. java2 s . c o m*/
        return;
    }
    context.getWindow().setSoftInputMode(3);
    try {
        Class[] arrayOfClass = new Class[1];
        arrayOfClass[0] = Boolean.TYPE;
        Method localMethod = EditText.class.getMethod("setShowSoftInputOnFocus", arrayOfClass);
        localMethod.setAccessible(true);
        Object[] arrayOfObject = new Object[1];
        arrayOfObject[0] = Boolean.valueOf(false);
        localMethod.invoke(paramEditText, arrayOfObject);
        return;
    } catch (Exception localException) {
        localException.printStackTrace();
    }
}

From source file:Main.java

/**
 * Measures the height of the changeView based on the size 
 * of the the view that should be expand, and returns it.
 * /* w w  w.  j  ava  2 s . c o m*/
 * The two parameters are often the same (e.g. in case of a TextView,
 * it's content changes, so it should be measured again, 
 * and it will be the one that will be applied the animation to as well.
 * 
 * TODO: Add listener support for applied animation!
 */
private static int measureViewHeight(final View viewToExpand, final View changedView) {
    try {
        final Method m = changedView.getClass().getDeclaredMethod("onMeasure", int.class, int.class);
        m.setAccessible(true);
        m.invoke(changedView, MeasureSpec.makeMeasureSpec(viewToExpand.getWidth(), MeasureSpec.AT_MOST),
                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
    } catch (Exception e) {
        return -1;
    }
    int measuredHeight = changedView.getMeasuredHeight();
    return measuredHeight;
}

From source file:Main.java

public static List<String> getDirs(Context context) {
    List<String> dirs = new ArrayList<String>();
    StorageManager storageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
    try {/*from  w  w w. j a v a2 s  . c om*/
        Class[] paramClasses = {};
        Method getVolumePathsMethod = StorageManager.class.getMethod("getVolumePaths", paramClasses);
        getVolumePathsMethod.setAccessible(true);
        Object[] params = {};
        Object invoke = getVolumePathsMethod.invoke(storageManager, params);
        for (int i = 0; i < ((String[]) invoke).length; i++) {
            // System.out.println(((String[])invoke)[i]);
            dirs.add(((String[]) invoke)[i]);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return dirs;
}

From source file:Main.java

public static void callInjectViews(Object activity) {
    try {/*from w  w w . j ava  2s .  co  m*/
        Class<?> viewMembersInjectorClass = Class.forName("roboguice.inject.ViewListener$ViewMembersInjector");
        Method injectViewsMethod = viewMembersInjectorClass.getDeclaredMethod("injectViews", Object.class);
        injectViewsMethod.setAccessible(true);
        injectViewsMethod.invoke(null, activity);
    } catch (ClassNotFoundException | InvocationTargetException | IllegalArgumentException
            | IllegalAccessException | SecurityException | NoSuchMethodException e) {
        throw new RuntimeException("Could not invoke RoboGuice method!", e);
    }
}

From source file:Main.java

/**
 * Sets the root of the preference hierarchy.
 *
 * @param preferenceScreen The root {@link android.preference.PreferenceScreen} of the preference hierarchy.
 * @return Whether the {@link android.preference.PreferenceScreen} given is different than the previous.
 *///from   w w w. ja va 2  s  . co  m
public static boolean setPreferences(@NonNull PreferenceManager manager, PreferenceScreen preferenceScreen) {
    try {
        Method m = PreferenceManager.class.getDeclaredMethod("setPreferences", PreferenceScreen.class);
        m.setAccessible(true);
        return ((Boolean) m.invoke(manager, preferenceScreen));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static int getApiLevel() {

    try {/*from w ww.  ja v  a2s. com*/
        Class<?> mClassType = Class.forName("android.os.SystemProperties");
        Method mGetIntMethod = mClassType.getDeclaredMethod("getInt", String.class, int.class);
        mGetIntMethod.setAccessible(true);
        return (Integer) mGetIntMethod.invoke(null, "ro.build.version.sdk", 14);
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return 14;

}

From source file:Main.java

/**
 * Requires {@link android.Manifest.permission#READ_PHONE_STATE} permission.
 *
 * @param context/*from w w w  .  j av a2  s  .co  m*/
 * @return
 */
public static boolean isMobileDataEnabled(Context context) {
    boolean mobileDataEnabled = false; // Assume disabled

    if (Build.VERSION.SDK_INT >= 21) {
        try {
            TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            Class<?> tmClass = Class.forName(tm.getClass().getName());
            Method method = tmClass.getDeclaredMethod("getDataEnabled");
            method.setAccessible(true);
            mobileDataEnabled = (Boolean) method.invoke(tm);
        } catch (ClassNotFoundException | NoSuchMethodException | IllegalArgumentException
                | IllegalAccessException | InvocationTargetException ex) {
            ex.printStackTrace();
        }
    } else {
        try {
            ConnectivityManager cm = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            Class<?> cmClass = Class.forName(cm.getClass().getName());
            Method method = cmClass.getDeclaredMethod("getMobileDataEnabled");
            method.setAccessible(true);
            mobileDataEnabled = (Boolean) method.invoke(cm);
        } catch (ClassNotFoundException | NoSuchMethodException | IllegalArgumentException
                | IllegalAccessException | InvocationTargetException ex) {
            ex.printStackTrace();
        }
    }

    return mobileDataEnabled;
}

From source file:Main.java

public static Object getVariableFromMethod(Activity activity, String method) {
    Object value = false;/*from  w  w  w  . j av a 2 s . c o  m*/
    Class c = activity.getClass();
    try {
        Method m = (Method) c.getDeclaredMethod(method, new Class[] {});
        m.setAccessible(true);
        value = m.invoke(activity, new Object[] {});
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    }
    return value;
}

From source file:Main.java

/**
 * Call a private method of a given class object
 * //from   w w w  . j a  va 2s  .  c o m
 * @param objectInstance Class object to invoke method on
 * @param methodName Method name to invoke
 * @param int1 int Parameter to the method
 * @return Return value from invoked method
 * 
 * @throws NoSuchMethodException
 * @throws IllegalArgumentException
 * @throws IllegalAccessException
 * @throws InvocationTargetException
 */
public static Object callPrivateMethod(Object objectInstance, String methodName, int int1)
        throws NoSuchMethodException, IllegalArgumentException, IllegalAccessException,
        InvocationTargetException // NOSONAR
{

    Method method = objectInstance.getClass().getDeclaredMethod(methodName, Integer.TYPE);

    method.setAccessible(true);

    return method.invoke(objectInstance, int1);
}

From source file:Main.java

/**
 * Call a private method of a given class object - with no parameters
 * /*www  . j  a va  2 s  . com*/
 * @param objectInstance Class object to invoke method on
 * @param methodName Method name to invoke
 * @param inputVal
 * @return Return value from invoked method
 * 
 * @throws IllegalArgumentException
 * @throws NoSuchMethodException
 * @throws IllegalAccessException
 * @throws InvocationTargetException
 */
public static Object callPrivateMethod(Object objectInstance, String methodName)
        throws IllegalArgumentException, IllegalAccessException, InvocationTargetException,
        NoSuchMethodException // NOSONAR
{
    Object[] params = null;

    Method method = objectInstance.getClass().getDeclaredMethod(methodName, (Class[]) null);

    method.setAccessible(true);

    return method.invoke(objectInstance, params);
}