From e5d551587ac715ef9d3290e440f0c0c0e5119263 Mon Sep 17 00:00:00 2001 From: Keith Donald Date: Tue, 7 Jun 2011 15:56:51 +0000 Subject: [PATCH] fixed failing test; initial conditional converter impls for collections, arrays, and maps --- .../springframework/core/convert/TypeDescriptor.java | 6 +++--- .../core/convert/support/ArrayToArrayConverter.java | 8 ++++++-- .../convert/support/ArrayToCollectionConverter.java | 8 ++++++-- .../core/convert/support/ArrayToObjectConverter.java | 8 ++++++-- .../core/convert/support/ArrayToStringConverter.java | 10 +++++++--- .../convert/support/CollectionToArrayConverter.java | 8 ++++++-- .../support/CollectionToCollectionConverter.java | 8 ++++++-- .../convert/support/CollectionToObjectConverter.java | 8 ++++++-- .../convert/support/CollectionToStringConverter.java | 8 ++++++-- .../core/convert/support/MapToMapConverter.java | 8 ++++++-- .../core/convert/support/ObjectToArrayConverter.java | 8 ++++++-- .../convert/support/ObjectToCollectionConverter.java | 8 ++++++-- 12 files changed, 70 insertions(+), 26 deletions(-) diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/TypeDescriptor.java b/org.springframework.core/src/main/java/org/springframework/core/convert/TypeDescriptor.java index 62c4867ae28..0b390976274 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/TypeDescriptor.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/TypeDescriptor.java @@ -293,11 +293,11 @@ public class TypeDescriptor { if (!typesAssignable) { return false; } - if (isArray()) { + if (isArray() && typeDescriptor.isArray()) { return getElementTypeDescriptor().isAssignableTo(typeDescriptor.getElementTypeDescriptor()); - } else if (isCollection()) { + } else if (isCollection() && typeDescriptor.isCollection()) { return isNestedAssignable(getElementTypeDescriptor(), typeDescriptor.getElementTypeDescriptor()); - } else if (isMap()) { + } else if (isMap() && typeDescriptor.isMap()) { return isNestedAssignable(getMapKeyTypeDescriptor(), typeDescriptor.getMapKeyTypeDescriptor()) && isNestedAssignable(getMapValueTypeDescriptor(), typeDescriptor.getMapValueTypeDescriptor()); } else { 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 51799159593..e30f4cb0935 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 @@ -22,7 +22,7 @@ import java.util.Set; import org.springframework.core.convert.ConversionService; 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; /** @@ -32,7 +32,7 @@ import org.springframework.util.ObjectUtils; * @author Keith Donald * @since 3.0 */ -final class ArrayToArrayConverter implements GenericConverter { +final class ArrayToArrayConverter implements ConditionalGenericConverter { private final CollectionToArrayConverter helperConverter; @@ -44,6 +44,10 @@ final class ArrayToArrayConverter implements GenericConverter { return Collections.singleton(new ConvertiblePair(Object[].class, Object[].class)); } + public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) { + return true; + } + 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 8bb233bca99..ab7086c8d48 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 @@ -24,7 +24,7 @@ import java.util.Set; import org.springframework.core.CollectionFactory; import org.springframework.core.convert.ConversionService; import org.springframework.core.convert.TypeDescriptor; -import org.springframework.core.convert.converter.GenericConverter; +import org.springframework.core.convert.converter.ConditionalGenericConverter; /** * Converts an Array to a Collection. @@ -36,7 +36,7 @@ import org.springframework.core.convert.converter.GenericConverter; * @author Keith Donald * @since 3.0 */ -final class ArrayToCollectionConverter implements GenericConverter { +final class ArrayToCollectionConverter implements ConditionalGenericConverter { private final ConversionService conversionService; @@ -48,6 +48,10 @@ final class ArrayToCollectionConverter implements GenericConverter { return Collections.singleton(new ConvertiblePair(Object[].class, Collection.class)); } + public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) { + return true; + } + @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/ArrayToObjectConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToObjectConverter.java index 842afd8524f..1031b1fd944 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 @@ -22,7 +22,7 @@ import java.util.Set; import org.springframework.core.convert.ConversionService; 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; /** @@ -32,7 +32,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; @@ -43,6 +43,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 true; + } 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/ArrayToStringConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToStringConverter.java index f2960d93b07..cbb7704923d 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToStringConverter.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToStringConverter.java @@ -22,7 +22,7 @@ import java.util.Set; import org.springframework.core.convert.ConversionService; 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; /** @@ -32,7 +32,7 @@ import org.springframework.util.ObjectUtils; * @author Keith Donald * @since 3.0 */ -final class ArrayToStringConverter implements GenericConverter { +final class ArrayToStringConverter implements ConditionalGenericConverter { private final CollectionToStringConverter helperConverter; @@ -43,7 +43,11 @@ final class ArrayToStringConverter implements GenericConverter { public Set getConvertibleTypes() { return Collections.singleton(new ConvertiblePair(Object[].class, String.class)); } - + + public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) { + return true; + } + 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 ad9a3f2cf0e..a29caa409af 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 @@ -23,7 +23,7 @@ import java.util.Set; import org.springframework.core.convert.ConversionService; import org.springframework.core.convert.TypeDescriptor; -import org.springframework.core.convert.converter.GenericConverter; +import org.springframework.core.convert.converter.ConditionalGenericConverter; /** * Converts a Collection to an array. @@ -36,7 +36,7 @@ import org.springframework.core.convert.converter.GenericConverter; * @author Keith Donald * @since 3.0 */ -final class CollectionToArrayConverter implements GenericConverter { +final class CollectionToArrayConverter implements ConditionalGenericConverter { private final ConversionService conversionService; @@ -48,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 true; + } + public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { if (source == null) { return null; 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 fcfb8f1d874..b14b4ee1f95 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,7 +23,7 @@ import java.util.Set; import org.springframework.core.CollectionFactory; import org.springframework.core.convert.ConversionService; import org.springframework.core.convert.TypeDescriptor; -import org.springframework.core.convert.converter.GenericConverter; +import org.springframework.core.convert.converter.ConditionalGenericConverter; /** * Converts from a Collection to another Collection. @@ -36,7 +36,7 @@ import org.springframework.core.convert.converter.GenericConverter; * @author Keith Donald * @since 3.0 */ -final class CollectionToCollectionConverter implements GenericConverter { +final class CollectionToCollectionConverter implements ConditionalGenericConverter { private final ConversionService conversionService; @@ -48,6 +48,10 @@ final class CollectionToCollectionConverter implements GenericConverter { return Collections.singleton(new ConvertiblePair(Collection.class, Collection.class)); } + public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) { + return true; + } + @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 0068041034a..79854a825f1 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 @@ -22,7 +22,7 @@ import java.util.Set; import org.springframework.core.convert.ConversionService; import org.springframework.core.convert.TypeDescriptor; -import org.springframework.core.convert.converter.GenericConverter; +import org.springframework.core.convert.converter.ConditionalGenericConverter; /** * Converts a Collection to an Object by returning the first collection element after converting it to the desired targetType. @@ -30,7 +30,7 @@ import org.springframework.core.convert.converter.GenericConverter; * @author Keith Donald * @since 3.0 */ -final class CollectionToObjectConverter implements GenericConverter { +final class CollectionToObjectConverter implements ConditionalGenericConverter { private final ConversionService conversionService; @@ -42,6 +42,10 @@ final class CollectionToObjectConverter implements GenericConverter { return Collections.singleton(new ConvertiblePair(Collection.class, Object.class)); } + public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) { + return true; + } + public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { if (source == null) { return null; diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToStringConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToStringConverter.java index f65fefcfcd1..48a207b4cfe 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToStringConverter.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToStringConverter.java @@ -22,7 +22,7 @@ import java.util.Set; import org.springframework.core.convert.ConversionService; import org.springframework.core.convert.TypeDescriptor; -import org.springframework.core.convert.converter.GenericConverter; +import org.springframework.core.convert.converter.ConditionalGenericConverter; /** * Converts a Collection to a comma-delimited String. @@ -30,7 +30,7 @@ import org.springframework.core.convert.converter.GenericConverter; * @author Keith Donald * @since 3.0 */ -final class CollectionToStringConverter implements GenericConverter { +final class CollectionToStringConverter implements ConditionalGenericConverter { private static final String DELIMITER = ","; @@ -44,6 +44,10 @@ final class CollectionToStringConverter implements GenericConverter { return Collections.singleton(new ConvertiblePair(Collection.class, String.class)); } + public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) { + return true; + } + public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { if (source == null) { return 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 6f4e3647577..2f721ec2c40 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 @@ -23,7 +23,7 @@ import java.util.Set; import org.springframework.core.CollectionFactory; import org.springframework.core.convert.ConversionService; import org.springframework.core.convert.TypeDescriptor; -import org.springframework.core.convert.converter.GenericConverter; +import org.springframework.core.convert.converter.ConditionalGenericConverter; /** * Converts a Map to another Map. @@ -36,7 +36,7 @@ import org.springframework.core.convert.converter.GenericConverter; * @author Keith Donald * @since 3.0 */ -final class MapToMapConverter implements GenericConverter { +final class MapToMapConverter implements ConditionalGenericConverter { private final ConversionService conversionService; @@ -48,6 +48,10 @@ final class MapToMapConverter implements GenericConverter { return Collections.singleton(new ConvertiblePair(Map.class, Map.class)); } + public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) { + return true; + } + @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/ObjectToArrayConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToArrayConverter.java index 6d2d122aa0e..944d0479434 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 @@ -22,7 +22,7 @@ import java.util.Set; import org.springframework.core.convert.ConversionService; import org.springframework.core.convert.TypeDescriptor; -import org.springframework.core.convert.converter.GenericConverter; +import org.springframework.core.convert.converter.ConditionalGenericConverter; /** * Converts an Object to a single-element Array containing the Object. @@ -31,7 +31,7 @@ import org.springframework.core.convert.converter.GenericConverter; * @author Keith Donald * @since 3.0 */ -final class ObjectToArrayConverter implements GenericConverter { +final class ObjectToArrayConverter implements ConditionalGenericConverter { private final ConversionService conversionService; @@ -43,6 +43,10 @@ final class ObjectToArrayConverter implements GenericConverter { return Collections.singleton(new ConvertiblePair(Object.class, Object[].class)); } + public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) { + return true; + } + public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { if (source == null) { return null; 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 c35ac04861d..c26b8b4dfda 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 @@ -23,7 +23,7 @@ import java.util.Set; import org.springframework.core.CollectionFactory; import org.springframework.core.convert.ConversionService; import org.springframework.core.convert.TypeDescriptor; -import org.springframework.core.convert.converter.GenericConverter; +import org.springframework.core.convert.converter.ConditionalGenericConverter; /** * Converts an Object to a single-element Collection containing the Object. @@ -33,7 +33,7 @@ import org.springframework.core.convert.converter.GenericConverter; * @author Juergen Hoeller * @since 3.0 */ -final class ObjectToCollectionConverter implements GenericConverter { +final class ObjectToCollectionConverter implements ConditionalGenericConverter { private final ConversionService conversionService; @@ -45,6 +45,10 @@ final class ObjectToCollectionConverter implements GenericConverter { return Collections.singleton(new ConvertiblePair(Object.class, Collection.class)); } + public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) { + return true; + } + @SuppressWarnings("unchecked") public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { if (source == null) {