Example usage for org.apache.commons.lang.reflect FieldUtils readField

List of usage examples for org.apache.commons.lang.reflect FieldUtils readField

Introduction

In this page you can find the example usage for org.apache.commons.lang.reflect FieldUtils readField.

Prototype

public static Object readField(Object target, String fieldName) throws IllegalAccessException 

Source Link

Document

Read the named public field.

Usage

From source file:com.neusoft.mid.clwapi.tools.JacksonUtils.java

/**
 * @param obj//from   ww  w . j  a  v  a 2  s.co m
 * @param f
 * @throws IllegalAccessException
 * @throws JacksonEncoderException
 */
private static Object jsonCoder(Object obj, Field f) throws IllegalAccessException, JacksonEncoderException {
    Object o = FieldUtils.readField(f, obj);
    if (o instanceof String) {
        FieldUtils.writeField(f, obj, jsonCoder((String) o, true));
    } else if (o instanceof Collection<?>) {
        logger.debug("Field [" + f.getName() + "] is " + o.getClass());
        Collection<?> c = (Collection<?>) o;
        Iterator<?> it = c.iterator();
        while (it.hasNext()) {
            jsonEncoder(it.next());
        }
    } else if (o instanceof Map<?, ?>) {
        logger.debug("Field [" + f.getName() + "] is " + o.getClass());
        Set<?> entries = ((Map<?, ?>) o).entrySet();
        Iterator<?> it = entries.iterator();
        while (it.hasNext()) {
            Map.Entry<Object, Object> entry = (Map.Entry<Object, Object>) it.next();
            logger.debug("Key is [" + entry.getKey() + "]");
            entry.setValue(jsonEncoder(entry.getValue()));
        }
    } else if (o instanceof Integer || o instanceof Byte || o instanceof Boolean || o instanceof Long
            || o instanceof Double || o instanceof Character || o instanceof Short || o instanceof Float
            || o instanceof Number) {
        return obj;
    } else {
        throw new JacksonEncoderException("??" + f.getClass());
    }
    return obj;
}

From source file:com.haulmont.cuba.core.sys.EntityManagerImpl.java

protected <T extends Entity> T internalMerge(T entity) {
    try {/*from   w w w .  ja v  a  2s  .c o m*/
        CubaUtil.setSoftDeletion(false);
        CubaUtil.setOriginalSoftDeletion(false);

        UUID uuid = null;
        if (entity.getId() instanceof IdProxy) {
            uuid = ((IdProxy) entity.getId()).getUuid();
        }

        T merged = delegate.merge(entity);

        if (entity.getId() instanceof IdProxy && uuid != null
                && !uuid.equals(((IdProxy) merged.getId()).getUuid())) {
            ((IdProxy) merged.getId()).setUuid(uuid);
        }

        // copy non-persistent attributes to the resulting merged instance
        for (MetaProperty property : metadata.getClassNN(entity.getClass()).getProperties()) {
            if (metadata.getTools().isNotPersistent(property) && !property.isReadOnly()) {
                // copy using reflection to avoid executing getter/setter code
                Field field = FieldUtils.getDeclaredField(entity.getClass(), property.getName(), true);
                if (field != null) {
                    try {
                        Object value = FieldUtils.readField(field, entity);
                        if (value != null) {
                            FieldUtils.writeField(field, merged, value);
                        }
                    } catch (IllegalAccessException e) {
                        throw new RuntimeException(
                                "Error copying non-persistent attribute value to merged instance", e);
                    }
                }
            }
        }

        return merged;
    } finally {
        CubaUtil.setSoftDeletion(softDeletion);
        CubaUtil.setOriginalSoftDeletion(softDeletion);
    }
}