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

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

Introduction

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

Prototype

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

Source Link

Document

Reads the named public Field .

Usage

From source file:com.astamuse.asta4d.data.InjectUtil.java

/**
 * Set the value of all the fields marked by {@link ContextData} of the given instance.
 * // ww  w.j  a  va2 s  . c o m
 * @param instance
 * @throws DataOperationException
 */
public final static void injectToInstance(Object instance) throws DataOperationException {

    InstanceWireTarget target = getInstanceTarget(instance);

    for (FieldInfo fi : target.setFieldList) {
        try {
            ContextDataHolder valueHolder = null;
            if (fi.isContextDataHolder) {
                valueHolder = (ContextDataHolder) FieldUtils.readField(fi.field, instance);
            }
            if (valueHolder == null) {
                valueHolder = fi.createDataHolderInstance();
            }
            Class searchType = valueHolder.getTypeCls();
            if (searchType == null) {
                throw new DataOperationException(fi.field.getName()
                        + " should be initialized at first or we can not retrieve the type you want since it is a type of CotnextDataHolder. "
                        + "You can also define an extended class to return the type class, in this case, you do not need to initialized it by your self");
            }
            ContextDataHolder foundData = findValueForTarget(fi, searchType);

            handleTypeUnMatch(instance, fi, foundData);

            if (fi.isContextDataHolder) {
                transferDataHolder(foundData, valueHolder);
                FieldUtils.writeField(fi.field, instance, valueHolder, true);
            } else {
                FieldUtils.writeField(fi.field, instance, foundData.getValue(), true);
            }
        } catch (IllegalAccessException | IllegalArgumentException | InstantiationException e) {
            throw new DataOperationException("Exception when inject value to " + fi.field.toString(), e);
        }

    }

    for (MethodInfo mi : target.setMethodList) {
        try {
            ContextDataHolder valueHolder = mi.createDataHolderInstance();
            Class searchType = valueHolder.getTypeCls();
            if (searchType == null) {
                throw new DataOperationException(mi.method.getName() + " cannot initialize an instance of "
                        + valueHolder.getClass().getName()
                        + ". You should define an extended class to return the type class");
            }
            ContextDataHolder foundData = findValueForTarget(mi, searchType);
            handleTypeUnMatch(instance, mi, foundData);
            if (mi.isContextDataHolder) {
                transferDataHolder(foundData, valueHolder);
                mi.method.invoke(instance, valueHolder);
            } else {
                mi.method.invoke(instance, foundData.getValue());
            }
        } catch (IllegalAccessException | IllegalArgumentException | InstantiationException
                | InvocationTargetException e) {
            throw new DataOperationException("Exception when inject value to " + mi.method.toString(), e);
        }
    }

}

From source file:com.validation.manager.test.AbstractVMTestCase.java

public boolean checkHistory(Versionable v) {
    History current = v.getHistoryList().get(v.getHistoryList().size() - 1);
    List<Field> af = getAuditableFields(v);
    assertEquals(af.size(), current.getHistoryFieldList().size());
    assertTrue(af.size() > 0);/* ww  w. j  a v  a  2 s . c o  m*/
    for (HistoryField hf : current.getHistoryFieldList()) {
        try {
            //Compare audit field vs. the record in history.
            Object o = FieldUtils.readField(FieldUtils.getField(v.getClass(), hf.getFieldName(), true), v);
            if (!Versionable.fieldMatchHistory(hf, o)) {
                return false;
            }
        } catch (SecurityException | IllegalArgumentException | IllegalAccessException ex) {
            LOG.log(Level.SEVERE, null, ex);
        }
    }
    v.getHistoryList().forEach(h -> {
        LOG.info(h.toString());
    });
    return true;
}

From source file:com.validation.manager.core.history.Versionable.java

public static synchronized boolean auditable(Versionable v) {
    History current;//from  www  .j a  va  2s. c  o  m
    boolean result = false;
    if (v.getHistoryList() != null && !v.getHistoryList().isEmpty()) {
        current = v.getHistoryList().get(v.getHistoryList().size() - 1);
        for (HistoryField hf : current.getHistoryFieldList()) {
            try {
                //Compare audit field vs. the record in history.
                Object o = FieldUtils.readField(FieldUtils.getField(v.getClass(), hf.getFieldName(), true), v);
                if ((o == null && !hf.getFieldValue().equals("null"))
                        || (!(o instanceof byte[]) && o != null && !o.toString().equals(hf.getFieldValue()))
                        || ((o instanceof byte[]) && !new String((byte[]) o, StandardCharsets.UTF_8)
                                .equals(hf.getFieldValue()))) {
                    result = true;
                    break;
                }
            } catch (SecurityException | IllegalArgumentException | IllegalAccessException ex) {
                LOG.log(Level.SEVERE, null, ex);
            }
        }
        //As last check the version fields for changes (i.e. baselineing, etc.)
        if (!result) {
            result = current.getMajorVersion() < v.getMajorVersion()
                    || current.getMidVersion() < v.getMidVersion()
                    || current.getMinorVersion() < v.getMinorVersion();
        }
        return result;
    }
    //No history so it is auditable if it has marked fields for audit.
    return !FieldUtils.getFieldsListWithAnnotation(v.getClass(), Auditable.class).isEmpty();
}

From source file:com.thinkbiganalytics.feedmgr.rest.controller.FeedRestController.java

private void updateFeedMetadata(FeedMetadata targetFeedMetadata, FeedMetadata modifiedFeedMetadata,
        FeedPropertySection feedPropertySection) {

    AnnotationFieldNameResolver annotationFieldNameResolver = new AnnotationFieldNameResolver(
            FeedPropertyType.class);
    List<AnnotatedFieldProperty> list = annotationFieldNameResolver.getProperties(FeedMetadata.class);
    List<AnnotatedFieldProperty> sectionList = list.stream()
            .filter(annotatedFieldProperty -> feedPropertySection
                    .equals(((FeedPropertyType) annotatedFieldProperty.getAnnotation()).section()))
            .collect(Collectors.toList());
    sectionList.forEach(annotatedFieldProperty -> {
        try {/*www .  ja v a2  s .c o m*/
            Object value = FieldUtils.readField(annotatedFieldProperty.getField(), modifiedFeedMetadata);
            FieldUtils.writeField(annotatedFieldProperty.getField(), targetFeedMetadata, value);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    });

}

From source file:org.apache.kylin.query.QueryConnectionTest.java

@SuppressWarnings("unchecked")
private static Map<InternalProperty, Object> dirtyReadProperties(Connection connection)
        throws IllegalAccessException {
    assertTrue(connection instanceof CalciteConnection);
    return (Map<InternalProperty, Object>) FieldUtils.readField(connection, "properties");

}