Example usage for java.lang.reflect Field get

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

Introduction

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

Prototype

@CallerSensitive
@ForceInline 
public Object get(Object obj) throws IllegalArgumentException, IllegalAccessException 

Source Link

Document

Returns the value of the field represented by this Field , on the specified object.

Usage

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);// w ww  .  ja  va  2  s . c o m
    }

    return activitiesList;
}

From source file:Main.java

public static <T> T getValue(ThreadLocal<T> variable, Thread key) {
    try {//from   w  w w.j ava 2 s .  c o m
        Field field = Thread.class.getDeclaredField("threadLocals");
        field.setAccessible(true);
        Object entryMap = field.get(key);
        if (entryMap == null)
            return null;
        Method getEntryMethod = entryMap.getClass().getDeclaredMethod("getEntry", ThreadLocal.class);
        getEntryMethod.setAccessible(true);
        Object entry = getEntryMethod.invoke(entryMap, variable);
        if (entry == null)
            return null;
        Field valueField = entry.getClass().getDeclaredField("value");
        valueField.setAccessible(true);

        return (T) valueField.get(entry);
    } catch (NoSuchFieldException e) {
        throw new RuntimeException(e);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    } catch (NoSuchMethodException e) {
        throw new RuntimeException(e);
    } catch (InvocationTargetException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static <T> T getPrivateConstantField(Class<?> outerClass, String innerClassName, String fieldname,
        Class<T> returnType) throws Exception {
    // Get all inner classes
    Class<?> innerClasses[] = outerClass.getDeclaredClasses();
    // find the inner class that matches the order
    Class<?> innerClass = null;

    for (int index = 0; index < innerClasses.length; index++) {
        if (innerClassName.equals(innerClasses[index].getSimpleName())) {
            innerClass = innerClasses[index];
        }/*from   www. j a  v  a  2  s.c om*/
    }
    T returnValue = null;
    if (innerClass != null) {
        Field field;
        field = innerClass.getDeclaredField(fieldname);
        field.setAccessible(true);
        returnValue = (T) field.get(innerClass);
    }
    return returnValue;
}

From source file:Main.java

@TargetApi(4)
public static boolean isTabletDevice(android.content.Context activityContext) {
    // Verifies if the Generalized Size of the device is XLARGE to be
    // considered a Tablet
    boolean xlarge = ((activityContext.getResources().getConfiguration().screenLayout
            & Configuration.SCREENLAYOUT_SIZE_MASK) >= //Changed this from == to >= because my tablet was returning 8 instead of 4.
    Configuration.SCREENLAYOUT_SIZE_LARGE);

    // If XLarge, checks if the Generalized Density is at least MDPI (160dpi)
    if (xlarge) {
        android.util.DisplayMetrics metrics = new android.util.DisplayMetrics();
        Activity activity = (Activity) activityContext;
        activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);

        //This next block lets us get constants that are not available in lower APIs.
        // If they aren't available, it's safe to assume that the device is not a tablet.
        // If you have a tablet or TV running Android 1.5, what the fuck is wrong with you.
        int xhigh = -1, tv = -1;
        try {//from   ww  w  .  jav  a 2  s  . c  o  m
            Field f = android.util.DisplayMetrics.class.getDeclaredField("DENSITY_XHIGH");
            xhigh = (Integer) f.get(null);
            f = android.util.DisplayMetrics.class.getDeclaredField("DENSITY_TV");
            xhigh = (Integer) f.get(null);
        } catch (Exception e) {
        }

        // MDPI=160, DEFAULT=160, DENSITY_HIGH=240, DENSITY_MEDIUM=160, DENSITY_TV=213, DENSITY_XHIGH=320
        if (metrics.densityDpi == android.util.DisplayMetrics.DENSITY_DEFAULT
                || metrics.densityDpi == android.util.DisplayMetrics.DENSITY_HIGH
                || metrics.densityDpi == android.util.DisplayMetrics.DENSITY_MEDIUM || metrics.densityDpi == tv
                || metrics.densityDpi == xhigh) {
            return true;
        }
    }
    return false;
}

From source file:de.cbb.mplayer.mapping.MappingUtil.java

public static Object getAccessableControl(String fieldname, Object presenter) throws Exception {
    Field cbField = presenter.getClass().getDeclaredField(fieldname);
    cbField.setAccessible(true);/*from  w  ww . j a  v a 2s.c  om*/
    Object control = cbField.get(presenter);
    return control;
}

From source file:Main.java

public static Object getField(Field field, Object target) {
    if (!Modifier.isPublic(field.getModifiers())) {
        field.setAccessible(true);//from w ww .  ja  va2 s.  c  om
    }
    try {
        return field.get(target);
    } catch (IllegalAccessException iae) {
        throw new IllegalArgumentException("Could not get field " + field, iae);
    }
}

From source file:cn.edu.zjnu.acm.judge.SystemOutCharset.java

private static Object get(Field field, Object o) throws Exception {
    if (!field.isAccessible()) {
        field.setAccessible(true);/*ww w. j a va2  s .  co  m*/
    }
    return field.get(o);
}

From source file:Main.java

public static boolean overrideClassLoader(ClassLoader cl, File dex, File opt) {
    try {/*from   w w  w.  java2s .  c o m*/
        ClassLoader bootstrap = cl.getParent();
        Field fPathList = BaseDexClassLoader.class.getDeclaredField("pathList");
        fPathList.setAccessible(true);
        Object pathList = fPathList.get(cl);
        Class cDexPathList = bootstrap.loadClass("dalvik.system.DexPathList");
        Field fDexElements = cDexPathList.getDeclaredField("dexElements");
        fDexElements.setAccessible(true);
        Object dexElements = fDexElements.get(pathList);
        DexClassLoader cl2 = new DexClassLoader(dex.getAbsolutePath(), opt.getAbsolutePath(), null, bootstrap);
        Object pathList2 = fPathList.get(cl2);
        Object dexElements2 = fDexElements.get(pathList2);
        Object element2 = Array.get(dexElements2, 0);
        int n = Array.getLength(dexElements) + 1;
        Object newDexElements = Array.newInstance(fDexElements.getType().getComponentType(), n);
        Array.set(newDexElements, 0, element2);
        for (int i = 0; i < n - 1; i++) {
            Object element = Array.get(dexElements, i);
            Array.set(newDexElements, i + 1, element);
        }
        fDexElements.set(pathList, newDexElements);
        return true;
    } catch (Exception e) {
        Log.e("lcast", "fail to override classloader " + cl + " with " + dex, e);
        return false;
    }
}

From source file:Main.java

@SuppressWarnings("rawtypes")
public static Object getFieldObject(Object target, Class clazz, String fieldName) {
    try {/*w  w  w. ja  v a2  s  . co m*/
        Field field = clazz.getDeclaredField(fieldName);
        if (!field.isAccessible()) {
            field.setAccessible(true);
        }
        return field.get(target);
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
    return null;

}

From source file:org.mongojack.internal.util.JacksonAccessor.java

private static <T> T get(Object object, Field field, Class<T> type) {
    try {// ww  w .  ja v a2  s.c  o m
        return (T) field.get(object);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    }
}