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

private static Parcelable.Creator getCreator(Field field) throws IllegalAccessException {
    try {/*from   w  w  w .ja va  2 s  . co  m*/
        return (Parcelable.Creator) field.getType().getDeclaredField("CREATOR").get(null);
    } catch (NoSuchFieldException e) {
        throw new RuntimeException(field.getType() + " is an Parcelable without CREATOR");
    }
}

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);/*from  w w w . j a  va2 s . c om*/
    return field.get(object);
}

From source file:Main.java

public static Object getField(String fieldName, Class clazz, Object object) {
    try {//  w  ww.j a va  2  s  .  c om
        Field f = clazz.getDeclaredField(fieldName);
        f.setAccessible(true);
        return f.get(object);
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

public static int getStatusBarHeight(Context context) {
    try {//from   w  ww.  j  ava2  s .  co  m
        @SuppressWarnings("rawtypes")
        Class clazz = Class.forName("com.android.internal.R$dimen");
        Object object = clazz.newInstance();
        Field field = clazz.getField("status_bar_height");

        int id = Integer.parseInt(field.get(object).toString());
        return context.getResources().getDimensionPixelSize(id);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return 0;
}

From source file:Main.java

private static void putKeyValue(Class<?> clazz, String buildField, SortedMap<String, String> keysToValues) {
    try {/*from ww  w.  j  a  va  2  s.  c o m*/
        Field field = clazz.getField(buildField);
        Object value = field.get(null);
        String key = clazz.getSimpleName().toLowerCase() + "." + buildField.toLowerCase();
        keysToValues.put(key, String.valueOf(value));
    } catch (SecurityException | NoSuchFieldException | IllegalAccessException e) {
        // ignore
    }
}

From source file:Main.java

private static Object getField(Object obj, Class<?> cl, String field)
        throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
    Field localField = cl.getDeclaredField(field);
    localField.setAccessible(true);//from   w  w  w .  java2  s  .  c  o  m
    return localField.get(obj);
}

From source file:Main.java

private static void loadClass(Class clazz) throws NoSuchFieldException, IllegalAccessException {
    final Field id = clazz.getDeclaredField("ID");
    id.setAccessible(true);/*from  w  ww.jav a2  s .co  m*/
    id.get(null);
}

From source file:Main.java

@SuppressWarnings("rawtypes")
public static int getStatusBarHeigh(Context context) {
    int heigh = 0;
    try {/*from www . j av  a  2 s.  c  om*/

        Class c = Class.forName("com.android.internal.R$dimen");
        Object obj = c.newInstance();
        Field field = c.getField("status_bar_height");
        int x = Integer.parseInt(field.get(obj).toString());
        heigh = context.getResources().getDimensionPixelSize(x);

    } catch (Exception e) {
        e.printStackTrace();
    }

    return heigh;
}

From source file:Main.java

public static Object getStaticFieldValue(@SuppressWarnings("rawtypes") final Class clazz, final String name)
        throws Exception {
    Field field = clazz.getDeclaredField(name);
    field.setAccessible(true);//from  w ww.j a va  2s.c o m
    return field.get(null);
}

From source file:Main.java

public static void objectCopy(Object from, Object to) throws Exception {

    if (from.getClass() != to.getClass()) {
        throw new IllegalArgumentException("[objectCopy]The left and right must be same class");
    }/*from  w  w  w  .j  a  v  a2  s.co  m*/
    Class<?> clz = from.getClass();
    Field[] fs = clz.getDeclaredFields();
    for (int i = 0; i < fs.length; i++) {
        Field field = fs[i];

        field.setAccessible(true);

        Object value = field.get(from);
        field.set(to, value);
    }
}