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

/**
 * Gets the field with the given name within the class, or {@code null} if not found. If found,
 * the field is made accessible.//from  w ww  .  j  ava 2s  .  com
 */
private static Field field(Class<?> clazz, String fieldName) {
    Field field;
    try {
        field = clazz.getDeclaredField(fieldName);
        field.setAccessible(true);
    } catch (Throwable t) {
        // Failed to access the fields.
        field = null;
    }
    return field;
}

From source file:Main.java

private static Field getFieldByNameFromAnywhere(Class<?> aClass, String fieldName) {
    Field result = null;/*from www  . j a  v  a 2s .  c o m*/
    try {
        result = aClass.getDeclaredField(fieldName);
    } catch (Exception e) {
        result = getFieldByNameFromAnywhere(aClass.getSuperclass(), fieldName);
    }
    return result;
}

From source file:Main.java

public static void init() {
    try {/*from   w w  w  . j av  a  2 s.c o  m*/
        Class AppOpsManager = Class.forName("android.app.AppOpsManager");
        Field field = AppOpsManager.getDeclaredField("OP_SYSTEM_ALERT_WINDOW");
        field.setAccessible(true);
        OP_SYSTEM_ALERT_WINDOW = field.getInt(null);
    } catch (ClassNotFoundException e2) {

    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
}

From source file:ReflectionHelper.java

/**
 * Checks whether the specified class contains a field matching the specified name.
 *
 * @param clazz The class to check./*  w  ww  . ja  v  a  2s . com*/
 * @param fieldName The field name.
 *
 * @return Returns <code>true</code> if the cass contains a field for the specified name, <code>
 *         false</code> otherwise.
 */
public static boolean containsField(Class<?> clazz, String fieldName) {
    try {
        clazz.getDeclaredField(fieldName);
        return true;
    } catch (NoSuchFieldException e) {
        return false;
    }
}

From source file:Main.java

private static void loadClass(Class clazz) throws NoSuchFieldException, IllegalAccessException {
    final Field id = clazz.getDeclaredField("ID");
    id.setAccessible(true);//from   ww w  . ja v  a  2 s.  c o m
    id.get(null);
}

From source file:Main.java

/**
 * @param clz class name//from  w ww. j ava 2s.  c o  m
 * @param fieldName field name
 * @return true if the field is in the class, else false
 */
public static boolean existsField(Class clz, String fieldName) {
    if (null != clz) {
        try {
            return clz.getDeclaredField(fieldName) != null;
        } catch (Exception e) {
        }
        if (clz != Object.class) {
            return existsField(clz.getSuperclass(), fieldName);
        }
    }
    return false;
}

From source file:Main.java

public static <T> void setFinalField(Class<T> clazz, T object, String fieldName, Object value) {
    try {//w w  w . jav  a 2s .  c om
        Field headerField = clazz.getDeclaredField(fieldName);
        headerField.setAccessible(true);
        headerField.set(object, value);
    } catch (NoSuchFieldException e) {
        throw new RuntimeException(e);
    } catch (IllegalAccessException e2) {
        throw new RuntimeException(e2);
    }
}

From source file:Main.java

public static void setFieldValue(Class<?> fieldClass, String fieldName, Object instance, Object value) {
    try {//from  ww  w . j a va  2  s  . c  o  m
        Field e = fieldClass.getDeclaredField(fieldName);
        e.setAccessible(true);
        e.set(instance, value);
    } catch (Exception var5) {
        var5.printStackTrace();
    }

}

From source file:Main.java

public static Object getReflection(Object itemToGetObject, String objectName) {
    try {/* w w w. jav  a2s. com*/
        Class<?> clazz = itemToGetObject.getClass();
        Field field;
        field = clazz.getDeclaredField(objectName);
        field.setAccessible(true);
        return field.get(itemToGetObject);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static boolean setReflection(Object itemToGetObject, String objectName, Object newValue) {
    try {/*  www .  ja va 2s. c om*/
        Class<?> clazz = itemToGetObject.getClass();
        Field field;
        field = clazz.getDeclaredField(objectName);
        field.setAccessible(true);
        field.set(itemToGetObject, newValue);
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
    return true;
}