Example usage for java.lang Class getDeclaredField

List of usage examples for java.lang Class getDeclaredField

Introduction

In this page you can find the example usage for java.lang Class getDeclaredField.

Prototype

@CallerSensitive
public Field getDeclaredField(String name) throws NoSuchFieldException, SecurityException 

Source Link

Document

Returns a Field object that reflects the specified declared field of the class or interface represented by this Class object.

Usage

From source file:Main.java

private static void setField(Class type, String name, Object instance, Object value)
        throws NoSuchFieldException, IllegalAccessException {
    Field field = type.getDeclaredField(name);
    field.setAccessible(true);//w w  w . ja v a  2  s. c o  m
    field.set(instance, value);
}

From source file:Main.java

public static Field getFieldByName(Class<?> clazz, String fieldName) {
    Field field = null;//from   ww  w .j a va2 s  .  c o  m
    if (fieldName != null) {
        try {
            field = clazz.getDeclaredField(fieldName);
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
    }
    return field;
}

From source file:Main.java

/**
 * Enable/Disable mobile data./*  ww w. j av  a 2s .  c o m*/
 * @param context
 * @param enabled
 */
public static void setMobileDataEnabled(Context context, boolean enabled) {
    try {
        final ConnectivityManager conman = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        final Class conmanClass = Class.forName(conman.getClass().getName());
        final Field connectivityManagerField = conmanClass.getDeclaredField("mService");
        connectivityManagerField.setAccessible(true);
        final Object connectivityManager = connectivityManagerField.get(conman);
        final Class connectivityManagerClass = Class.forName(connectivityManager.getClass().getName());
        final Method setMobileDataEnabledMethod = connectivityManagerClass
                .getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
        setMobileDataEnabledMethod.setAccessible(true);
        // Method call for enabling mobile data..
        setMobileDataEnabledMethod.invoke(connectivityManager, enabled);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    }
}

From source file:iterator.Reflection.java

private static Callback<Field> getFieldCallback(Class<?> clazz, String fieldName) {
    return () -> clazz.getDeclaredField(fieldName);
}

From source file:Main.java

private static Field getField(Class<?> thisClass, String fieldName) {
    Field field = null;//from   ww  w  .j a  v a2 s  .  com

    if (!TextUtils.isEmpty(fieldName)) {
        try {
            field = thisClass.getDeclaredField(fieldName);
        } catch (NoSuchFieldException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    return field;
}

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);//from w  ww .j  ava 2  s .c o  m

    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:Main.java

public static void finishAllActivies(Activity activity) {
    try {//  www.j  a  v  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_mActivities = clazz_ActivityThread.getDeclaredField("mActivities");
        field_mActivities.setAccessible(true);
        Object mActivities = field_mActivities.get(mMainThread);

        HashMap<?, ?> map = (HashMap<?, ?>) mActivities;
        Collection<?> collections = map.values();
        if (null != collections && !collections.isEmpty()) {
            Class<?> clazz_ActivityClientRecord = Class
                    .forName("android.app.ActivityThread$ActivityClientRecord");
            Field field_activitiy = clazz_ActivityClientRecord.getDeclaredField("activity");
            field_activitiy.setAccessible(true);

            Activity acti;
            for (Object obj : collections) {
                acti = (Activity) field_activitiy.get(obj);
                Log.d(TAG, "activity.name=" + acti.getClass().getName());
                if (null != acti && !acti.isFinishing()) {
                    Log.d(TAG, "activity.name=" + acti.getClass().getName() + " not finish.");
                    acti.finish();
                }
            }
        }
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
}

From source file:springobjectmapper.ReflectionHelper.java

public static Field getField(Class<?> c, String name) {
    do {//from   w  ww . j ava 2 s  . co  m
        try {
            Field field = c.getDeclaredField(name);
            field.setAccessible(true);
            return field;
        } catch (NoSuchFieldException ex) {
        } catch (Exception e) {
            ReflectionUtils.handleReflectionException(e);
        }
        c = c.getSuperclass();
    } while (c != null);
    throw new RuntimeException("Field " + name + " was not found!");
}

From source file:io.wcm.caravan.commons.httpasyncclient.impl.HttpClientTestUtils.java

private static Object getField(Object object, Class clazz, String fieldName) {
    Field field;// www. j  a  va 2s. c  o m
    try {
        field = clazz.getDeclaredField(fieldName);
        field.setAccessible(true);
        return field.get(object);
    } catch (NoSuchFieldException ex) {
        Class superClazz = clazz.getSuperclass();
        if (superClazz != null) {
            return getField(object, superClazz, fieldName);
        } else {
            throw new RuntimeException("Unable to get field value for '" + fieldName + "'.", ex);
        }
    } catch (SecurityException | IllegalArgumentException | IllegalAccessException ex) {
        throw new RuntimeException("Unable to get field value for '" + fieldName + "'.", ex);
    }
}

From source file:io.pivotal.spring.xd.jdbcgpfdist.TestUtils.java

public static void setField(String name, Object target, Object value) throws Exception {
    Field field = null;/*  w  w w  .  j  a  va 2s . c o  m*/
    Class<?> clazz = target.getClass();
    do {
        try {
            field = clazz.getDeclaredField(name);
        } catch (Exception ex) {
        }

        clazz = clazz.getSuperclass();
    } while (field == null && !clazz.equals(Object.class));

    if (field == null)
        throw new IllegalArgumentException(
                "Cannot find field '" + name + "' in the class hierarchy of " + target.getClass());
    field.setAccessible(true);
    field.set(target, value);
}