support for empty collection->collection. map->map, collection->array conversion
This commit is contained in:
parent
42403a37c3
commit
8e23685a6d
|
|
@ -49,7 +49,11 @@ final class CollectionToArrayConverter implements ConditionalGenericConverter {
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
|
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) {
|
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
|
||||||
|
|
|
||||||
|
|
@ -43,7 +43,11 @@ final class CollectionToObjectConverter implements ConditionalGenericConverter {
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
|
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) {
|
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
|
||||||
|
|
|
||||||
|
|
@ -49,6 +49,14 @@ final class MapToMapConverter implements ConditionalGenericConverter {
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
|
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()) &&
|
return this.conversionService.canConvert(sourceType.getMapKeyTypeDescriptor(), targetType.getMapKeyTypeDescriptor()) &&
|
||||||
this.conversionService.canConvert(sourceType.getMapValueTypeDescriptor(), targetType.getMapValueTypeDescriptor());
|
this.conversionService.canConvert(sourceType.getMapValueTypeDescriptor(), targetType.getMapValueTypeDescriptor());
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,14 @@
|
||||||
|
|
||||||
package org.springframework.core.convert.support;
|
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.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
@ -24,15 +32,13 @@ import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import org.springframework.core.convert.ConversionFailedException;
|
import org.springframework.core.convert.ConversionFailedException;
|
||||||
import org.springframework.core.convert.ConverterNotFoundException;
|
import org.springframework.core.convert.ConverterNotFoundException;
|
||||||
import org.springframework.core.convert.TypeDescriptor;
|
import org.springframework.core.convert.TypeDescriptor;
|
||||||
import org.springframework.core.convert.converter.Converter;
|
import org.springframework.core.convert.converter.Converter;
|
||||||
import org.springframework.core.io.Resource;
|
|
||||||
import org.springframework.core.io.DescriptiveResource;
|
import org.springframework.core.io.DescriptiveResource;
|
||||||
|
import org.springframework.core.io.Resource;
|
||||||
import org.springframework.util.StopWatch;
|
import org.springframework.util.StopWatch;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
|
|
@ -383,7 +389,7 @@ public class GenericConversionServiceTests {
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void emptyList() throws Exception {
|
public void emptyListToList() throws Exception {
|
||||||
conversionService.addConverter(new CollectionToCollectionConverter(conversionService));
|
conversionService.addConverter(new CollectionToCollectionConverter(conversionService));
|
||||||
conversionService.addConverterFactory(new StringToNumberConverterFactory());
|
conversionService.addConverterFactory(new StringToNumberConverterFactory());
|
||||||
List<String> list = new ArrayList<String>();
|
List<String> list = new ArrayList<String>();
|
||||||
|
|
@ -394,6 +400,41 @@ public class GenericConversionServiceTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Integer> emptyListTarget;
|
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 {
|
private interface MyBaseInterface {
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue