From e0d922d35202fdb9f309f2aeccd8293b09943ebd Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 6 Dec 2011 21:17:25 +0000 Subject: [PATCH] ConversionService is able to work with "Collections.emptyList()" as target type (again; SPR-7293) --- .../support/CollectionToCollectionConverter.java | 3 +++ .../core/convert/support/MapToMapConverter.java | 3 +++ .../support/CollectionToCollectionConverterTests.java | 10 ++++++++++ 3 files changed, 16 insertions(+) diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToCollectionConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToCollectionConverter.java index 74eb7603795..42ad1e8c398 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToCollectionConverter.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToCollectionConverter.java @@ -60,6 +60,9 @@ final class CollectionToCollectionConverter implements ConditionalGenericConvert } boolean copyRequired = !targetType.getType().isInstance(source); Collection sourceCollection = (Collection) source; + if (!copyRequired && sourceCollection.isEmpty()) { + return sourceCollection; + } Collection target = CollectionFactory.createCollection(targetType.getType(), sourceCollection.size()); if (targetType.getElementTypeDescriptor() == null) { for (Object element : sourceCollection) { 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 6652fc84333..e8923e3ec6e 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 @@ -59,6 +59,9 @@ final class MapToMapConverter implements ConditionalGenericConverter { } boolean copyRequired = !targetType.getType().isInstance(source); Map sourceMap = (Map) source; + if (!copyRequired && sourceMap.isEmpty()) { + return sourceMap; + } Map targetMap = CollectionFactory.createMap(targetType.getType(), sourceMap.size()); for (Map.Entry entry : sourceMap.entrySet()) { Object sourceKey = entry.getKey(); diff --git a/org.springframework.core/src/test/java/org/springframework/core/convert/support/CollectionToCollectionConverterTests.java b/org.springframework.core/src/test/java/org/springframework/core/convert/support/CollectionToCollectionConverterTests.java index a69f19f0bca..20e6a5f9d7a 100644 --- a/org.springframework.core/src/test/java/org/springframework/core/convert/support/CollectionToCollectionConverterTests.java +++ b/org.springframework.core/src/test/java/org/springframework/core/convert/support/CollectionToCollectionConverterTests.java @@ -24,6 +24,7 @@ import java.net.URL; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.Collections; import java.util.LinkedList; import java.util.List; import java.util.Vector; @@ -289,6 +290,15 @@ public class CollectionToCollectionConverterTests { testCollectionConversionToArrayList(vector); } + @Test + public void testCollectionsEmptyList() throws Exception { + CollectionToCollectionConverter converter = new CollectionToCollectionConverter(new GenericConversionService()); + TypeDescriptor type = new TypeDescriptor(getClass().getField("list")); + converter.convert(list, type, TypeDescriptor.valueOf(Class.forName("java.util.Collections$EmptyList"))); + } + + public List list = Collections.emptyList(); + @SuppressWarnings("rawtypes") private void testCollectionConversionToArrayList(Collection aSource) { Object myConverted = (new CollectionToCollectionConverter(new GenericConversionService())).convert(