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 int getResId(String name, Class<?> c) {
    try {//from ww w .j  av a2s .c om
        Field idField = c.getDeclaredField(name);
        return idField.getInt(idField);
    } catch (Exception e) {
        e.printStackTrace();
        return -1;
    }
}

From source file:Main.java

public static int getResId(String variableName, Class<?> c) {

    try {/*  w  ww. jav a 2s  .  c  om*/
        Field idField = c.getDeclaredField(variableName);
        return idField.getInt(idField);
    } catch (Exception e) {
        e.printStackTrace();
        return -1;
    }
}

From source file:Main.java

public static int getResId(String resName, Class<?> c) {

    try {//from w  w  w.j  av a  2s .c  o  m
        Field idField = c.getDeclaredField(resName);
        return idField.getInt(idField);
    } catch (Exception e) {
        e.printStackTrace();
        return -1;
    }

}

From source file:Main.java

public static Field getDeclaredField(Class clazz, String name) {
    try {//w ww. j  a  v  a 2 s .c  om
        return clazz.getDeclaredField(name);
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static Object getField(String fieldName, Class clazz, Object object) {
    try {/*w  w  w. j  a  va2s .co m*/
        Field f = clazz.getDeclaredField(fieldName);
        f.setAccessible(true);
        return f.get(object);
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

/**
 * Gets resource ID of the resource//w  w w  . j a v a2  s .c  o  m
 * @param resourceName  resource name
 * @param clazz an instance of Class, e.g. R.drawable.class
 * @return  an resource ID
 */
public static int getId(String resourceName, Class<?> clazz) {
    try {
        Field idField = clazz.getDeclaredField(resourceName);
        return idField.getInt(idField);
    } catch (Exception e) {
        throw new RuntimeException("No resource ID found for: " + resourceName + " / " + clazz, e);
    }
}

From source file:Main.java

public static Field getDeclaredField(Class<?> clazz, String name) {
    try {/*from   w  w  w. ja v a2s.  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 Object getStaticFieldValue(Class<?> clazz, String fieldName) throws Exception {
    Field field = clazz.getDeclaredField(fieldName);
    field.setAccessible(true);//from  w ww  .j a va  2s  . co  m
    return field.get(fieldName);
}

From source file:Main.java

public static Field getDeclaredField(Class clazz, String name) {
    try {//  w w  w .jav a  2 s .  c  o m
        Field field = clazz.getDeclaredField(name);
        return field;
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    }
    return null;

}

From source file:Main.java

public static Field fieldGetOrg(Object object, Class<?> clazz, String name) throws Exception {
    Field field = clazz.getDeclaredField(name);
    field.setAccessible(true);/*from w  w w  .ja  va 2 s.  c om*/
    return field;
}