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.thinkbiganalytics.metadata.jpa.support.QueryDslPathInspector.java

private static Object getObjectForField(BeanPath basePath, String field) throws IllegalAccessException {
    Map<String, Field> fieldSet = getFields(basePath.getClass());
    if (StringUtils.isNotBlank(field)) {
        Field f = fieldSet.get(field);
        if (f != null) {
            Object o = f.get(basePath);
            return o;
        }/*from   ww  w. ja v  a 2  s.  co m*/
    }
    return null;

}

From source file:Main.java

private static Object getDeclaredField(Object obj, String name)
        throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
    Field f = obj.getClass().getDeclaredField(name);
    f.setAccessible(true);/*from  w  ww .  j  a  va 2 s.  c  om*/
    Object out = f.get(obj);
    //System.out.println(obj.getClass().getName() + "." + name + " = "+ out);
    return out;
}

From source file:com.streametry.json.JsonSerializer.java

public static Object getFieldValue(Field f, Object o) {
    try {//from w  w  w. ja v  a2  s.  c o  m
        f.setAccessible(true);
        return f.get(o);
    } catch (IllegalAccessException e) {
        throw new IllegalStateException(e);
    }
}

From source file:com.aw.support.reflection.AttributeAccessor.java

/**
 * @param target// ww w .  j  ava2 s.c  om
 * @param attrName
 * @return
 */
public static Object get(Object target, String attrName) {
    Object objectToReturn = null;
    try {
        Class cls = target.getClass();
        Field field = getField(cls, attrName);
        objectToReturn = field.get(target);
    } catch (Throwable e) {
        logger.error("Exception getting attribute value:" + attrName, e);
        e.printStackTrace();
    }
    return objectToReturn;
}

From source file:Main.java

public static String getMobileInfo() {
    StringBuffer sb = new StringBuffer();
    try {/*from   w ww . j  ava2 s . c om*/

        Field[] fields = Build.class.getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            String name = field.getName();
            String value = field.get(null).toString();
            sb.append(name + "=" + value);
            sb.append("\n");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return sb.toString();
}

From source file:net.projectmonkey.spring.acl.util.reflect.FieldUtil.java

public static Object getFieldValue(final Object domainObject, final String fieldName) {
    Field field = ReflectionUtils.findField(domainObject.getClass(), fieldName);
    field.setAccessible(true);//from w ww .jav a  2  s. c  om
    try {
        return field.get(domainObject);
    } catch (Exception e) {
        throw new AuthenticationServiceException("Exception retrieving field " + fieldName, e);
    }
}

From source file:Main.java

public static <T> JSONArray beansToJsonArray(T... beans)
        throws IllegalArgumentException, IllegalAccessException, JSONException {
    JSONArray jArr = new JSONArray();
    for (T bean : beans) {
        JSONObject jObj = new JSONObject();
        Field[] fields = bean.getClass().getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);/*from w  w  w  .  j  av a  2s  .c o m*/
            String name = field.getName();
            Object value = field.get(bean);

            jObj.put(name, value);
        }
        jArr.put(jObj);
    }
    return jArr;
}

From source file:Main.java

public static void collectDeviceInfo(Context ctx, HashMap<String, String> infos) {
    try {/*from   w  w w .j  a va  2  s.c o m*/
        PackageManager pm = ctx.getPackageManager();
        PackageInfo pi = pm.getPackageInfo(ctx.getPackageName(), PackageManager.GET_ACTIVITIES);
        if (pi != null) {
            String versionName = pi.versionName == null ? "null" : pi.versionName;
            String versionCode = pi.versionCode + "";
            infos.put("versionName", versionName);
            infos.put("versionCode", versionCode);
        }
    } catch (NameNotFoundException e) {
        e.getMessage();
    }
    Field[] fields = Build.class.getDeclaredFields();
    for (Field field : fields) {
        try {
            field.setAccessible(true);
            infos.put(field.getName(), field.get(null).toString());
        } catch (Exception e) {
            e.getMessage();
        }
    }
}

From source file:SampleGet.java

static void printHeight(Rectangle r) {
    Field heightField;
    Integer heightValue;// w  w  w  . ja va2s .c  o m
    Class c = r.getClass();
    try {
        heightField = c.getField("height");
        heightValue = (Integer) heightField.get(r);
        System.out.println("Height: " + heightValue.toString());
    } catch (NoSuchFieldException e) {
        System.out.println(e);
    } catch (SecurityException e) {
        System.out.println(e);
    } catch (IllegalAccessException e) {
        System.out.println(e);
    }
}

From source file:com.skelril.skree.content.modifier.ModExtendCommand.java

public static CommandSpec aquireSpec() {
    Map<String, String> choices = new HashMap<>();
    for (Field field : Modifiers.class.getFields()) {
        try {/*from ww w.  j a v a2  s .  c  o m*/
            Object result = field.get(null);
            if (result instanceof String) {
                choices.put((String) result, (String) result);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    return CommandSpec.builder().description(Text.of("Extend modifiers")).permission("skree.modifier")
            .arguments(
                    seq(onlyOne(choices(Text.of("modifier"), choices)), onlyOne(integer(Text.of("minutes")))))
            .executor(new ModExtendCommand()).build();
}