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

public static Object fieldGet(Object object, Class<?> clazz, String fieldName) throws Exception {
    Field field = clazz.getDeclaredField(fieldName);
    field.setAccessible(true);/* w  w  w .j  a va2s.c o m*/
    return field.get(object);
}

From source file:Main.java

public static boolean hasField(Class<?> klass, String fieldName) {
    try {/*from   www .j av a 2 s.c o m*/
        klass.getDeclaredField(fieldName);
        return true;
    } catch (NoSuchFieldException e) {
        return false;
    }
}

From source file:Main.java

public static void injectLayoutInflater(LayoutInflater layoutInflater, Object src, Class clz, String name) {
    try {//from   w w  w. ja v a2 s. c o  m
        Field field = clz.getDeclaredField(name);
        field.setAccessible(true);
        field.set(src, layoutInflater);
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }

}

From source file:Main.java

static Field getField(Class clazz, String fieldName) {
    try {/*from ww  w. ja  v  a2s .  com*/
        final Field f = clazz.getDeclaredField(fieldName);
        f.setAccessible(true);
        return f;
    } catch (NoSuchFieldException ignored) {
    }
    return null;
}

From source file:Main.java

public static int getResourceId(String variableName, Context context, Class<?> clazz) {
    try {/*from w w w  .  ja  va  2s .c om*/
        Field field = clazz.getDeclaredField(variableName);
        return field.getInt(field);
    } catch (Exception e) {
        e.printStackTrace();
        return 0;
    }
}

From source file:Main.java

private static Field getSingletonField(Class<?> hostClas) {
    Field f = null;//ww  w  .j ava 2  s  .c o m
    try {
        f = hostClas.getDeclaredField("I");
        if (!Modifier.isStatic(f.getModifiers()))
            throw new Exception("'I' field should be static." + f);
        f.setAccessible(true);
    } catch (Throwable ex) {
    }
    return f;
}

From source file:Main.java

@SuppressWarnings("rawtypes")
public static Field getDeclaredField(Class clazz, String name) {
    try {//from   www .j a va 2s. c o m
        return clazz.getDeclaredField(name);
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static void fieldSet(Object object, Class<?> clazz, String fieldName, Object value) throws Exception {
    Field field = clazz.getDeclaredField(fieldName);
    field.setAccessible(true);/* ww w  .jav  a2  s .co  m*/
    field.set(object, value);
}

From source file:Main.java

public static Field findField(Class src, String fieldName) throws Exception {
    try {/*w  w w .  j  av  a2  s  .  c o m*/
        return src.getDeclaredField(fieldName);
    } catch (Exception e) {
        if (src.getSuperclass() != null) {
            return findField(src.getSuperclass(), fieldName);
        } else {
            throw e;
        }
    }
}

From source file:Main.java

private static final Field getFieldOrDie(Class<?> clazz, String fieldName) {
    try {//from   www  .  j a  v a2 s  .  co m
        Field field = clazz.getDeclaredField(fieldName);
        field.setAccessible(true);
        return field;
    } catch (ReflectiveOperationException e) {
        throw new LinkageError(e.getMessage());
    }
}