SPR-6537 initial commit; more extensive testing to follow

This commit is contained in:
Keith Donald 2009-12-10 17:36:58 +00:00
parent dd1bbde891
commit 96de14ef2e
16 changed files with 125 additions and 44 deletions

View File

@ -32,7 +32,7 @@ import org.springframework.util.ObjectUtils;
*/
final class ArrayToArrayConverter implements GenericConverter {
private final GenericConverter helperConverter;
private final CollectionToArrayConverter helperConverter;
public ArrayToArrayConverter(GenericConversionService conversionService) {
this.helperConverter = new CollectionToArrayConverter(conversionService);
@ -42,6 +42,10 @@ final class ArrayToArrayConverter implements GenericConverter {
return Collections.singleton(new ConvertiblePair(Object[].class, Object[].class));
}
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
return this.helperConverter.matches(sourceType, targetType);
}
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
return this.helperConverter.convert(Arrays.asList(ObjectUtils.toObjectArray(source)), sourceType, targetType);
}

View File

@ -16,6 +16,8 @@
package org.springframework.core.convert.support;
import static org.springframework.core.convert.support.ConversionUtils.invokeConverter;
import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Collections;
@ -24,8 +26,8 @@ import java.util.Set;
import org.springframework.core.CollectionFactory;
import org.springframework.core.convert.ConverterNotFoundException;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.ConditionalGenericConverter;
import org.springframework.core.convert.converter.GenericConverter;
import static org.springframework.core.convert.support.ConversionUtils.*;
/**
* Converts from an array to a collection.
@ -33,7 +35,7 @@ import static org.springframework.core.convert.support.ConversionUtils.*;
* @author Keith Donald
* @since 3.0
*/
final class ArrayToCollectionConverter implements GenericConverter {
final class ArrayToCollectionConverter implements ConditionalGenericConverter {
private final GenericConversionService conversionService;
@ -45,6 +47,10 @@ final class ArrayToCollectionConverter implements GenericConverter {
return Collections.singleton(new ConvertiblePair(Object[].class, Collection.class));
}
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
return this.conversionService.canConvert(sourceType.getElementTypeDescriptor(), targetType.getElementTypeDescriptor());
}
@SuppressWarnings("unchecked")
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
if (source == null) {

View File

@ -22,7 +22,7 @@ import java.util.Map;
import java.util.Set;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.GenericConverter;
import org.springframework.core.convert.converter.ConditionalGenericConverter;
import org.springframework.util.ObjectUtils;
/**
@ -31,7 +31,7 @@ import org.springframework.util.ObjectUtils;
* @author Keith Donald
* @since 3.0
*/
final class ArrayToMapConverter implements GenericConverter {
final class ArrayToMapConverter implements ConditionalGenericConverter {
private final CollectionToMapConverter helperConverter;
@ -43,6 +43,10 @@ final class ArrayToMapConverter implements GenericConverter {
return Collections.singleton(new ConvertiblePair(Object[].class, Map.class));
}
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
return this.helperConverter.matches(sourceType, targetType);
}
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
return this.helperConverter.convert(Arrays.asList(ObjectUtils.toObjectArray(source)), sourceType, targetType);
}

View File

@ -21,7 +21,7 @@ import java.util.Collections;
import java.util.Set;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.GenericConverter;
import org.springframework.core.convert.converter.ConditionalGenericConverter;
import org.springframework.util.ObjectUtils;
/**
@ -30,7 +30,7 @@ import org.springframework.util.ObjectUtils;
* @author Keith Donald
* @since 3.0
*/
final class ArrayToObjectConverter implements GenericConverter {
final class ArrayToObjectConverter implements ConditionalGenericConverter {
private final CollectionToObjectConverter helperConverter;
@ -41,6 +41,10 @@ final class ArrayToObjectConverter implements GenericConverter {
public Set<ConvertiblePair> getConvertibleTypes() {
return Collections.singleton(new ConvertiblePair(Object[].class, Object.class));
}
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
return this.helperConverter.matches(sourceType, targetType);
}
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
return this.helperConverter.convert(Arrays.asList(ObjectUtils.toObjectArray(source)), sourceType, targetType);

View File

@ -16,6 +16,9 @@
package org.springframework.core.convert.support;
import static org.springframework.core.convert.support.ConversionUtils.getElementType;
import static org.springframework.core.convert.support.ConversionUtils.invokeConverter;
import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Collections;
@ -24,8 +27,8 @@ import java.util.Set;
import org.springframework.core.convert.ConverterNotFoundException;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.ConditionalGenericConverter;
import org.springframework.core.convert.converter.GenericConverter;
import static org.springframework.core.convert.support.ConversionUtils.*;
/**
* Converts from a Collection to an array.
@ -33,7 +36,7 @@ import static org.springframework.core.convert.support.ConversionUtils.*;
* @author Keith Donald
* @since 3.0
*/
final class CollectionToArrayConverter implements GenericConverter {
final class CollectionToArrayConverter implements ConditionalGenericConverter {
private final GenericConversionService conversionService;
@ -45,6 +48,10 @@ final class CollectionToArrayConverter implements GenericConverter {
return Collections.singleton(new ConvertiblePair(Collection.class, Object[].class));
}
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
return this.conversionService.canConvert(sourceType.getElementTypeDescriptor(), targetType.getElementTypeDescriptor());
}
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
if (source == null) {
return this.conversionService.convertNullSource(sourceType, targetType);

View File

@ -23,6 +23,7 @@ import java.util.Set;
import org.springframework.core.CollectionFactory;
import org.springframework.core.convert.ConverterNotFoundException;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.ConditionalGenericConverter;
import org.springframework.core.convert.converter.GenericConverter;
import static org.springframework.core.convert.support.ConversionUtils.*;
@ -32,7 +33,7 @@ import static org.springframework.core.convert.support.ConversionUtils.*;
* @author Keith Donald
* @since 3.0
*/
final class CollectionToCollectionConverter implements GenericConverter {
final class CollectionToCollectionConverter implements ConditionalGenericConverter {
private final GenericConversionService conversionService;
@ -44,6 +45,10 @@ final class CollectionToCollectionConverter implements GenericConverter {
return Collections.singleton(new ConvertiblePair(Collection.class, Collection.class));
}
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
return this.conversionService.canConvert(sourceType.getElementTypeDescriptor(), targetType.getElementTypeDescriptor());
}
@SuppressWarnings("unchecked")
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
if (source == null) {

View File

@ -16,6 +16,8 @@
package org.springframework.core.convert.support;
import static org.springframework.core.convert.support.ConversionUtils.getElementType;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
@ -23,8 +25,7 @@ import java.util.Set;
import org.springframework.core.CollectionFactory;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.GenericConverter;
import static org.springframework.core.convert.support.ConversionUtils.*;
import org.springframework.core.convert.converter.ConditionalGenericConverter;
/**
* Converts from a Collection to a Map.
@ -32,7 +33,7 @@ import static org.springframework.core.convert.support.ConversionUtils.*;
* @author Keith Donald
* @since 3.0
*/
final class CollectionToMapConverter implements GenericConverter {
final class CollectionToMapConverter implements ConditionalGenericConverter {
private final GenericConversionService conversionService;
@ -44,6 +45,11 @@ final class CollectionToMapConverter implements GenericConverter {
return Collections.singleton(new ConvertiblePair(Collection.class, Map.class));
}
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
return this.conversionService.canConvert(sourceType.getElementTypeDescriptor(), targetType.getMapKeyTypeDescriptor()) &&
this.conversionService.canConvert(sourceType.getElementTypeDescriptor(), targetType.getMapValueTypeDescriptor());
}
@SuppressWarnings("unchecked")
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
if (source == null) {

View File

@ -16,14 +16,17 @@
package org.springframework.core.convert.support;
import static org.springframework.core.convert.support.ConversionUtils.getElementType;
import static org.springframework.core.convert.support.ConversionUtils.invokeConverter;
import java.util.Collection;
import java.util.Collections;
import java.util.Set;
import org.springframework.core.convert.ConverterNotFoundException;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.ConditionalGenericConverter;
import org.springframework.core.convert.converter.GenericConverter;
import static org.springframework.core.convert.support.ConversionUtils.*;
/**
* Converts from a Collection to a single Object.
@ -31,7 +34,7 @@ import static org.springframework.core.convert.support.ConversionUtils.*;
* @author Keith Donald
* @since 3.0
*/
final class CollectionToObjectConverter implements GenericConverter {
final class CollectionToObjectConverter implements ConditionalGenericConverter {
private static final String DELIMITER = ",";
@ -45,6 +48,10 @@ final class CollectionToObjectConverter implements GenericConverter {
return Collections.singleton(new ConvertiblePair(Collection.class, Object.class));
}
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
return this.conversionService.canConvert(sourceType.getElementTypeDescriptor(), targetType);
}
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
if (source == null) {
return this.conversionService.convertNullSource(sourceType, targetType);

View File

@ -22,7 +22,7 @@ import java.util.Map;
import java.util.Set;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.GenericConverter;
import org.springframework.core.convert.converter.ConditionalGenericConverter;
/**
* Converts from a Map to an array.
@ -30,11 +30,11 @@ import org.springframework.core.convert.converter.GenericConverter;
* @author Keith Donald
* @since 3.0
*/
final class MapToArrayConverter implements GenericConverter {
final class MapToArrayConverter implements ConditionalGenericConverter {
private final GenericConverter mapToCollectionHelperConverter;
private final MapToCollectionConverter mapToCollectionHelperConverter;
private final GenericConverter collectionToArrayHelperConverter;
private final CollectionToArrayConverter collectionToArrayHelperConverter;
public MapToArrayConverter(GenericConversionService conversionService) {
this.mapToCollectionHelperConverter = new MapToCollectionConverter(conversionService);
@ -45,6 +45,10 @@ final class MapToArrayConverter implements GenericConverter {
return Collections.singleton(new ConvertiblePair(Map.class, Object[].class));
}
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
return this.mapToCollectionHelperConverter.matches(sourceType, targetType);
}
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
TypeDescriptor collectionType = TypeDescriptor.collection(List.class, targetType.getElementTypeDescriptor());
Object collection = this.mapToCollectionHelperConverter.convert(source, sourceType, collectionType);

View File

@ -16,6 +16,8 @@
package org.springframework.core.convert.support;
import static org.springframework.core.convert.support.ConversionUtils.getMapEntryTypes;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
@ -23,8 +25,7 @@ import java.util.Set;
import org.springframework.core.CollectionFactory;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.GenericConverter;
import static org.springframework.core.convert.support.ConversionUtils.*;
import org.springframework.core.convert.converter.ConditionalGenericConverter;
/**
* Converts from a Map to a Collection.
@ -32,7 +33,7 @@ import static org.springframework.core.convert.support.ConversionUtils.*;
* @author Keith Donald
* @since 3.0
*/
final class MapToCollectionConverter implements GenericConverter {
final class MapToCollectionConverter implements ConditionalGenericConverter {
private final GenericConversionService conversionService;
@ -44,6 +45,11 @@ final class MapToCollectionConverter implements GenericConverter {
return Collections.singleton(new ConvertiblePair(Map.class, Collection.class));
}
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
return this.conversionService.canConvert(sourceType.getMapKeyTypeDescriptor(), targetType.getElementTypeDescriptor()) &&
this.conversionService.canConvert(sourceType.getMapValueTypeDescriptor(), targetType.getElementTypeDescriptor());
}
@SuppressWarnings("unchecked")
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
if (source == null) {

View File

@ -16,14 +16,15 @@
package org.springframework.core.convert.support;
import static org.springframework.core.convert.support.ConversionUtils.getMapEntryTypes;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import org.springframework.core.CollectionFactory;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.GenericConverter;
import static org.springframework.core.convert.support.ConversionUtils.*;
import org.springframework.core.convert.converter.ConditionalGenericConverter;
/**
* Converts from a source Map to a target Map type.
@ -31,7 +32,7 @@ import static org.springframework.core.convert.support.ConversionUtils.*;
* @author Keith Donald
* @since 3.0
*/
final class MapToMapConverter implements GenericConverter {
final class MapToMapConverter implements ConditionalGenericConverter {
private final GenericConversionService conversionService;
@ -43,6 +44,11 @@ final class MapToMapConverter implements GenericConverter {
return Collections.singleton(new ConvertiblePair(Map.class, Map.class));
}
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
return this.conversionService.canConvert(sourceType.getMapKeyTypeDescriptor(), targetType.getMapKeyTypeDescriptor()) &&
this.conversionService.canConvert(sourceType.getMapValueTypeDescriptor(), targetType.getMapValueTypeDescriptor());
}
@SuppressWarnings("unchecked")
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
if (source == null) {

View File

@ -16,6 +16,8 @@
package org.springframework.core.convert.support;
import static org.springframework.core.convert.support.ConversionUtils.getMapEntryTypes;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Collections;
@ -24,8 +26,7 @@ import java.util.Properties;
import java.util.Set;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.GenericConverter;
import static org.springframework.core.convert.support.ConversionUtils.*;
import org.springframework.core.convert.converter.ConditionalGenericConverter;
/**
* Converts from a Map to a single Object.
@ -33,7 +34,7 @@ import static org.springframework.core.convert.support.ConversionUtils.*;
* @author Keith Donald
* @since 3.0
*/
final class MapToObjectConverter implements GenericConverter {
final class MapToObjectConverter implements ConditionalGenericConverter {
private final GenericConversionService conversionService;
@ -45,6 +46,11 @@ final class MapToObjectConverter implements GenericConverter {
return Collections.singleton(new ConvertiblePair(Map.class, Object.class));
}
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
return this.conversionService.canConvert(sourceType.getMapKeyTypeDescriptor(), targetType) &&
this.conversionService.canConvert(sourceType.getMapValueTypeDescriptor(), targetType);
}
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
if (source == null) {
return this.conversionService.convertNullSource(sourceType, targetType);

View File

@ -16,14 +16,16 @@
package org.springframework.core.convert.support;
import static org.springframework.core.convert.support.ConversionUtils.invokeConverter;
import java.lang.reflect.Array;
import java.util.Collections;
import java.util.Set;
import org.springframework.core.convert.ConverterNotFoundException;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.ConditionalGenericConverter;
import org.springframework.core.convert.converter.GenericConverter;
import static org.springframework.core.convert.support.ConversionUtils.*;
import org.springframework.util.StringUtils;
/**
@ -32,7 +34,7 @@ import org.springframework.util.StringUtils;
* @author Keith Donald
* @since 3.0
*/
final class ObjectToArrayConverter implements GenericConverter {
final class ObjectToArrayConverter implements ConditionalGenericConverter {
private final GenericConversionService conversionService;
@ -44,6 +46,10 @@ final class ObjectToArrayConverter implements GenericConverter {
return Collections.singleton(new ConvertiblePair(Object.class, Object[].class));
}
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
return this.conversionService.canConvert(sourceType, targetType.getElementTypeDescriptor());
}
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
if (source == null) {
return this.conversionService.convertNullSource(sourceType, targetType);

View File

@ -16,6 +16,8 @@
package org.springframework.core.convert.support;
import static org.springframework.core.convert.support.ConversionUtils.invokeConverter;
import java.util.Collection;
import java.util.Collections;
import java.util.Set;
@ -23,8 +25,8 @@ import java.util.Set;
import org.springframework.core.CollectionFactory;
import org.springframework.core.convert.ConverterNotFoundException;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.ConditionalGenericConverter;
import org.springframework.core.convert.converter.GenericConverter;
import static org.springframework.core.convert.support.ConversionUtils.*;
import org.springframework.util.StringUtils;
/**
@ -33,7 +35,7 @@ import org.springframework.util.StringUtils;
* @author Keith Donald
* @since 3.0
*/
final class ObjectToCollectionConverter implements GenericConverter {
final class ObjectToCollectionConverter implements ConditionalGenericConverter {
private final GenericConversionService conversionService;
@ -45,6 +47,10 @@ final class ObjectToCollectionConverter implements GenericConverter {
return Collections.singleton(new ConvertiblePair(Object.class, Collection.class));
}
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
return this.conversionService.canConvert(sourceType, targetType.getElementTypeDescriptor());
}
@SuppressWarnings("unchecked")
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
if (source == null) {

View File

@ -24,7 +24,7 @@ import java.util.Set;
import org.springframework.core.CollectionFactory;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.GenericConverter;
import org.springframework.core.convert.converter.ConditionalGenericConverter;
/**
* Converts from a single Object to a Map.
@ -32,7 +32,7 @@ import org.springframework.core.convert.converter.GenericConverter;
* @author Keith Donald
* @since 3.0
*/
final class ObjectToMapConverter implements GenericConverter {
final class ObjectToMapConverter implements ConditionalGenericConverter {
private final GenericConversionService conversionService;
@ -44,6 +44,11 @@ final class ObjectToMapConverter implements GenericConverter {
return Collections.singleton(new ConvertiblePair(Object.class, Map.class));
}
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
return this.conversionService.canConvert(sourceType, targetType.getMapKeyTypeDescriptor()) &&
this.conversionService.canConvert(sourceType, targetType.getMapValueTypeDescriptor());
}
@SuppressWarnings("unchecked")
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
if (source == null) {

View File

@ -16,14 +16,19 @@
package org.springframework.core.convert.support;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
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.HashMap;
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;
@ -139,14 +144,8 @@ public class GenericConversionServiceTests {
@Test
public void genericConverterDelegatingBackToConversionServiceConverterNotFound() {
try {
conversionService.addGenericConverter(new ObjectToArrayConverter(conversionService));
conversionService.convert("1", Integer[].class);
fail("Should hace failed");
}
catch (ConversionFailedException e) {
assertTrue(e.getCause() instanceof ConverterNotFoundException);
}
conversionService.addGenericConverter(new ObjectToArrayConverter(conversionService));
assertFalse(conversionService.canConvert(String.class, Integer[].class));
}
@Test