From c6c782df59c09a6a36c8283cbd725e9aef20e0e8 Mon Sep 17 00:00:00 2001 From: Keith Donald Date: Thu, 6 Jan 2011 18:33:50 +0000 Subject: [PATCH] forNestedType usage clarification --- .../beans/BeanWrapperImpl.java | 7 ++--- .../format/number/NumberFormattingTests.java | 30 +++++++++++++++++-- .../core/convert/TypeDescriptor.java | 10 +++---- .../support/PropertyTypeDescriptor.java | 11 +++---- 4 files changed, 41 insertions(+), 17 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 5f3f5a82c3b..8729b284752 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 @@ -935,7 +935,7 @@ public class BeanWrapperImpl extends AbstractPropertyAccessor implements BeanWra oldValue = Array.get(propValue, arrayIndex); } Object convertedValue = convertIfNecessary(propertyName, oldValue, pv.getValue(), requiredType, - PropertyTypeDescriptor.forNestedType(requiredType, new MethodParameter(pd.getReadMethod(), -1), pd)); + PropertyTypeDescriptor.forNestedType(requiredType, new MethodParameter(pd.getReadMethod(), -1, tokens.keys.length), pd)); Array.set(propValue, arrayIndex, convertedValue); } catch (IndexOutOfBoundsException ex) { @@ -953,9 +953,8 @@ public class BeanWrapperImpl extends AbstractPropertyAccessor implements BeanWra if (isExtractOldValueForEditor() && index < list.size()) { oldValue = list.get(index); } - // TODO method parameter nesting level should be token.keys.length + 1 Object convertedValue = convertIfNecessary(propertyName, oldValue, pv.getValue(), requiredType, - PropertyTypeDescriptor.forNestedType(requiredType, new MethodParameter(pd.getReadMethod(), -1), pd)); + PropertyTypeDescriptor.forNestedType(requiredType, new MethodParameter(pd.getReadMethod(), -1, tokens.keys.length), pd)); if (index < list.size()) { list.set(index, convertedValue); } @@ -994,7 +993,7 @@ public class BeanWrapperImpl extends AbstractPropertyAccessor implements BeanWra // TODO method parameter nesting level should be token.keys.length + 1 Object convertedMapValue = convertIfNecessary( propertyName, oldValue, pv.getValue(), mapValueType, - PropertyTypeDescriptor.forNestedType(mapValueType, new MethodParameter(pd.getReadMethod(), -1), pd)); + PropertyTypeDescriptor.forNestedType(mapValueType, new MethodParameter(pd.getReadMethod(), -1, tokens.keys.length), pd)); map.put(convertedMapKey, convertedMapValue); } else { diff --git a/org.springframework.context/src/test/java/org/springframework/format/number/NumberFormattingTests.java b/org.springframework.context/src/test/java/org/springframework/format/number/NumberFormattingTests.java index d7d6885da70..5a809406805 100644 --- a/org.springframework.context/src/test/java/org/springframework/format/number/NumberFormattingTests.java +++ b/org.springframework.context/src/test/java/org/springframework/format/number/NumberFormattingTests.java @@ -153,6 +153,18 @@ public class NumberFormattingTests { assertEquals("2,35.00", binder.getBindingResult().getFieldValue("patternList[1]")); } + @Test + @Ignore + public void testPatternList2Formatting() { + MutablePropertyValues propertyValues = new MutablePropertyValues(); + propertyValues.add("patternList2[0]", "1,25.00"); + propertyValues.add("patternList2[1]", "2,35.00"); + binder.bind(propertyValues); + assertEquals(0, binder.getBindingResult().getErrorCount()); + assertEquals("1,25.00", binder.getBindingResult().getFieldValue("patternList[0]")); + assertEquals("2,35.00", binder.getBindingResult().getFieldValue("patternList[1]")); + } + @SuppressWarnings("unused") private static class TestBean { @@ -175,8 +187,11 @@ public class NumberFormattingTests { private BigDecimal[] patternArray; @NumberFormat(pattern="#,##.00") - private List[] patternList; + private List[] patternList; + @NumberFormat(pattern="#,##.00") + private List patternList2; + public Integer getNumberDefault() { return numberDefault; } @@ -225,13 +240,22 @@ public class NumberFormattingTests { this.patternArray = patternArray; } - public List[] getPatternList() { + public List[] getPatternList() { return patternList; } - public void setPatternList(List[] patternList) { + public void setPatternList(List[] patternList) { this.patternList = patternList; } + + public List getPatternList2() { + return patternList2; + } + + public void setPatternList2(List patternList2) { + this.patternList2 = patternList2; + } + } } diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/TypeDescriptor.java b/org.springframework.core/src/main/java/org/springframework/core/convert/TypeDescriptor.java index 588aa5224d4..965c18187f3 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/TypeDescriptor.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/TypeDescriptor.java @@ -136,11 +136,11 @@ public class TypeDescriptor { * Create a new type descriptor for a nested type declared on an array, collection, or map-based method parameter. * Use this factory method when you've resolved a nested source object such as a collection element or map value and wish to have it converted. * @param nestedType the nested type - * @param parentMethodParameter the parent method parameter declaring the collection or map + * @param methodParameter the method parameter declaring the collection or map * @return the nested type descriptor */ - public static TypeDescriptor forNestedType(Class nestedType, MethodParameter parentMethodParameter) { - return new TypeDescriptor(nestedType, parentMethodParameter); + public static TypeDescriptor forNestedType(Class nestedType, MethodParameter methodParameter) { + return new TypeDescriptor(nestedType, methodParameter); } /** @@ -410,9 +410,9 @@ public class TypeDescriptor { // subclassing hooks - protected TypeDescriptor(Class nestedType, MethodParameter parentMethodParameter) { + protected TypeDescriptor(Class nestedType, MethodParameter methodParameter) { this.type = handleUnknownNestedType(nestedType); - this.methodParameter = createNestedMethodParameter(parentMethodParameter); + this.methodParameter = createNestedMethodParameter(methodParameter); } protected Annotation[] resolveAnnotations() { diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/PropertyTypeDescriptor.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/PropertyTypeDescriptor.java index d4f3631b3a7..be2d111af62 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/PropertyTypeDescriptor.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/PropertyTypeDescriptor.java @@ -54,12 +54,13 @@ public class PropertyTypeDescriptor extends TypeDescriptor { /** * Create a new type descriptor for a nested type declared on an array, collection, or map-based property. * Use this factory method when you've resolved a nested source object such as a collection element or map value and wish to have it converted. + * Builds in protection for increasing the nesting level of the provided MethodParameter if the nestedType is itself a collection. * @param nestedType the nested type - * @param parentMethodParameter the parent property's method parameter that declares the collection or map - * @return the parent property descriptor + * @param parentMethodParameter the method parameter + * @return the property descriptor */ - public static PropertyTypeDescriptor forNestedType(Class nestedType, MethodParameter propertyMethodParameter, PropertyDescriptor propertyDescriptor) { - return new PropertyTypeDescriptor(nestedType, propertyMethodParameter, propertyDescriptor); + public static PropertyTypeDescriptor forNestedType(Class nestedType, MethodParameter methodParameter, PropertyDescriptor propertyDescriptor) { + return new PropertyTypeDescriptor(nestedType, methodParameter, propertyDescriptor); } /** @@ -120,4 +121,4 @@ public class PropertyTypeDescriptor extends TypeDescriptor { this.propertyDescriptor = propertyDescriptor; } -} +} \ No newline at end of file