diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/converter/GenericConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/converter/GenericConverter.java
index d91f3ef37ff..52da8c3ca05 100644
--- a/org.springframework.core/src/main/java/org/springframework/core/convert/converter/GenericConverter.java
+++ b/org.springframework.core/src/main/java/org/springframework/core/convert/converter/GenericConverter.java
@@ -16,19 +16,26 @@
package org.springframework.core.convert.converter;
+import java.util.Set;
+
import org.springframework.core.convert.TypeDescriptor;
+import org.springframework.util.Assert;
/**
* Generic converter interface for converting between two or more types.
- *
- * This is the most flexible of the Converter SPI interfaces, but also the most complex.
- * It is flexible in that a GenericConverter may support converting between multiple source/target type pairs (see {@link #getConvertibleTypes()}.
- * In addition, GenericConverter implementations have access to source/target {@link TypeDescriptor field context} during the type conversion process.
- * This allows for resolving source and target field metadata such as annotations and generics information, which can be used influence the conversion logic.
- *
- * This interface should generally not be used when the simpler {@link Converter} or {@link ConverterFactory} interfaces are sufficient.
+ *
+ *
This is the most flexible of the Converter SPI interfaces, but also the most complex.
+ * It is flexible in that a GenericConverter may support converting between multiple source/target
+ * type pairs (see {@link #getConvertibleTypes()}. In addition, GenericConverter implementations
+ * have access to source/target {@link TypeDescriptor field context} during the type conversion process.
+ * This allows for resolving source and target field metadata such as annotations and generics
+ * information, which can be used influence the conversion logic.
+ *
+ *
This interface should generally not be used when the simpler {@link Converter} or
+ * {@link ConverterFactory} interfaces are sufficient.
*
* @author Keith Donald
+ * @author Juergen Hoeller
* @since 3.0
* @see TypeDescriptor
* @see Converter
@@ -37,11 +44,10 @@ import org.springframework.core.convert.TypeDescriptor;
public interface GenericConverter {
/**
- * The source and target types this converter can convert between.
- * Each entry in the returned array is a convertible source-to-target type pair, also expressed as an array.
- * For each pair, the first array element is a sourceType that can be converted from, and the second array element is a targetType that can be converted to.
+ * Return the source and target types which this converter can convert between.
+ *
Each entry is a convertible source-to-target type pair.
*/
- public Class>[][] getConvertibleTypes();
+ Set getConvertibleTypes();
/**
* Convert the source to the targetType described by the TypeDescriptor.
@@ -52,4 +58,35 @@ public interface GenericConverter {
*/
Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType);
+
+ /**
+ * Holder for a source-to-target class pair.
+ */
+ public static final class ConvertiblePair {
+
+ private final Class> sourceType;
+
+ private final Class> targetType;
+
+ /**
+ * Create a new source-to-target pair.
+ * @param sourceType the source type
+ * @param targetType the target type
+ */
+ public ConvertiblePair(Class> sourceType, Class> targetType) {
+ Assert.notNull(sourceType, "Source type must not be null");
+ Assert.notNull(targetType, "Target type must not be null");
+ this.sourceType = sourceType;
+ this.targetType = targetType;
+ }
+
+ public Class> getSourceType() {
+ return this.sourceType;
+ }
+
+ public Class> getTargetType() {
+ return this.targetType;
+ }
+ }
+
}
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 6fc38e79fe2..caf98954fc5 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
@@ -17,6 +17,8 @@
package org.springframework.core.convert.support;
import java.util.Arrays;
+import java.util.Collections;
+import java.util.Set;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.GenericConverter;
@@ -35,9 +37,9 @@ final class ArrayToArrayConverter implements GenericConverter {
public ArrayToArrayConverter(GenericConversionService conversionService) {
this.helperConverter = new CollectionToArrayConverter(conversionService);
}
-
- public Class>[][] getConvertibleTypes() {
- return new Class>[][] { { Object[].class, Object[].class } };
+
+ public Set getConvertibleTypes() {
+ return Collections.singleton(new ConvertiblePair(Object[].class, Object[].class));
}
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor 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 36566845473..9fd4dd856b6 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,15 +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.Collection;
+import java.util.Collections;
+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.GenericConverter;
+import static org.springframework.core.convert.support.ConversionUtils.*;
/**
* Converts from an array to a collection.
@@ -40,8 +41,8 @@ final class ArrayToCollectionConverter implements GenericConverter {
this.conversionService = conversionService;
}
- public Class>[][] getConvertibleTypes() {
- return new Class>[][] { { Object[].class, Collection.class } };
+ public Set getConvertibleTypes() {
+ return Collections.singleton(new ConvertiblePair(Object[].class, Collection.class));
}
@SuppressWarnings("unchecked")
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 5377c6facf0..5553dead0de 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
@@ -17,7 +17,9 @@
package org.springframework.core.convert.support;
import java.util.Arrays;
+import java.util.Collections;
import java.util.Map;
+import java.util.Set;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.GenericConverter;
@@ -37,8 +39,8 @@ final class ArrayToMapConverter implements GenericConverter {
this.helperConverter = new CollectionToMapConverter(conversionService);
}
- public Class>[][] getConvertibleTypes() {
- return new Class>[][] { { Object[].class, Map.class } };
+ public Set getConvertibleTypes() {
+ return Collections.singleton(new ConvertiblePair(Object[].class, Map.class));
}
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor 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 25914ce3903..b6fa1c1c48e 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
@@ -17,6 +17,8 @@
package org.springframework.core.convert.support;
import java.util.Arrays;
+import java.util.Collections;
+import java.util.Set;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.GenericConverter;
@@ -36,8 +38,8 @@ final class ArrayToObjectConverter implements GenericConverter {
this.helperConverter = new CollectionToObjectConverter(conversionService);
}
- public Class>[][] getConvertibleTypes() {
- return new Class>[][] {{Object[].class, Object.class}};
+ public Set getConvertibleTypes() {
+ return Collections.singleton(new ConvertiblePair(Object[].class, Object.class));
}
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor 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 93f0275ff2d..2dd0e5846f3 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,16 +16,16 @@
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;
import java.util.Iterator;
+import java.util.Set;
import org.springframework.core.convert.ConverterNotFoundException;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.GenericConverter;
+import static org.springframework.core.convert.support.ConversionUtils.*;
/**
* Converts from a Collection to an array.
@@ -41,8 +41,8 @@ final class CollectionToArrayConverter implements GenericConverter {
this.conversionService = conversionService;
}
- public Class>[][] getConvertibleTypes() {
- return new Class>[][] { { Collection.class, Object[].class } };
+ public Set getConvertibleTypes() {
+ return Collections.singleton(new ConvertiblePair(Collection.class, Object[].class));
}
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor 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 d177c6dcda3..1e4c5288803 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
@@ -16,15 +16,15 @@
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.CollectionFactory;
import org.springframework.core.convert.ConverterNotFoundException;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.GenericConverter;
+import static org.springframework.core.convert.support.ConversionUtils.*;
/**
* Converts from a source Collection to target Collection type.
@@ -40,8 +40,8 @@ final class CollectionToCollectionConverter implements GenericConverter {
this.conversionService = conversionService;
}
- public Class>[][] getConvertibleTypes() {
- return new Class>[][] { { Collection.class, Collection.class } };
+ public Set getConvertibleTypes() {
+ return Collections.singleton(new ConvertiblePair(Collection.class, Collection.class));
}
@SuppressWarnings("unchecked")
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 bd6c549d6f2..236b27e9b74 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,14 +16,15 @@
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;
+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.*;
/**
* Converts from a Collection to a Map.
@@ -39,8 +40,8 @@ final class CollectionToMapConverter implements GenericConverter {
this.conversionService = conversionService;
}
- public Class>[][] getConvertibleTypes() {
- return new Class>[][] { { Collection.class, Map.class } };
+ public Set getConvertibleTypes() {
+ return Collections.singleton(new ConvertiblePair(Collection.class, Map.class));
}
@SuppressWarnings("unchecked")
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 c5c8183d310..caf248509c7 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,14 @@
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.GenericConverter;
+import static org.springframework.core.convert.support.ConversionUtils.*;
/**
* Converts from a Collection to a single Object.
@@ -41,8 +41,8 @@ final class CollectionToObjectConverter implements GenericConverter {
this.conversionService = conversionService;
}
- public Class>[][] getConvertibleTypes() {
- return new Class>[][] { { Collection.class, Object.class } };
+ public Set getConvertibleTypes() {
+ return Collections.singleton(new ConvertiblePair(Collection.class, Object.class));
}
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ConversionServiceFactory.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ConversionServiceFactory.java
index 9e438b26a85..42261dfe3b8 100644
--- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ConversionServiceFactory.java
+++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ConversionServiceFactory.java
@@ -16,9 +16,16 @@
package org.springframework.core.convert.support;
+import java.util.Set;
+
+import org.springframework.core.convert.converter.Converter;
+import org.springframework.core.convert.converter.ConverterFactory;
+import org.springframework.core.convert.converter.ConverterRegistry;
+import org.springframework.core.convert.converter.GenericConverter;
/**
- * A factory for common ConversionService configurations.
+ * A factory for common {@link org.springframework.core.convert.ConversionService}
+ * configurations.
*
* @author Keith Donald
* @author Juergen Hoeller
@@ -67,4 +74,30 @@ public abstract class ConversionServiceFactory {
conversionService.addGenericConverter(new IdToEntityConverter(conversionService));
}
+ /**
+ * Register the given converter objects with the given target registry.
+ * @param converters the converter objects: implementing {@link Converter},
+ * {@link ConverterFactory}, or {@link GenericConverter}
+ * @param registry the target registry to register with
+ */
+ public static void registerConverters(Set