Example usage for org.springframework.beans DirectFieldAccessor getPropertyType

List of usage examples for org.springframework.beans DirectFieldAccessor getPropertyType

Introduction

In this page you can find the example usage for org.springframework.beans DirectFieldAccessor getPropertyType.

Prototype

@Override
    @Nullable
    public Class<?> getPropertyType(String propertyName) throws BeansException 

Source Link

Usage

From source file:org.openlegacy.utils.XmlSerializationUtil.java

/**
 * This method purpose is to reduce the amount of XML written when serializing an object It reset member to null when the
 * default value matches the object value
 * //  ww w .j av  a2  s.co m
 * @param source
 */
private static void resetDefaultValues(Object source) {
    DirectFieldAccessor fieldAccessor = new DirectFieldAccessor(source);
    PropertyDescriptor[] propertyDescriptors = PropertyUtils.getPropertyDescriptors(source);
    for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
        String propertyName = propertyDescriptor.getName();
        Class<?> propertyType = fieldAccessor.getPropertyType(propertyName);
        if (propertyType == null || Collection.class.isAssignableFrom(propertyType)
                || Map.class.isAssignableFrom(propertyType)) {
            continue;
        }
        Object defaultValue = PropertyUtil.getPropertyDefaultValue(source.getClass(), propertyName);
        Object value = fieldAccessor.getPropertyValue(propertyName);
        if (fieldAccessor.isWritableProperty(propertyName) && ObjectUtils.equals(value, defaultValue)
                && !propertyType.isPrimitive()) {
            fieldAccessor.setPropertyValue(propertyName, null);
        }

    }
}