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

public static Object getSIGNAL_STRENGTH_NAMES(Object instance) {
    try {/*from   w  w  w. j  av  a 2 s  .c  om*/
        Field f = getField(instance.getClass(), "SIGNAL_STRENGTH_NAMES");
        return f.get(instance);
    } catch (NoSuchFieldException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;

}

From source file:Main.java

private static Object getField(Object paramObject, Class<?> paramClass, String paramString)
        throws NoSuchFieldException, IllegalAccessException, IllegalArgumentException {
    Field localField = paramClass.getDeclaredField(paramString);
    localField.setAccessible(true);/*from  www  .java  2 s.c om*/
    return localField.get(paramObject);
}

From source file:Main.java

public static <T> T getMember(Object object, String member, Class<T> klass)
        throws NoSuchFieldException, IllegalAccessException {
    Field field = getField(object.getClass(), member);
    field.setAccessible(true);//from   ww w  . ja  v a 2  s . c  o m
    return (T) field.get(object);
}

From source file:Main.java

public static int getResourceId(String packageName, String resFileName, String parameterName) {
    if ((packageName != null) && (resFileName != null) && (parameterName != null))
        try {//  ww  w.  j av a2  s .c o  m
            Class localClass = Class.forName(packageName + "$" + resFileName);
            Field localField = localClass.getField(parameterName);
            Object localObject = localField.get(localClass.newInstance());
            return Integer.parseInt(localObject.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    return -1;
}

From source file:Main.java

public static String getFields(Class clazz, Object object) {
    try {//  www  .j  a  v  a2s .  c  om
        StringBuilder builder = new StringBuilder();
        Field[] fs = clazz.getDeclaredFields();
        for (Field f : fs) {
            f.setAccessible(true);
            builder.append(f.getName() + "=" + f.get(object) + "\r\n");
        }
        return builder.toString();
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

public static Object getFieldValue(final Object object, final String name) throws Exception {
    Field field = object.getClass().getDeclaredField(name);
    field.setAccessible(true);//from  w ww . ja  v  a2  s  . co m
    return field.get(object);
}

From source file:Main.java

public static Map<String, String> convertBeanToMap(Object bean)
        throws IllegalArgumentException, IllegalAccessException {
    Field[] fields = bean.getClass().getDeclaredFields();
    HashMap<String, String> data = new HashMap<String, String>();
    for (Field field : fields) {
        field.setAccessible(true);//from w  w  w  .j ava  2 s  .  c  om
        data.put(field.getName(), (String) field.get(bean));
    }
    return data;
}

From source file:Main.java

public static String getId(Object t) {
    try {//from  w  w  w.ja va 2 s .com
        Field idField = t.getClass().getDeclaredField("id");
        idField.setAccessible(true);
        return idField.get(t).toString();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:org.energy_home.jemma.internal.ah.m2m.device.M2MUtils.java

static String getAhConstant(String name) {
    try {//from  www.  j a  va 2s.c  o  m
        Class ahConstantsClass = Class.forName("org.energy_home.jemma.ah.AHConstants");
        Field field = ahConstantsClass.getField(name);
        return (String) field.get(null);
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

/**
 * Get the private field data value of the given instance of a class
 * //from w w w .  j  a v a 2s. co m
 * @param objectInstance Object to get private member data from
 * @param fieldName Name of private member to get data from
 * @return Private member's data
 * 
 * @throws NoSuchFieldException
 * @throws IllegalArgumentException
 * @throws IllegalAccessException
 */
public static Object getPrivateField(Object objectInstance, String fieldName)
        throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException // NOSONAR
{
    Class<? extends Object> clazz = objectInstance.getClass();

    Field field = clazz.getDeclaredField(fieldName);

    field.setAccessible(true);

    return field.get(objectInstance);
}