List of usage examples for org.springframework.beans BeanWrapper setPropertyValues
void setPropertyValues(Map<?, ?> map) throws BeansException;
From source file:org.springframework.beans.BeanWrapperTests.java
@Test public void testMapAccessWithTypeConversion() { IndexedTestBean bean = new IndexedTestBean(); BeanWrapper bw = new BeanWrapperImpl(bean); bw.registerCustomEditor(TestBean.class, new PropertyEditorSupport() { @Override/* w ww .j av a 2 s.c o m*/ public void setAsText(String text) throws IllegalArgumentException { if (!StringUtils.hasLength(text)) { throw new IllegalArgumentException(); } setValue(new TestBean(text)); } }); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("map[key1]", "rod"); pvs.add("map[key2]", "rob"); bw.setPropertyValues(pvs); assertEquals("rod", ((TestBean) bean.getMap().get("key1")).getName()); assertEquals("rob", ((TestBean) bean.getMap().get("key2")).getName()); pvs = new MutablePropertyValues(); pvs.add("map[key1]", "rod"); pvs.add("map[key2]", ""); try { bw.setPropertyValues(pvs); fail("Should have thrown TypeMismatchException"); } catch (PropertyBatchUpdateException ex) { PropertyAccessException pae = ex.getPropertyAccessException("map[key2]"); assertTrue(pae instanceof TypeMismatchException); } }
From source file:org.springframework.beans.BeanWrapperTests.java
@Test public void testMapAccessWithUnmodifiableMap() { IndexedTestBean bean = new IndexedTestBean(); BeanWrapper bw = new BeanWrapperImpl(bean); bw.registerCustomEditor(TestBean.class, "map", new PropertyEditorSupport() { @Override// www . j a v a 2 s. c o m public void setAsText(String text) throws IllegalArgumentException { if (!StringUtils.hasLength(text)) { throw new IllegalArgumentException(); } setValue(new TestBean(text)); } }); Map<Integer, String> inputMap = new HashMap<Integer, String>(); inputMap.put(new Integer(1), "rod"); inputMap.put(new Integer(2), "rob"); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("map", Collections.unmodifiableMap(inputMap)); bw.setPropertyValues(pvs); assertEquals("rod", ((TestBean) bean.getMap().get(new Integer(1))).getName()); assertEquals("rob", ((TestBean) bean.getMap().get(new Integer(2))).getName()); }
From source file:org.springframework.beans.BeanWrapperTests.java
@Test public void testMapAccessWithCustomUnmodifiableMap() { IndexedTestBean bean = new IndexedTestBean(); BeanWrapper bw = new BeanWrapperImpl(bean); bw.registerCustomEditor(TestBean.class, "map", new PropertyEditorSupport() { @Override//from w ww. java2 s .c o m public void setAsText(String text) throws IllegalArgumentException { if (!StringUtils.hasLength(text)) { throw new IllegalArgumentException(); } setValue(new TestBean(text)); } }); Map<Object, Object> inputMap = new HashMap<Object, Object>(); inputMap.put(new Integer(1), "rod"); inputMap.put(new Integer(2), "rob"); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("map", new ReadOnlyMap(inputMap)); bw.setPropertyValues(pvs); assertEquals("rod", ((TestBean) bean.getMap().get(new Integer(1))).getName()); assertEquals("rob", ((TestBean) bean.getMap().get(new Integer(2))).getName()); }
From source file:org.springframework.beans.BeanWrapperTests.java
@SuppressWarnings("unchecked") // must work with raw map in this test @Test// ww w. ja v a2 s. c o m public void testRawMapAccessWithNoEditorRegistered() { IndexedTestBean bean = new IndexedTestBean(); BeanWrapper bw = new BeanWrapperImpl(bean); Map inputMap = new HashMap(); inputMap.put(new Integer(1), "rod"); inputMap.put(new Integer(2), "rob"); ReadOnlyMap readOnlyMap = new ReadOnlyMap(inputMap); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("map", readOnlyMap); bw.setPropertyValues(pvs); assertSame(readOnlyMap, bean.getMap()); assertFalse(readOnlyMap.isAccessed()); }
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 va 2s .c o 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); } }