Example usage for org.springframework.beans MutablePropertyValues setPropertyValueAt

List of usage examples for org.springframework.beans MutablePropertyValues setPropertyValueAt

Introduction

In this page you can find the example usage for org.springframework.beans MutablePropertyValues setPropertyValueAt.

Prototype

public void setPropertyValueAt(PropertyValue pv, int i) 

Source Link

Document

Modify a PropertyValue object held in this object.

Usage

From source file:org.cloudfoundry.identity.uaa.config.YamlBindingTests.java

private BindingResult bind(Object target, String values) {
    YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
    factory.setResources(new ByteArrayResource[] { new ByteArrayResource(values.getBytes()) });
    Map<Object, Object> map = factory.getObject();
    DataBinder binder = new DataBinder(target) {

        @Override//from   w  w w .j a  v a2  s.c o  m
        protected void doBind(MutablePropertyValues mpvs) {
            modifyProperties(mpvs, getTarget());
            super.doBind(mpvs);
        }

        private void modifyProperties(MutablePropertyValues mpvs, Object target) {

            List<PropertyValue> list = mpvs.getPropertyValueList();
            BeanWrapperImpl bw = new BeanWrapperImpl(target);

            for (int i = 0; i < list.size(); i++) {
                PropertyValue pv = list.get(i);

                String name = pv.getName();
                StringBuilder builder = new StringBuilder();

                for (String key : StringUtils.delimitedListToStringArray(name, ".")) {
                    if (builder.length() != 0) {
                        builder.append(".");
                    }
                    builder.append(key);
                    String base = builder.toString();
                    Class<?> type = bw.getPropertyType(base);
                    if (type != null && Map.class.isAssignableFrom(type)) {
                        String suffix = name.substring(base.length());
                        Map<String, Object> nested = new LinkedHashMap<String, Object>();
                        if (bw.getPropertyValue(base) != null) {
                            @SuppressWarnings("unchecked")
                            Map<String, Object> existing = (Map<String, Object>) bw.getPropertyValue(base);
                            nested = existing;
                        } else {
                            bw.setPropertyValue(base, nested);
                        }
                        Map<String, Object> value = nested;
                        String[] tree = StringUtils.delimitedListToStringArray(suffix, ".");
                        for (int j = 1; j < tree.length - 1; j++) {
                            String subtree = tree[j];
                            value.put(subtree, nested);
                            value = nested;
                        }
                        String refName = base + suffix.replaceAll("\\.([a-zA-Z0-9]*)", "[$1]");
                        mpvs.setPropertyValueAt(new PropertyValue(refName, pv.getValue()), i);
                        break;
                    }
                }

            }

        }

    };
    binder.setIgnoreUnknownFields(false);
    LocalValidatorFactoryBean validatorFactoryBean = new LocalValidatorFactoryBean();
    validatorFactoryBean.afterPropertiesSet();
    binder.setValidator(validatorFactoryBean);
    binder.bind(new MutablePropertyValues(map));
    binder.validate();

    return binder.getBindingResult();
}