ConversionService is able to work with "Collections.emptyList()" as target type (again; SPR-7293)

This commit is contained in:
Juergen Hoeller 2011-12-06 21:17:25 +00:00
parent 5ab2bf16a5
commit e0d922d352
3 changed files with 16 additions and 0 deletions

View File

@ -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<Object> target = CollectionFactory.createCollection(targetType.getType(), sourceCollection.size());
if (targetType.getElementTypeDescriptor() == null) {
for (Object element : sourceCollection) {

View File

@ -59,6 +59,9 @@ final class MapToMapConverter implements ConditionalGenericConverter {
}
boolean copyRequired = !targetType.getType().isInstance(source);
Map<Object, Object> sourceMap = (Map<Object, Object>) source;
if (!copyRequired && sourceMap.isEmpty()) {
return sourceMap;
}
Map<Object, Object> targetMap = CollectionFactory.createMap(targetType.getType(), sourceMap.size());
for (Map.Entry<Object, Object> entry : sourceMap.entrySet()) {
Object sourceKey = entry.getKey();

View File

@ -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<String> aSource) {
Object myConverted = (new CollectionToCollectionConverter(new GenericConversionService())).convert(