diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToArrayConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToArrayConverter.java index caf98954fc5..ac929280095 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToArrayConverter.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToArrayConverter.java @@ -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); } diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToCollectionConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToCollectionConverter.java index 9fd4dd856b6..087b77adbbe 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToCollectionConverter.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToCollectionConverter.java @@ -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) { diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToMapConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToMapConverter.java index 5553dead0de..6789ef66d36 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToMapConverter.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToMapConverter.java @@ -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); } diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToObjectConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToObjectConverter.java index b6fa1c1c48e..c3b8a5c6366 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToObjectConverter.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToObjectConverter.java @@ -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 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); diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToArrayConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToArrayConverter.java index 2dd0e5846f3..acaa18b802a 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToArrayConverter.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToArrayConverter.java @@ -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); 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 1e4c5288803..8227385fb8e 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 @@ -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) { diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToMapConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToMapConverter.java index 236b27e9b74..1c33db2117d 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToMapConverter.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToMapConverter.java @@ -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) { diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToObjectConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToObjectConverter.java index caf248509c7..4f59c048150 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToObjectConverter.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToObjectConverter.java @@ -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); diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/MapToArrayConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/MapToArrayConverter.java index 38c53ec65b6..fa70f9024d8 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/MapToArrayConverter.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/MapToArrayConverter.java @@ -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); diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/MapToCollectionConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/MapToCollectionConverter.java index 9982f673144..5db1af64ffc 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/MapToCollectionConverter.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/MapToCollectionConverter.java @@ -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) { 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 e55e8609790..9920b1dff8a 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 @@ -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) { diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/MapToObjectConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/MapToObjectConverter.java index 7bde51dea5e..6cf2e759efb 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/MapToObjectConverter.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/MapToObjectConverter.java @@ -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); diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToArrayConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToArrayConverter.java index be1a90f1338..69f8561e6aa 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToArrayConverter.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToArrayConverter.java @@ -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); diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToCollectionConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToCollectionConverter.java index 3369b79c9ba..3a53d18eeed 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToCollectionConverter.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToCollectionConverter.java @@ -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) { diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToMapConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToMapConverter.java index e8de7618b67..0209599aaf2 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToMapConverter.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToMapConverter.java @@ -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) { diff --git a/org.springframework.core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java b/org.springframework.core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java index 4a44fa99e42..5e2f6e086b1 100644 --- a/org.springframework.core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java +++ b/org.springframework.core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java @@ -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