Example usage for java.lang.reflect Field setAccessible

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

Introduction

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

Prototype

@Override
@CallerSensitive
public void setAccessible(boolean flag) 

Source Link

Usage

From source file:Main.java

/**
 * Resets MBeanServerFactory and ManagementFactory to a known consistent state.
 * This involves releasing all currently registered MBeanServers and resetting
 * the platformMBeanServer to null./*from w w w .  j  a  va  2  s.  c  o m*/
 */
public static void resetMBeanServers() throws Exception {
    for (MBeanServer server : MBeanServerFactory.findMBeanServer(null)) {
        MBeanServerFactory.releaseMBeanServer(server);
    }
    Field field = ManagementFactory.class.getDeclaredField("platformMBeanServer");
    field.setAccessible(true);
    field.set(null, null);
}

From source file:Main.java

private static List<Activity> getAllActivitiesHack() throws ClassNotFoundException, NoSuchMethodException,
        NoSuchFieldException, IllegalAccessException, InvocationTargetException {
    Class activityThreadClass = Class.forName("android.app.ActivityThread");
    Object activityThread = activityThreadClass.getMethod("currentActivityThread").invoke(null);
    Field activitiesField = activityThreadClass.getDeclaredField("mActivities");
    activitiesField.setAccessible(true);
    Map activities = (Map) activitiesField.get(activityThread);
    List<Activity> activitiesList = new ArrayList<Activity>(activities.size());

    for (Object activityRecord : activities.values()) {
        Class activityRecordClass = activityRecord.getClass();
        Field activityField = activityRecordClass.getDeclaredField("activity");
        activityField.setAccessible(true);
        Activity activity = (Activity) activityField.get(activityRecord);
        activitiesList.add(activity);/*from w  ww  . j  ava 2  s  .c  om*/
    }

    return activitiesList;
}

From source file:Main.java

private static boolean equalFields(Object paramObject1, Object paramObject2) {
    boolean bool1 = false;
    Field[] arrayOfField1 = paramObject1.getClass().getDeclaredFields();
    Field[] arrayOfField2 = paramObject2.getClass().getDeclaredFields();
    if (arrayOfField1.length != arrayOfField2.length) {
        return bool1;
    }//from w ww.j a  va2s .  c o m

    int i = 0;
    try {
        while (true) {
            if (i >= arrayOfField1.length)
                break;
            Field localField1 = arrayOfField1[i];
            localField1.setAccessible(true);
            Field localField2 = arrayOfField2[i];
            localField2.setAccessible(true);
            Object localObject1 = localField1.get(paramObject1);
            Object localObject2 = localField2.get(paramObject2);
            if ((localObject1 == null) && (localObject2 != null))
                break;
            if (localObject1 != null) {
                boolean bool2 = localObject1.equals(localObject2);
                if (!bool2)
                    break;
            }
            i++;
        }
    } catch (IllegalArgumentException localIllegalArgumentException) {
        localIllegalArgumentException.printStackTrace();
        bool1 = true;
    } catch (IllegalAccessException localIllegalAccessException) {
        label122: localIllegalAccessException.printStackTrace();
    }

    return bool1;
}

From source file:Main.java

public static Activity getCurrentTopActivity()
        throws ClassNotFoundException, IllegalArgumentException, SecurityException, IllegalAccessException,
        InvocationTargetException, NoSuchMethodException, NoSuchFieldException {
    Class<?> activityThreadClass = Class.forName("android.app.ActivityThread");
    Object activityThread = activityThreadClass.getMethod("currentActivityThread").invoke(null);
    Field activitiesField = activityThreadClass.getDeclaredField("mActivities");
    activitiesField.setAccessible(true);
    Map<?, ?> activities = (Map<?, ?>) activitiesField.get(activityThread);
    for (Object activityRecord : activities.values()) {
        Class<?> activityRecordClass = activityRecord.getClass();
        Field pausedField = activityRecordClass.getDeclaredField("paused");
        pausedField.setAccessible(true);
        if (!pausedField.getBoolean(activityRecord)) {
            Field activityField = activityRecordClass.getDeclaredField("activity");
            activityField.setAccessible(true);
            Activity activity = (Activity) activityField.get(activityRecord);
            return activity;
        }//from w ww  .ja  va2 s.  c om
    }
    return null;
}

From source file:Main.java

private static LongSparseArray<Drawable.ConstantState> hackPreloadDrawablesV18(Resources res) {
    try {/*w  ww. j  a  va 2s .  c o  m*/
        Field field = Resources.class.getDeclaredField("sPreloadedDrawables");
        field.setAccessible(true);
        return ((LongSparseArray<Drawable.ConstantState>[]) field.get(res))[0];
    } catch (NoSuchFieldException e) {
        // ignore
    } catch (IllegalAccessException e) {
        // ignore
    } catch (IllegalArgumentException e) {
        // ignore
    }
    return null;
}

From source file:Main.java

private static LongSparseArray<Drawable.ConstantState> hackPreloadDrawablesV15(Resources res) {
    try {//w  w  w . j  a  v  a 2 s. c om
        Field field = Resources.class.getDeclaredField("sPreloadedDrawables");
        field.setAccessible(true);
        return (LongSparseArray<Drawable.ConstantState>) field.get(res);
    } catch (NoSuchFieldException e) {
        // ignore
    } catch (IllegalAccessException e) {
        // ignore
    } catch (IllegalArgumentException e) {
        // ignore
    }
    return null;
}

From source file:Main.java

public static void setListViewEdgeEffectColor(AbsListView listView, int color) {
    if (Build.VERSION.SDK_INT >= 21) {
        try {//  w w w . j av a2 s.  c  o  m
            Field field = AbsListView.class.getDeclaredField("mEdgeGlowTop");
            field.setAccessible(true);
            EdgeEffect mEdgeGlowTop = (EdgeEffect) field.get(listView);
            if (mEdgeGlowTop != null) {
                mEdgeGlowTop.setColor(color);
            }

            field = AbsListView.class.getDeclaredField("mEdgeGlowBottom");
            field.setAccessible(true);
            EdgeEffect mEdgeGlowBottom = (EdgeEffect) field.get(listView);
            if (mEdgeGlowBottom != null) {
                mEdgeGlowBottom.setColor(color);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

public static Object set(Field f, Object obj, Object value)
        throws IllegalArgumentException, IllegalAccessException {
    f.setAccessible(true);
    f.set(obj, value);/* w  w  w. j  ava  2  s .c o m*/
    return f.get(obj);
}

From source file:Main.java

public static void clearCursorDrawable(EditText editText) {
    if (editText == null || Build.VERSION.SDK_INT < 12) {
        return;//  w w w  .  j  a  v  a 2 s  .  c o m
    }
    try {
        Field mCursorDrawableRes = TextView.class.getDeclaredField("mCursorDrawableRes");
        mCursorDrawableRes.setAccessible(true);
        mCursorDrawableRes.setInt(editText, 0);
    } catch (Exception e) {
        //            FileLog.e("tmessages", e);
    }
}

From source file:Main.java

private static void setDeclaredField(Object obj, String name, Object value)
        throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
    Field f = obj.getClass().getDeclaredField(name);
    f.setAccessible(true);
    f.set(obj, value);//  www.jav  a2 s  . c  o  m
}