Search in sources :

Example 11 with TypeConverter

use of org.springframework.beans.TypeConverter in project spring-framework by spring-projects.

the class AbstractAutowireCapableBeanFactory method applyPropertyValues.

/**
	 * Apply the given property values, resolving any runtime references
	 * to other beans in this bean factory. Must use deep copy, so we
	 * don't permanently modify this property.
	 * @param beanName the bean name passed for better exception information
	 * @param mbd the merged bean definition
	 * @param bw the BeanWrapper wrapping the target object
	 * @param pvs the new property values
	 */
protected void applyPropertyValues(String beanName, BeanDefinition mbd, BeanWrapper bw, PropertyValues pvs) {
    if (pvs == null || pvs.isEmpty()) {
        return;
    }
    MutablePropertyValues mpvs = null;
    List<PropertyValue> original;
    if (System.getSecurityManager() != null) {
        if (bw instanceof BeanWrapperImpl) {
            ((BeanWrapperImpl) bw).setSecurityContext(getAccessControlContext());
        }
    }
    if (pvs instanceof MutablePropertyValues) {
        mpvs = (MutablePropertyValues) pvs;
        if (mpvs.isConverted()) {
            // Shortcut: use the pre-converted values as-is.
            try {
                bw.setPropertyValues(mpvs);
                return;
            } catch (BeansException ex) {
                throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Error setting property values", ex);
            }
        }
        original = mpvs.getPropertyValueList();
    } else {
        original = Arrays.asList(pvs.getPropertyValues());
    }
    TypeConverter converter = getCustomTypeConverter();
    if (converter == null) {
        converter = bw;
    }
    BeanDefinitionValueResolver valueResolver = new BeanDefinitionValueResolver(this, beanName, mbd, converter);
    // Create a deep copy, resolving any references for values.
    List<PropertyValue> deepCopy = new ArrayList<>(original.size());
    boolean resolveNecessary = false;
    for (PropertyValue pv : original) {
        if (pv.isConverted()) {
            deepCopy.add(pv);
        } else {
            String propertyName = pv.getName();
            Object originalValue = pv.getValue();
            Object resolvedValue = valueResolver.resolveValueIfNecessary(pv, originalValue);
            Object convertedValue = resolvedValue;
            boolean convertible = bw.isWritableProperty(propertyName) && !PropertyAccessorUtils.isNestedOrIndexedProperty(propertyName);
            if (convertible) {
                convertedValue = convertForProperty(resolvedValue, propertyName, bw, converter);
            }
            // in order to avoid re-conversion for every created bean instance.
            if (resolvedValue == originalValue) {
                if (convertible) {
                    pv.setConvertedValue(convertedValue);
                }
                deepCopy.add(pv);
            } else if (convertible && originalValue instanceof TypedStringValue && !((TypedStringValue) originalValue).isDynamic() && !(convertedValue instanceof Collection || ObjectUtils.isArray(convertedValue))) {
                pv.setConvertedValue(convertedValue);
                deepCopy.add(pv);
            } else {
                resolveNecessary = true;
                deepCopy.add(new PropertyValue(pv, convertedValue));
            }
        }
    }
    if (mpvs != null && !resolveNecessary) {
        mpvs.setConverted();
    }
    // Set our (possibly massaged) deep copy.
    try {
        bw.setPropertyValues(new MutablePropertyValues(deepCopy));
    } catch (BeansException ex) {
        throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Error setting property values", ex);
    }
}
Also used : BeanCreationException(org.springframework.beans.factory.BeanCreationException) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) ArrayList(java.util.ArrayList) PropertyValue(org.springframework.beans.PropertyValue) TypeConverter(org.springframework.beans.TypeConverter) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) Collection(java.util.Collection) TypedStringValue(org.springframework.beans.factory.config.TypedStringValue) BeansException(org.springframework.beans.BeansException)

Example 12 with TypeConverter

use of org.springframework.beans.TypeConverter in project spring-framework by spring-projects.

the class AbstractBeanFactory method getTypeConverter.

@Override
public TypeConverter getTypeConverter() {
    TypeConverter customConverter = getCustomTypeConverter();
    if (customConverter != null) {
        return customConverter;
    } else {
        // Build default TypeConverter, registering custom editors.
        SimpleTypeConverter typeConverter = new SimpleTypeConverter();
        typeConverter.setConversionService(getConversionService());
        registerCustomEditors(typeConverter);
        return typeConverter;
    }
}
Also used : TypeConverter(org.springframework.beans.TypeConverter) SimpleTypeConverter(org.springframework.beans.SimpleTypeConverter) SimpleTypeConverter(org.springframework.beans.SimpleTypeConverter)

Example 13 with TypeConverter

use of org.springframework.beans.TypeConverter in project spring-framework by spring-projects.

the class ListFactoryBean method createInstance.

@Override
@SuppressWarnings("unchecked")
protected List<Object> createInstance() {
    if (this.sourceList == null) {
        throw new IllegalArgumentException("'sourceList' is required");
    }
    List<Object> result = null;
    if (this.targetListClass != null) {
        result = BeanUtils.instantiateClass(this.targetListClass);
    } else {
        result = new ArrayList<>(this.sourceList.size());
    }
    Class<?> valueType = null;
    if (this.targetListClass != null) {
        valueType = ResolvableType.forClass(this.targetListClass).asCollection().resolveGeneric();
    }
    if (valueType != null) {
        TypeConverter converter = getBeanTypeConverter();
        for (Object elem : this.sourceList) {
            result.add(converter.convertIfNecessary(elem, valueType));
        }
    } else {
        result.addAll(this.sourceList);
    }
    return result;
}
Also used : TypeConverter(org.springframework.beans.TypeConverter)

Aggregations

TypeConverter (org.springframework.beans.TypeConverter)13 Map (java.util.Map)4 InjectionPoint (org.springframework.beans.factory.InjectionPoint)4 LinkedHashMap (java.util.LinkedHashMap)3 BeansException (org.springframework.beans.BeansException)3 SimpleTypeConverter (org.springframework.beans.SimpleTypeConverter)3 TypeMismatchException (org.springframework.beans.TypeMismatchException)3 UnsatisfiedDependencyException (org.springframework.beans.factory.UnsatisfiedDependencyException)3 MethodParameter (org.springframework.core.MethodParameter)3 ArrayList (java.util.ArrayList)2 IdentityHashMap (java.util.IdentityHashMap)2 LinkedHashSet (java.util.LinkedHashSet)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 BeanCreationException (org.springframework.beans.factory.BeanCreationException)2 ConstructorArgumentValues (org.springframework.beans.factory.config.ConstructorArgumentValues)2 ValueHolder (org.springframework.beans.factory.config.ConstructorArgumentValues.ValueHolder)2 ResolvableType (org.springframework.core.ResolvableType)2 PropertyDescriptor (java.beans.PropertyDescriptor)1 Constructor (java.lang.reflect.Constructor)1 Method (java.lang.reflect.Method)1