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:com.liferay.tool.datamanipulator.util.GetterUtil.java

public static Object getFieldValue(String className, String fieldName) throws ClassNotFoundException,
        SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {

    Class<?> clazz = getClass(className);

    Field field = clazz.getField(fieldName);

    return field.get(null);
}

From source file:Main.java

public static int getStatusBarHeight(Activity activity) {
    try {//w  w w. j  a  v  a2s  .co m
        Class<?> clazz = Class.forName("com.android.internal.R$dimen");
        Object object = clazz.newInstance();
        Field field = clazz.getField("status_bar_height");
        int dpHeight = Integer.parseInt(field.get(object).toString());
        return activity.getResources().getDimensionPixelSize(dpHeight);
    } catch (Exception e1) {
        e1.printStackTrace();
        return 0;
    }
}

From source file:Main.java

/**
 * Returns status bar height;/*from  w w  w. j  av a2  s.  c om*/
 * 
 * @param context
 * @return
 */
public static int getStatusBarHeight(Context context) {
    int height = 0;
    try {
        Class<?> c = Class.forName("com.android.internal.R$dimen");
        Object obj = c.newInstance();
        Field field = c.getField("status_bar_height");
        int temp = Integer.parseInt(field.get(obj).toString());
        height = context.getResources().getDimensionPixelSize(temp);
    } catch (Exception e) {
        e.printStackTrace();
    }

    return height;
}

From source file:Main.java

public static Object getFieldValue(Class<?> fieldClass, String fieldName, Object instance) {
    try {/* w  ww  .  ja  va 2s.c  o m*/
        Field e = fieldClass.getDeclaredField(fieldName);
        e.setAccessible(true);
        return e.get(instance);
    } catch (Exception var4) {
        var4.printStackTrace();
        return null;
    }
}

From source file:Main.java

static int getApiLevel() {
    if (_version < 0) {
        try {//  ww w .j a v a  2 s. c o m
            Field SDK_INT_field = Build.VERSION.class.getField("SDK_INT");
            _version = (Integer) SDK_INT_field.get(null);
        } catch (Exception ignored) {
            _version = 3;
        }
    }
    return _version;
}

From source file:Main.java

protected static Object getDeclaredField(Object obj, String name)
        throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
    final Field f = obj.getClass().getDeclaredField(name);
    f.setAccessible(true);/*  w ww  .  j  a  va  2s. c  o  m*/
    return f.get(obj);
}

From source file:Main.java

/**
 * Called internally by installGtkPopupBugWorkaround. Returns a specific
 * GTK style object./*from   w ww .  ja  va2  s .c  o m*/
 * 
 * @param styleFactory
 *            The GTK style factory.
 * @param component
 *            The target component of the style.
 * @param regionName
 *            The name of the target region of the style.
 * @return The GTK style.
 * @throws Exception
 *             When reflection fails.
 */
private static Object getGtkStyle(Object styleFactory, JComponent component, String regionName)
        throws Exception {
    // Create the region object
    Class<?> regionClass = Class.forName("javax.swing.plaf.synth.Region");
    Field field = regionClass.getField(regionName);
    Object region = field.get(regionClass);

    // Get and return the style
    Class<?> styleFactoryClass = styleFactory.getClass();
    Method method = styleFactoryClass.getMethod("getStyle", new Class<?>[] { JComponent.class, regionClass });
    boolean accessible = method.isAccessible();
    method.setAccessible(true);
    Object style = method.invoke(styleFactory, component, region);
    method.setAccessible(accessible);
    return style;
}

From source file:Main.java

public static <T> T reflectField(Object src, String fieldName, Class<T> clazz) throws Exception {
    Field field = findField(src, fieldName);
    field.setAccessible(true);// w  ww  .j a  v a2 s  . c  o  m
    return (T) field.get(src);
}

From source file:Main.java

private static void putKeyValue(Class<?> clazz, String buildField, SortedMap<String, String> keysToValues) {
    try {//from ww  w  .jav  a 2s.co  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 e) {
        // ignore
    } catch (NoSuchFieldException e) {
        // ignore
    } catch (IllegalAccessException e) {
        // ignore
    }
}

From source file:Main.java

public static Object fieldGet(Object object, String fieldName) throws Exception {
    Field field = object.getClass().getDeclaredField(fieldName);
    field.setAccessible(true);/*from  w  w w.  jav a 2 s.c  o  m*/
    return field.get(object);
}