support for empty collection->collection. map->map, collection->array conversion

git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@3878 50f2f4bb-b051-0410-bef5-90022cba6387
This commit is contained in:
Keith Donald 2011-01-07 03:24:24 +00:00
parent 950493a4c8
commit 76443f55c1
4 changed files with 63 additions and 6 deletions

View File

@ -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) {

View File

@ -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) {

View File

@ -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());
}

View File

@ -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<String> list = new ArrayList<String>();
@ -395,6 +401,41 @@ public class GenericConversionServiceTests {
public List<Integer> emptyListTarget;
@Test
public void emptyListToArray() throws Exception {
conversionService.addConverter(new CollectionToArrayConverter(conversionService));
conversionService.addConverterFactory(new StringToNumberConverterFactory());
List<String> list = new ArrayList<String>();
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<String> list = new ArrayList<String>();
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<String, String> map = new HashMap<String, String>();
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<String, String> emptyMapTarget;
private interface MyBaseInterface {
}