Browse Source

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

pull/7/head
Keith Donald 15 years ago
parent
commit
414fcab899
  1. 20
      org.springframework.beans/src/main/java/org/springframework/beans/BeanWrapperImpl.java
  2. 9
      org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/annotation/Spr7839Tests.java

20
org.springframework.beans/src/main/java/org/springframework/beans/BeanWrapperImpl.java

@ -603,7 +603,7 @@ public class BeanWrapperImpl extends AbstractPropertyAccessor implements BeanWra @@ -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 @@ -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 @@ -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);

9
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/annotation/Spr7839Tests.java

@ -2,8 +2,6 @@ package org.springframework.web.servlet.mvc.annotation; @@ -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 { @@ -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 { @@ -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) {

Loading…
Cancel
Save