diff --git a/org.springframework.beans/src/main/java/org/springframework/beans/BeanWrapperImpl.java b/org.springframework.beans/src/main/java/org/springframework/beans/BeanWrapperImpl.java index 5ea46393b3c791fcaa0dec504dce64fccc38ec3a..b42d41205db32aff180f51a1bca2ca59f80638cf 100644 --- a/org.springframework.beans/src/main/java/org/springframework/beans/BeanWrapperImpl.java +++ b/org.springframework.beans/src/main/java/org/springframework/beans/BeanWrapperImpl.java @@ -586,18 +586,22 @@ public class BeanWrapperImpl extends AbstractPropertyAccessor implements BeanWra PropertyTokenHolder tokens = new PropertyTokenHolder(); tokens.actualName = propertyName; tokens.canonicalName = propertyName; - setPropertyValue(tokens, createDefaultPropertyValue(tokens)); - return getPropertyValue(tokens); + return setDefaultValue(tokens); } private Object setDefaultValue(PropertyTokenHolder tokens) { - setPropertyValue(tokens, createDefaultPropertyValue(tokens)); - return getPropertyValue(tokens); + PropertyValue pv = createDefaultPropertyValue(tokens); + setPropertyValue(tokens, pv); + return pv.getValue(); } private PropertyValue createDefaultPropertyValue(PropertyTokenHolder tokens) { - PropertyDescriptor pd = getCachedIntrospectionResults().getPropertyDescriptor(tokens.actualName); - Object defaultValue = newValue(pd.getPropertyType(), tokens.canonicalName); + Class type = getPropertyType(tokens.canonicalName); + if (type == null) { + throw new NullValueInNestedPathException(getRootClass(), this.nestedPath + tokens.canonicalName, + "Could not determine property type for auto-growing a default value"); + } + Object defaultValue = newValue(type, tokens.canonicalName); return new PropertyValue(tokens.canonicalName, defaultValue); } @@ -791,9 +795,6 @@ public class BeanWrapperImpl extends AbstractPropertyAccessor implements BeanWra // must not kick in for map keys but rather only for map values. Object convertedMapKey = convertIfNecessary(null, null, key, mapKeyType, new PropertyTypeDescriptor(pd, new MethodParameter(pd.getReadMethod(), -1), mapKeyType)); - // Pass full property name and old value in here, since we want full - // conversion ability for map values. - growMapIfNecessary(map, convertedMapKey, indexedPropertyName, pd, i + 1); value = map.get(convertedMapKey); } else { @@ -865,21 +866,6 @@ public class BeanWrapperImpl extends AbstractPropertyAccessor implements BeanWra } } - @SuppressWarnings("unchecked") - private void growMapIfNecessary( - Map map, Object key, String name, PropertyDescriptor pd, int nestingLevel) { - - if (!this.autoGrowNestedPaths) { - return; - } - if (!map.containsKey(key)) { - Class valueType = GenericCollectionTypeResolver.getMapValueReturnType(pd.getReadMethod(), nestingLevel); - if (valueType != null) { - map.put(key, newValue(valueType, name)); - } - } - } - @Override public void setPropertyValue(String propertyName, Object value) throws BeansException { BeanWrapperImpl nestedBw; diff --git a/org.springframework.beans/src/test/java/org/springframework/beans/BeanWrapperAutoGrowingTests.java b/org.springframework.beans/src/test/java/org/springframework/beans/BeanWrapperAutoGrowingTests.java index 2e554a41ebcdd4dd50f5deb5a79976b5d9be306f..691b89c78d31132842347d2ace65600192841179 100644 --- a/org.springframework.beans/src/test/java/org/springframework/beans/BeanWrapperAutoGrowingTests.java +++ b/org.springframework.beans/src/test/java/org/springframework/beans/BeanWrapperAutoGrowingTests.java @@ -129,17 +129,10 @@ public class BeanWrapperAutoGrowingTests { wrapper.getPropertyValue("listNotParameterized[0]"); } - @Test - public void getPropertyValueAutoGrowMap() { - assertNotNull(wrapper.getPropertyValue("map[A]")); - assertEquals(1, bean.getMap().size()); - assertTrue(bean.getMap().get("A") instanceof Bean); - } - @Test public void setPropertyValueAutoGrowMap() { - wrapper.setPropertyValue("map[A].prop", "test"); - assertEquals("test", bean.getMap().get("A").getProp()); + wrapper.setPropertyValue("map[A]", new Bean()); + assertTrue(bean.getMap().get("A") instanceof Bean); }