提交 0195b0da 编写于 作者: J Juergen Hoeller

BeanWrapper does not attempt to populate Map values on access (just auto-grows Map itself)

上级 27a10c74
......@@ -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;
......
......@@ -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);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册