Code example for Field

Methods: getAnnotation, getType, set, setAccessible

0
     * @param field Field of object where to put value 
     * @param obj   Object which field will be set 
     * @throws IllegalAccessException 
     */ 
    protected <T> boolean processSimpleValue(ElementUnmarshaler elem, Field field, T obj) throws IllegalAccessException {
        String annotationName = field.getAnnotation(XmlElement.class).name();
        String value = elem.getValue(annotationName);
 
        field.setAccessible(true);
        Class<?> valueType = field.getType();
        if (String.class.equals(valueType)) {
            field.set(obj, value);
            return true; 
        } else if (Integer.class.equals(valueType)) {
            field.set(obj, Integer.valueOf(value));
            return true; 
        } else if (Long.class.equals(valueType)) {
            field.set(obj, Long.valueOf(value));
            return true; 
        } else if (Float.class.equals(valueType)) {
            field.set(obj, Float.valueOf(value));
            return true; 
        } else if (Double.class.equals(valueType)) {
            field.set(obj, Double.valueOf(value));
            return true; 
        } else if (Boolean.class.equals(valueType)) {
            field.set(obj, Boolean.valueOf(value));
            return true; 
        } 
        return false; 
    }