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:com.codebase.foundation.classloader.xbean.ClassLoaderUtil.java

/**
 * Clears the caches maintained by the SunVM object stream implementation.  This method uses reflection and
 * setAccessable to obtain access to the Sun cache.  The cache is locked with a synchronize monitor and cleared.
 * This method completely clears the class loader cache which will impact performance of object serialization.
 * @param clazz the name of the class containing the cache field
 * @param fieldName the name of the cache field
 *//*from ww w.ja v a 2  s . co m*/
@SuppressWarnings("all")
public static void clearSunSoftCache(Class clazz, String fieldName) {
    Map cache = null;
    try {
        Field field = clazz.getDeclaredField(fieldName);
        field.setAccessible(true);
        cache = (Map) field.get(null);
    } catch (Throwable ignored) {
        // there is nothing a user could do about this anyway
    }

    if (cache != null) {
        synchronized (cache) {
            cache.clear();
        }
    }
}

From source file:Main.java

public static boolean isMeizuSecurity(Context context) {
    if (!isSecVer(context)) {
        return false;
    }//from  ww w  .  ja v  a 2  s  .  c  o m
    Boolean valueOf = Boolean.valueOf(false);
    try {
        Boolean bool;
        Class cls = Class.forName("android.os.Build");
        Field field = cls.getField("MeizuSecurity");
        field.setAccessible(true);
        bool = (Boolean) field.get(cls);
        return bool;
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (Error e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
    return valueOf.booleanValue();
}

From source file:bigbluej.ReflectionUtils.java

public static SortedMap<String, Object> getFieldsAndValuesInSortedMap(Object object)
        throws IllegalAccessException {
    Validate.notNull(object);/*from   ww w . java  2s.  c o  m*/
    SortedMap<String, Object> fieldAndValues = new TreeMap<>();
    for (Field field : object.getClass().getDeclaredFields()) {
        field.setAccessible(true);
        String name = field.getName();
        Object value = field.get(object);
        if (!field.isSynthetic() && value != null) {
            fieldAndValues.put(name, value);
        }
    }
    return fieldAndValues;
}

From source file:Main.java

private static LongSparseArray<Drawable.ConstantState> hackPreloadDrawablesV18(Resources res) {
    try {//w w w .  j a va  2  s.  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

/**
 * Determines whether notifications are enabled for the app represented by |context|.
 * Notifications may be disabled because either the user, or a management tool, has explicitly
 * disallowed the Chrome App to display notifications.
 *
 * This check requires Android KitKat or later. Earlier versions will return an INDETERMINABLE
 * status. When an exception occurs, an EXCEPTION status will be returned instead.
 *
 * @param context The context to check of whether it can show notifications.
 * @return One of the APP_NOTIFICATION_STATUS_* constants defined in this class.
 */// w  w  w .j  a  v  a2  s  .  com
@TargetApi(Build.VERSION_CODES.KITKAT)
static int determineAppNotificationStatus(Context context) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
        return APP_NOTIFICATIONS_STATUS_UNDETERMINABLE;
    }

    final String packageName = context.getPackageName();
    final int uid = context.getApplicationInfo().uid;
    final AppOpsManager appOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);

    try {
        Class appOpsManagerClass = Class.forName(AppOpsManager.class.getName());

        @SuppressWarnings("unchecked")
        final Method checkOpNoThrowMethod = appOpsManagerClass.getMethod(CHECK_OP_NO_THROW, Integer.TYPE,
                Integer.TYPE, String.class);

        final Field opPostNotificationField = appOpsManagerClass.getDeclaredField(OP_POST_NOTIFICATION);

        int value = (int) opPostNotificationField.get(Integer.class);
        int status = (int) checkOpNoThrowMethod.invoke(appOpsManager, value, uid, packageName);

        return status == AppOpsManager.MODE_ALLOWED ? APP_NOTIFICATIONS_STATUS_ENABLED
                : APP_NOTIFICATIONS_STATUS_DISABLED;

    } catch (RuntimeException e) {
    } catch (Exception e) {
        // Silently fail here, since this is just collecting statistics. The histogram includes
        // a count for thrown exceptions, if that proves to be significant we can revisit.
    }

    return APP_NOTIFICATIONS_STATUS_EXCEPTION;
}

From source file:Main.java

@Deprecated
public static void quitAppliation(Activity activity) {
    try {/* w ww.  j av  a 2 s  .  c om*/
        Class<?> clazz_Activity = Class.forName("android.app.Activity");
        Field field_mMainThread = clazz_Activity.getDeclaredField("mMainThread");
        field_mMainThread.setAccessible(true);
        Object mMainThread = field_mMainThread.get(activity);

        Class<?> clazz_ActivityThread = Class.forName("android.app.ActivityThread");
        Field field_mAppThread = clazz_ActivityThread.getDeclaredField("mAppThread");
        field_mAppThread.setAccessible(true);
        Object mAppThread = field_mAppThread.get(mMainThread);

        Class<?> clazz_ActivityClientRecord = Class.forName("android.app.ActivityThread$ApplicationThread");
        Method method_scheduleExit = clazz_ActivityClientRecord.getDeclaredMethod("scheduleExit");
        method_scheduleExit.invoke(mAppThread);
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

static Map<String, Object> objectToMap(Object object) {
    Map<String, Object> map = new HashMap<>();

    Class cls = object.getClass();
    while (cls != null) {
        for (Field field : cls.getDeclaredFields()) {
            field.setAccessible(true);//from w w w.j a va  2 s.com

            Object value = null;
            try {
                value = field.get(object);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }

            if (value != null)
                map.put(field.getName(), value);
        }

        cls = cls.getSuperclass();
    }

    return map;
}

From source file:Main.java

private static int getImageViewFieldValue(Object object, String fieldName) {
    int value = 0;
    try {/*  w  ww .j  ava2 s .  c o  m*/
        Field field = ImageView.class.getDeclaredField(fieldName);
        field.setAccessible(true);
        int fieldValue = (Integer) field.get(object);
        if (fieldValue > 0 && fieldValue < Integer.MAX_VALUE) {
            value = fieldValue;
        }
    } catch (Exception e) {
        Log.e("", e.getMessage());
    }
    return value;
}

From source file:Main.java

public static String readRegistryValue(boolean user, String path, String key) throws Exception {
    final Class clz = Class.forName("java.util.prefs.WindowsPreferences");
    final Field f = clz.getDeclaredField(user ? "userRoot" : "systemRoot");

    f.setAccessible(true);/*  www. jav a 2  s .  c om*/

    final Object root = f.get(null);
    final Method openKey = clz.getDeclaredMethod("openKey", byte[].class, int.class, int.class);

    openKey.setAccessible(true);

    final Method closeKey = clz.getDeclaredMethod("closeKey", int.class);

    closeKey.setAccessible(true);

    final Method winRegQueryValue = clz.getDeclaredMethod("WindowsRegQueryValueEx", int.class, byte[].class);

    winRegQueryValue.setAccessible(true);

    byte[] valb = null;
    String vals = null;
    Integer handle = -1;

    handle = (Integer) openKey.invoke(root, toCSTR(path), 0x20019, 0x20019);
    valb = (byte[]) winRegQueryValue.invoke(root, handle, toCSTR(key));
    vals = ((valb != null) ? new String(valb).trim() : null);
    closeKey.invoke(root, handle);

    return vals;
}

From source file:com.akamai.edgegrid.signer.restassured.RestAssuredEdgeGridRequestSigner.java

private static String getRequestPath(FilterableRequestSpecification requestSpec) {
    try {/*from   www  . j  a  v  a  2s.c o  m*/
        Field f = requestSpec.getClass().getDeclaredField("path");
        f.setAccessible(true);
        return (String) f.get(requestSpec);
    } catch (NoSuchFieldException | IllegalAccessException e) {
        throw new RuntimeException(e); // should never occur
    }
}