Example usage for org.springframework.beans BeanWrapperImpl convertForProperty

List of usage examples for org.springframework.beans BeanWrapperImpl convertForProperty

Introduction

In this page you can find the example usage for org.springframework.beans BeanWrapperImpl convertForProperty.

Prototype

@Nullable
public Object convertForProperty(@Nullable Object value, String propertyName) throws TypeMismatchException 

Source Link

Document

Convert the given value for the specified property to the latter's type.

Usage

From source file:org.archive.spring.Sheet.java

/**
 * Ensure any properties targetted by this Sheet know to 
 * check the right property paths for overrides at lookup time,
 * and that the override values are compatible types for their 
 * destination properties. /*from ww  w  .j a  v  a 2  s.  c  o  m*/
 * 
 * Should be done as soon as all possible targets are 
 * constructed (ApplicationListener ContextRefreshedEvent)
 * 
 * TODO: consider if  an 'un-priming' also needs to occur to 
 * prevent confusing side-effects. 
 * TODO: consider if priming should move to another class
 */
public void prime() {
    for (String fullpath : map.keySet()) {
        int lastDot = fullpath.lastIndexOf(".");
        String beanPath = fullpath.substring(0, lastDot);
        String terminalProp = fullpath.substring(lastDot + 1);
        Object value = map.get(fullpath);
        int i = beanPath.indexOf(".");
        Object bean;
        HasKeyedProperties hkp;
        if (i < 0) {
            bean = beanFactory.getBean(beanPath);
        } else {
            String beanName = beanPath.substring(0, i);
            String propPath = beanPath.substring(i + 1);
            BeanWrapperImpl wrapper = new BeanWrapperImpl(beanFactory.getBean(beanName));
            bean = wrapper.getPropertyValue(propPath);
        }
        try {
            hkp = (HasKeyedProperties) bean;
        } catch (ClassCastException cce) {
            // targetted bean has no overridable properties
            throw new TypeMismatchException(bean, HasKeyedProperties.class, cce);
        }
        // install knowledge of this path 
        hkp.getKeyedProperties().addExternalPath(beanPath);
        // verify type-compatibility
        BeanWrapperImpl wrapper = new BeanWrapperImpl(hkp);
        Class<?> requiredType = wrapper.getPropertyType(terminalProp);
        try {
            // convert for destination type
            map.put(fullpath, wrapper.convertForProperty(value, terminalProp));
        } catch (TypeMismatchException tme) {
            TypeMismatchException tme2 = new TypeMismatchException(
                    new PropertyChangeEvent(hkp, fullpath, wrapper.getPropertyValue(terminalProp), value),
                    requiredType, tme);
            throw tme2;
        }
    }
}