array autogrow on set e.g. array[0]=foo

git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@3887 50f2f4bb-b051-0410-bef5-90022cba6387
This commit is contained in:
Keith Donald 2011-01-07 17:28:18 +00:00
parent 407c54ca29
commit 7d9e728c55
3 changed files with 8 additions and 6 deletions

View File

@ -37,7 +37,6 @@ import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.CollectionFactory;
import org.springframework.core.GenericCollectionTypeResolver;
import org.springframework.core.MethodParameter;
@ -946,12 +945,18 @@ public class BeanWrapperImpl extends AbstractPropertyAccessor implements BeanWra
int arrayIndex = Integer.parseInt(key);
Object oldValue = null;
try {
if (isExtractOldValueForEditor()) {
if (isExtractOldValueForEditor() && arrayIndex < Array.getLength(propValue)) {
oldValue = Array.get(propValue, arrayIndex);
}
Object convertedValue = convertIfNecessary(propertyName, oldValue, pv.getValue(), requiredType,
PropertyTypeDescriptor.forNestedType(requiredType, new MethodParameter(pd.getReadMethod(), -1, tokens.keys.length), pd));
// TODO reduce this grow algorithm along side the null gap algorithm for setting lists below ... the two are inconsistent
propValue = growArrayIfNecessary(propValue, arrayIndex, actualName);
Array.set(propValue, arrayIndex, convertedValue);
PropertyValue newValue = new PropertyValue(actualName, propValue);
newValue.resolvedDescriptor = pd;
newValue.conversionNecessary = false;
setPropertyValue(newValue);
}
catch (IndexOutOfBoundsException ex) {
throw new InvalidPropertyException(getRootClass(), this.nestedPath + propertyName,

View File

@ -24,7 +24,6 @@ import java.util.Locale;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.context.i18n.LocaleContextHolder;

View File

@ -94,14 +94,13 @@ public class Spr7839Tests {
@Test
@Ignore
public void arrayOfLists() throws Exception {
// TODO two issues here: no autogrow for array properties, and GenericCollectionResolver not capable of accessing nested element type
// TODO TypeDescriptor not capable of accessing nested element type for arrays
request.setRequestURI("/nested/arrayOfLists");
request.addParameter("nested.arrayOfLists[0]", "Nested1,Nested2");
adapter.handle(request, response, controller);
}
@Test
@Ignore
public void map() throws Exception {
request.setRequestURI("/nested/map");
request.addParameter("nested.map['apple'].foo", "bar");
@ -145,7 +144,6 @@ public class Spr7839Tests {
@RequestMapping("/nested/arrayOfLists")
public void handlerArrayOfLists(JavaBean bean) {
System.out.println(bean.nested.arrayOfLists[0].get(1).getClass());
assertEquals("Nested2", bean.nested.arrayOfLists[0].get(1).foo);
}