From d269c050cc536c1c59d930427cf05bf7c88ab225 Mon Sep 17 00:00:00 2001 From: Keith Donald Date: Fri, 7 Jan 2011 19:22:45 +0000 Subject: [PATCH] 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 --- .../beans/BeanWrapperImpl.java | 20 ++++++++++++++----- .../servlet/mvc/annotation/Spr7839Tests.java | 9 +++------ 2 files changed, 18 insertions(+), 11 deletions(-) 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 8a4cf937be0..cf6746f3bab 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 @@ -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); diff --git a/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/annotation/Spr7839Tests.java b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/annotation/Spr7839Tests.java index 7f04d7b5a6b..2bfedd28710 100644 --- a/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/annotation/Spr7839Tests.java +++ b/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; 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[] arrayOfLists; - private Map map = new HashMap(); + private Map map; - private Map> mapOfLists = new HashMap>(); + private Map> mapOfLists; public NestedBean() { - mapOfLists.put("apples", new ArrayList()); + } public NestedBean(String foo) {