List of usage examples for org.springframework.beans PropertyValue setConvertedValue
public synchronized void setConvertedValue(@Nullable Object value)
From source file:org.codehaus.groovy.grails.web.binding.GrailsDataBinder.java
@SuppressWarnings("unchecked") private void processStructuredProperty(StructuredPropertyEditor structuredEditor, String propertyName, Class<?> type, List<String> valueNames, MutablePropertyValues propertyValues) { List requiredFields = structuredEditor.getRequiredFields(); List<String> fields = new ArrayList<String>(); fields.addAll(requiredFields);//from w w w.j a va 2 s . c o m fields.addAll(structuredEditor.getOptionalFields()); Map<String, String> fieldValues = new HashMap<String, String>(); try { String firstRequiredField = null; for (String field : fields) { String fullName = propertyName + STRUCTURED_PROPERTY_SEPERATOR + field; // don't re-process related properties valueNames.remove(fullName); if (firstRequiredField != null) { continue; } PropertyValue partialStructValue = propertyValues.getPropertyValue(fullName); if (partialStructValue == null) { if (requiredFields.contains(field)) { firstRequiredField = field; } } else { fieldValues.put(field, getStringValue(partialStructValue)); } } // set to null since it either won't be created because of problem, or will be overwritten propertyValues.removePropertyValue(propertyName); if (firstRequiredField != null) { throw new MissingPropertyException( "Required structured property is missing [" + firstRequiredField + "]"); } try { Object value = structuredEditor.assemble(type, fieldValues); for (String field : fields) { PropertyValue partialStructValue = propertyValues .getPropertyValue(propertyName + STRUCTURED_PROPERTY_SEPERATOR + field); if (null != partialStructValue) { partialStructValue.setConvertedValue(getStringValue(partialStructValue)); } } propertyValues.addPropertyValue(new PropertyValue(propertyName, value)); } catch (IllegalArgumentException e) { LOG.warn("Unable to parse structured date from request for date [" + propertyName + "]", e); } } catch (InvalidPropertyException ignored) { // ignored } }
From source file:org.codehaus.groovy.grails.web.binding.GrailsDataBinder.java
private void filterBlankValuesWhenTargetIsNullable(MutablePropertyValues mpvs) { Object target = getTarget();/* ww w. j a v a2 s . c o m*/ Map constrainedProperties = resolveConstrainedProperties(target, domainClass); if (constrainedProperties == null) { return; } PropertyValue[] valueArray = mpvs.getPropertyValues(); for (PropertyValue propertyValue : valueArray) { if (BLANK.equals(propertyValue.getValue())) { ConstrainedProperty cp = getConstrainedPropertyForPropertyValue(constrainedProperties, propertyValue); if (shouldNullifyBlankString(propertyValue, cp)) { propertyValue.setConvertedValue(null); } } } }
From source file:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.java
/** * 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/*from w ww . j a v a 2 s .co m*/ */ protected void applyPropertyValues(String beanName, BeanDefinition mbd, BeanWrapper bw, PropertyValues pvs) { if (pvs.isEmpty()) { return; } if (System.getSecurityManager() != null && bw instanceof BeanWrapperImpl) { ((BeanWrapperImpl) bw).setSecurityContext(getAccessControlContext()); } MutablePropertyValues mpvs = null; List<PropertyValue> original; 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); } // Possibly store converted value in merged bean definition, // 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); } }