BeanWrapper does not attempt to populate Map values on access (just auto-grows Map itself)
git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@3756 50f2f4bb-b051-0410-bef5-90022cba6387
This commit is contained in:
parent
f797ff683b
commit
c025d123c9
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue