sp7839 - map autogrow, including auto-grow support for map values

git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@3892 50f2f4bb-b051-0410-bef5-90022cba6387
This commit is contained in:
Keith Donald 2011-01-07 19:22:45 +00:00
parent fb2e3af50c
commit d269c050cc
2 changed files with 18 additions and 11 deletions

View File

@ -603,7 +603,7 @@ public class BeanWrapperImpl extends AbstractPropertyAccessor implements BeanWra
}
private PropertyValue createDefaultPropertyValue(PropertyTokenHolder tokens) {
Class type = getPropertyType(tokens.canonicalName);
Class<?> type = getPropertyTypeDescriptor(tokens.canonicalName).getType();
if (type == null) {
throw new NullValueInNestedPathException(getRootClass(), this.nestedPath + tokens.canonicalName,
"Could not determine property type for auto-growing a default value");
@ -637,6 +637,7 @@ public class BeanWrapperImpl extends AbstractPropertyAccessor implements BeanWra
}
}
catch (Exception ex) {
// TODO Root cause exception context is lost here... should we throw another exception type that preserves context instead?
throw new NullValueInNestedPathException(getRootClass(), this.nestedPath + name,
"Could not instantiate property type [" + type.getName() + "] to auto-grow nested property path: " + ex);
}
@ -936,11 +937,20 @@ public class BeanWrapperImpl extends AbstractPropertyAccessor implements BeanWra
// Set value for last key.
String key = tokens.keys[tokens.keys.length - 1];
if (propValue == null) {
throw new NullValueInNestedPathException(getRootClass(), this.nestedPath + propertyName,
"Cannot access indexed value in property referenced " +
"in indexed property path '" + propertyName + "': returned null");
// null map value case
if (this.autoGrowNestedPaths) {
// TODO: cleanup, this is pretty hacky
int lastKeyIndex = tokens.canonicalName.lastIndexOf('[');
getterTokens.canonicalName = tokens.canonicalName.substring(0, lastKeyIndex);
propValue = setDefaultValue(getterTokens);
}
else {
throw new NullValueInNestedPathException(getRootClass(), this.nestedPath + propertyName,
"Cannot access indexed value in property referenced " +
"in indexed property path '" + propertyName + "': returned null");
}
}
else if (propValue.getClass().isArray()) {
if (propValue.getClass().isArray()) {
PropertyDescriptor pd = getCachedIntrospectionResults().getPropertyDescriptor(actualName);
Class requiredType = propValue.getClass().getComponentType();
int arrayIndex = Integer.parseInt(key);

View File

@ -2,8 +2,6 @@ package org.springframework.web.servlet.mvc.annotation;
import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -101,7 +99,6 @@ public class Spr7839Tests {
}
@Test
@Ignore
public void map() throws Exception {
request.setRequestURI("/nested/map");
request.addParameter("nested.map['apple'].foo", "bar");
@ -185,12 +182,12 @@ public class Spr7839Tests {
private List<NestedBean>[] arrayOfLists;
private Map<String, NestedBean> map = new HashMap<String, NestedBean>();
private Map<String, NestedBean> map;
private Map<String, List<Integer>> mapOfLists = new HashMap<String, List<Integer>>();
private Map<String, List<Integer>> mapOfLists;
public NestedBean() {
mapOfLists.put("apples", new ArrayList<Integer>());
}
public NestedBean(String foo) {