From 8e23685a6dc7cbb810e8814c60379985f6c4373e Mon Sep 17 00:00:00 2001 From: Keith Donald Date: Fri, 7 Jan 2011 03:24:24 +0000 Subject: [PATCH] support for empty collection->collection. map->map, collection->array conversion --- .../support/CollectionToArrayConverter.java | 6 ++- .../support/CollectionToObjectConverter.java | 6 ++- .../convert/support/MapToMapConverter.java | 8 +++ .../GenericConversionServiceTests.java | 49 +++++++++++++++++-- 4 files changed, 63 insertions(+), 6 deletions(-) diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToArrayConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToArrayConverter.java index e26bae837f7..36e084ad951 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToArrayConverter.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToArrayConverter.java @@ -49,7 +49,11 @@ final class CollectionToArrayConverter implements ConditionalGenericConverter { } public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) { - return this.conversionService.canConvert(sourceType.getElementTypeDescriptor(), targetType.getElementTypeDescriptor()); + TypeDescriptor sourceElementType = sourceType.getElementTypeDescriptor(); + if (Object.class.equals(sourceElementType.getType())) { + return true; + } + return this.conversionService.canConvert(sourceElementType, targetType.getElementTypeDescriptor()); } public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToObjectConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToObjectConverter.java index c97b4c9f685..d5785b2df56 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToObjectConverter.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToObjectConverter.java @@ -43,7 +43,11 @@ final class CollectionToObjectConverter implements ConditionalGenericConverter { } public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) { - return this.conversionService.canConvert(sourceType.getElementTypeDescriptor(), targetType); + TypeDescriptor sourceElementType = sourceType.getElementTypeDescriptor(); + if (Object.class.equals(sourceElementType.getType())) { + return true; + } + return this.conversionService.canConvert(sourceElementType, targetType); } public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/MapToMapConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/MapToMapConverter.java index 9a099773c6e..6d675d67b00 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/MapToMapConverter.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/MapToMapConverter.java @@ -49,6 +49,14 @@ final class MapToMapConverter implements ConditionalGenericConverter { } public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) { + TypeDescriptor sourceKeyType = sourceType.getMapKeyTypeDescriptor(); + TypeDescriptor targetKeyType = targetType.getMapKeyTypeDescriptor(); + TypeDescriptor sourceValueType = sourceType.getMapValueTypeDescriptor(); + TypeDescriptor targetValueType = targetType.getMapValueTypeDescriptor(); + if (Object.class.equals(sourceKeyType.getType()) && Object.class.equals(sourceValueType.getType()) + || Object.class.equals(targetKeyType.getType()) && Object.class.equals(targetValueType.getType())) { + return true; + } return this.conversionService.canConvert(sourceType.getMapKeyTypeDescriptor(), targetType.getMapKeyTypeDescriptor()) && this.conversionService.canConvert(sourceType.getMapValueTypeDescriptor(), targetType.getMapValueTypeDescriptor()); } diff --git a/org.springframework.core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java b/org.springframework.core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java index 3ca6aa040b5..033396842ef 100644 --- a/org.springframework.core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java +++ b/org.springframework.core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java @@ -16,6 +16,14 @@ package org.springframework.core.convert.support; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; @@ -24,15 +32,13 @@ import java.util.LinkedList; import java.util.List; import java.util.Map; -import static org.junit.Assert.*; import org.junit.Test; - import org.springframework.core.convert.ConversionFailedException; import org.springframework.core.convert.ConverterNotFoundException; import org.springframework.core.convert.TypeDescriptor; import org.springframework.core.convert.converter.Converter; -import org.springframework.core.io.Resource; import org.springframework.core.io.DescriptiveResource; +import org.springframework.core.io.Resource; import org.springframework.util.StopWatch; import org.springframework.util.StringUtils; @@ -383,7 +389,7 @@ public class GenericConversionServiceTests { @Test - public void emptyList() throws Exception { + public void emptyListToList() throws Exception { conversionService.addConverter(new CollectionToCollectionConverter(conversionService)); conversionService.addConverterFactory(new StringToNumberConverterFactory()); List list = new ArrayList(); @@ -394,6 +400,41 @@ public class GenericConversionServiceTests { } public List emptyListTarget; + + @Test + public void emptyListToArray() throws Exception { + conversionService.addConverter(new CollectionToArrayConverter(conversionService)); + conversionService.addConverterFactory(new StringToNumberConverterFactory()); + List list = new ArrayList(); + TypeDescriptor sourceType = TypeDescriptor.forObject(list); + TypeDescriptor targetType = TypeDescriptor.valueOf(String[].class); + assertTrue(conversionService.canConvert(sourceType, targetType)); + assertEquals(0, ((String[])conversionService.convert(list, sourceType, targetType)).length); + } + + @Test + public void emptyListToObject() throws Exception { + conversionService.addConverter(new CollectionToObjectConverter(conversionService)); + conversionService.addConverterFactory(new StringToNumberConverterFactory()); + List list = new ArrayList(); + TypeDescriptor sourceType = TypeDescriptor.forObject(list); + TypeDescriptor targetType = TypeDescriptor.valueOf(Integer.class); + assertTrue(conversionService.canConvert(sourceType, targetType)); + assertNull(conversionService.convert(list, sourceType, targetType)); + } + + @Test + public void emptyMapToMap() throws Exception { + conversionService.addConverter(new MapToMapConverter(conversionService)); + conversionService.addConverterFactory(new StringToNumberConverterFactory()); + Map map = new HashMap(); + TypeDescriptor sourceType = TypeDescriptor.forObject(map); + TypeDescriptor targetType = new TypeDescriptor(getClass().getField("emptyMapTarget")); + assertTrue(conversionService.canConvert(sourceType, targetType)); + assertEquals(map, conversionService.convert(map, sourceType, targetType)); + } + + public Map emptyMapTarget; private interface MyBaseInterface {