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/BindingPoint.java similarity index 95% rename from org.springframework.core/src/main/java/org/springframework/core/convert/TypeDescriptor.java rename to org.springframework.core/src/main/java/org/springframework/core/convert/BindingPoint.java index 0d2ef012c14..d079c1432a8 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/BindingPoint.java @@ -32,12 +32,12 @@ import org.springframework.util.Assert; * @author Keith Donald * @author Andy Clement */ -public class TypeDescriptor { +public class BindingPoint { /** * Constant value typeDescriptor for the type of a null value */ - public final static TypeDescriptor NULL_TYPE_DESCRIPTOR = new TypeDescriptor((Class) null); + public final static BindingPoint NULL_TYPE_DESCRIPTOR = new BindingPoint((Class) null); private MethodParameter methodParameter; @@ -52,7 +52,7 @@ public class TypeDescriptor { * a Map or collection, where no additional binding metadata is available. * @param type the actual type */ - public TypeDescriptor(Class type) { + public BindingPoint(Class type) { this.type = type; } @@ -61,7 +61,7 @@ public class TypeDescriptor { * from a method parameter, such as a setter method argument. * @param methodParameter the MethodParameter to wrap */ - public TypeDescriptor(MethodParameter methodParameter) { + public BindingPoint(MethodParameter methodParameter) { Assert.notNull(methodParameter, "MethodParameter must not be null"); this.methodParameter = methodParameter; } @@ -70,7 +70,7 @@ public class TypeDescriptor { * Create a new descriptor for a field. Use this constructor when a bound value originates from a field. * @param field the field to wrap */ - public TypeDescriptor(Field field) { + public BindingPoint(Field field) { Assert.notNull(field, "Field must not be null"); this.field = field; } @@ -248,7 +248,7 @@ public class TypeDescriptor { * @param targetType the target type * @return true if this type is assignable to the target */ - public boolean isAssignableTo(TypeDescriptor targetType) { + public boolean isAssignableTo(BindingPoint targetType) { return targetType.getType().isAssignableFrom(getType()); } @@ -257,9 +257,9 @@ public class TypeDescriptor { * @param type the class * @return the type descriptor */ - public static TypeDescriptor valueOf(Class type) { + public static BindingPoint valueOf(Class type) { // TODO needs a cache for common type descriptors - return new TypeDescriptor(type); + return new BindingPoint(type); } /** @@ -267,7 +267,7 @@ public class TypeDescriptor { * @param object the object * @return the type descriptor */ - public static TypeDescriptor forObject(Object object) { + public static BindingPoint forObject(Object object) { if (object == null) { return NULL_TYPE_DESCRIPTOR; } else { diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/ConversionException.java b/org.springframework.core/src/main/java/org/springframework/core/convert/ConversionFailedException.java similarity index 89% rename from org.springframework.core/src/main/java/org/springframework/core/convert/ConversionException.java rename to org.springframework.core/src/main/java/org/springframework/core/convert/ConversionFailedException.java index bb0ba912310..e821be4b8e1 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/ConversionException.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/ConversionFailedException.java @@ -22,7 +22,7 @@ import org.springframework.core.style.StylerUtils; * * @author Keith Donald */ -public class ConversionException extends ConvertException { +public class ConversionFailedException extends ConvertException { private transient Object value; @@ -37,7 +37,7 @@ public class ConversionException extends ConvertException { * @param targetType the value's target type * @param cause the cause of the conversion failure */ - public ConversionException(Object value, Class sourceType, Class targetType, Throwable cause) { + public ConversionFailedException(Object value, Class sourceType, Class targetType, Throwable cause) { super(defaultMessage(value, sourceType, targetType, cause), cause); this.value = value; this.sourceType = sourceType; @@ -51,7 +51,7 @@ public class ConversionException extends ConvertException { * @param targetType the value's target type * @param message a descriptive message of what went wrong. */ - public ConversionException(Object value, Class sourceType, Class targetType, String message) { + public ConversionFailedException(Object value, Class sourceType, Class targetType, String message) { super(message); this.value = value; this.sourceType = sourceType; diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/TypeConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/TypeConverter.java index fba41258b9c..ac3bfb18eef 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/TypeConverter.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/TypeConverter.java @@ -18,7 +18,7 @@ package org.springframework.core.convert; /** * A service interface for type conversion. This is the entry point into the convert system. *

- * Call {@link #convert(Object, TypeDescriptor)} to perform a thread-safe type conversion using + * Call {@link #convert(Object, Class)} to perform a thread-safe type conversion using * this system. * * @author Keith Donald @@ -28,19 +28,35 @@ public interface TypeConverter { /** * Returns true if objects of sourceType can be converted to targetType. * @param source the source to convert from (may be null) - * @param targetType context about the target type to convert to + * @param targetType the target type to convert to * @return true if a conversion can be performed, false if not */ - boolean canConvert(Class sourceType, TypeDescriptor targetType); + boolean canConvert(Class sourceType, Class targetType); + + /** + * Returns true if objects of sourceType can be converted to the type of the binding point. + * @param source the source to convert from (may be null) + * @param point context about the target type to convert to + * @return true if a conversion can be performed, false if not + */ + boolean canConvert(Class sourceType, BindingPoint point); /** * Convert the source to targetType. * @param source the source to convert from (may be null) - * @param targetType context about the target type to convert to - * @return the converted object, an instance of {@link TypeDescriptor#getType()}, or null if a null source - * was provided + * @param targetType the target type to convert to + * @return the converted object, an instance of targetType, or null if a null source was provided * @throws ConvertException if an exception occurred */ - T convert(S source, TypeDescriptor targetType); + T convert(S source, Class targetType); + + /** + * Convert the source to type T needed by the binding point. + * @param source the source to convert from (may be null) + * @param point a binding point where a conversion is required + * @return the converted object, an instance of {@link BindingPoint#getType()}, or null if a null source was provided + * @throws ConvertException if an exception occurred + */ + T convert(S source, BindingPoint point); } \ No newline at end of file diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/converter/ConverterRegistry.java b/org.springframework.core/src/main/java/org/springframework/core/convert/converter/ConverterRegistry.java new file mode 100644 index 00000000000..440ad97fce7 --- /dev/null +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/converter/ConverterRegistry.java @@ -0,0 +1,12 @@ +package org.springframework.core.convert.converter; + +public interface ConverterRegistry { + void addConverter(Converter converter); + + void addConverterFactory(ConverterFactory converter); + + void removeConverter(Converter converter); + + void removeConverterFactory(Converter converter); + +} \ No newline at end of file diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/AbstractCollectionConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/AbstractCollectionConverter.java index 3c546d1a0da..0ced2fadc81 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/AbstractCollectionConverter.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/AbstractCollectionConverter.java @@ -15,8 +15,8 @@ */ package org.springframework.core.convert.support; -import org.springframework.core.convert.ConversionException; -import org.springframework.core.convert.TypeDescriptor; +import org.springframework.core.convert.ConversionFailedException; +import org.springframework.core.convert.BindingPoint; /** * Base class for converters that convert to and from collection types (arrays and java.util.Collection types) @@ -24,22 +24,22 @@ import org.springframework.core.convert.TypeDescriptor; */ abstract class AbstractCollectionConverter implements ConversionExecutor { - private GenericConversionService conversionService; + private GenericTypeConverter conversionService; private ConversionExecutor elementConverter; - private TypeDescriptor sourceCollectionType; + private BindingPoint sourceCollectionType; - private TypeDescriptor targetCollectionType; + private BindingPoint targetCollectionType; - public AbstractCollectionConverter(TypeDescriptor sourceCollectionType, TypeDescriptor targetCollectionType, GenericConversionService conversionService) { + public AbstractCollectionConverter(BindingPoint sourceCollectionType, BindingPoint targetCollectionType, GenericTypeConverter conversionService) { this.conversionService = conversionService; this.sourceCollectionType = sourceCollectionType; this.targetCollectionType = targetCollectionType; Class sourceElementType = sourceCollectionType.getElementType(); Class targetElementType = targetCollectionType.getElementType(); if (sourceElementType != null && targetElementType != null) { - elementConverter = conversionService.getConversionExecutor(sourceElementType, TypeDescriptor.valueOf(targetElementType)); + elementConverter = conversionService.getConversionExecutor(sourceElementType, BindingPoint.valueOf(targetElementType)); } else { elementConverter = NoOpConversionExecutor.INSTANCE; } @@ -59,7 +59,7 @@ abstract class AbstractCollectionConverter implements ConversionExecutor { return targetCollectionType.getElementType(); } - protected GenericConversionService getConversionService() { + protected GenericTypeConverter getConversionService() { return conversionService; } @@ -75,7 +75,7 @@ abstract class AbstractCollectionConverter implements ConversionExecutor { try { return doExecute(source); } catch (Exception e) { - throw new ConversionException(source, sourceCollectionType.getType(), targetCollectionType.getType(), e); + throw new ConversionFailedException(source, sourceCollectionType.getType(), targetCollectionType.getType(), e); } } diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToArray.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToArray.java index 4ec9b2e77e5..ef2dd3f9c28 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToArray.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToArray.java @@ -18,7 +18,7 @@ package org.springframework.core.convert.support; import java.lang.reflect.Array; import org.springframework.core.convert.TypeConverter; -import org.springframework.core.convert.TypeDescriptor; +import org.springframework.core.convert.BindingPoint; /** * Special one-way converter that converts from a source array to a target array. Supports type conversion of the @@ -29,7 +29,7 @@ import org.springframework.core.convert.TypeDescriptor; */ class ArrayToArray extends AbstractCollectionConverter { - public ArrayToArray(TypeDescriptor sourceArrayType, TypeDescriptor targetArrayType, GenericConversionService conversionService) { + public ArrayToArray(BindingPoint sourceArrayType, BindingPoint targetArrayType, GenericTypeConverter conversionService) { super(sourceArrayType, targetArrayType, conversionService); } diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToCollection.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToCollection.java index b01566c15ec..fb1e3f9bba3 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToCollection.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToCollection.java @@ -18,7 +18,7 @@ package org.springframework.core.convert.support; import java.lang.reflect.Array; import java.util.Collection; -import org.springframework.core.convert.TypeDescriptor; +import org.springframework.core.convert.BindingPoint; /** * Special converter that converts from a source array to a target collection. Supports the selection of an @@ -29,8 +29,8 @@ import org.springframework.core.convert.TypeDescriptor; */ class ArrayToCollection extends AbstractCollectionConverter { - public ArrayToCollection(TypeDescriptor sourceArrayType, TypeDescriptor targetCollectionType, - GenericConversionService conversionService) { + public ArrayToCollection(BindingPoint sourceArrayType, BindingPoint targetCollectionType, + GenericTypeConverter conversionService) { super(sourceArrayType, targetCollectionType, conversionService); } diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToArray.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToArray.java index 61aae61ef6c..255e2b85b95 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToArray.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToArray.java @@ -19,7 +19,7 @@ import java.lang.reflect.Array; import java.util.Collection; import java.util.Iterator; -import org.springframework.core.convert.TypeDescriptor; +import org.springframework.core.convert.BindingPoint; /** * Special converter that converts from target collection to a source array. @@ -28,8 +28,8 @@ import org.springframework.core.convert.TypeDescriptor; */ class CollectionToArray extends AbstractCollectionConverter { - public CollectionToArray(TypeDescriptor sourceArrayType, TypeDescriptor targetCollectionType, - GenericConversionService conversionService) { + public CollectionToArray(BindingPoint sourceArrayType, BindingPoint targetCollectionType, + GenericTypeConverter conversionService) { super(sourceArrayType, targetCollectionType, conversionService); } @@ -52,7 +52,7 @@ class CollectionToArray extends AbstractCollectionConverter { while (it.hasNext()) { Object value = it.next(); if (value != null) { - elementConverter = getConversionService().getConversionExecutor(value.getClass(), TypeDescriptor.valueOf(getTargetElementType())); + elementConverter = getConversionService().getConversionExecutor(value.getClass(), BindingPoint.valueOf(getTargetElementType())); break; } } diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToCollection.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToCollection.java index 3e715cb1227..8d5a72ec6cd 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToCollection.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToCollection.java @@ -18,7 +18,7 @@ package org.springframework.core.convert.support; import java.util.Collection; import java.util.Iterator; -import org.springframework.core.convert.TypeDescriptor; +import org.springframework.core.convert.BindingPoint; /** * A converter that can convert from one collection type to another. @@ -27,8 +27,8 @@ import org.springframework.core.convert.TypeDescriptor; */ class CollectionToCollection extends AbstractCollectionConverter { - public CollectionToCollection(TypeDescriptor sourceCollectionType, TypeDescriptor targetCollectionType, - GenericConversionService conversionService) { + public CollectionToCollection(BindingPoint sourceCollectionType, BindingPoint targetCollectionType, + GenericTypeConverter conversionService) { super(sourceCollectionType, targetCollectionType, conversionService); } @@ -53,7 +53,7 @@ class CollectionToCollection extends AbstractCollectionConverter { while (it.hasNext()) { Object value = it.next(); if (value != null) { - elementConverter = getConversionService().getConversionExecutor(value.getClass(), TypeDescriptor.valueOf(getTargetElementType())); + elementConverter = getConversionService().getConversionExecutor(value.getClass(), BindingPoint.valueOf(getTargetElementType())); break; } } diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ConversionExecutor.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ConversionExecutor.java index 283f1ef858b..93e89d7c215 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ConversionExecutor.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ConversionExecutor.java @@ -15,7 +15,7 @@ */ package org.springframework.core.convert.support; -import org.springframework.core.convert.ConversionException; +import org.springframework.core.convert.ConversionFailedException; /** * A command parameterized with the information necessary to perform a conversion of a source input to a @@ -29,7 +29,7 @@ public interface ConversionExecutor { /** * Convert the source. * @param source the source to convert - * @throws ConversionException if an exception occurs during type conversion + * @throws ConversionFailedException if an exception occurs during type conversion */ public Object execute(Object source); diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/DefaultConversionService.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/DefaultTypeConverter.java similarity index 86% rename from org.springframework.core/src/main/java/org/springframework/core/convert/support/DefaultConversionService.java rename to org.springframework.core/src/main/java/org/springframework/core/convert/support/DefaultTypeConverter.java index 18bbf942c26..bd9402b867f 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/DefaultConversionService.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/DefaultTypeConverter.java @@ -15,7 +15,6 @@ */ package org.springframework.core.convert.support; -import org.springframework.core.convert.support.StringToEnumFactory.StringToEnum; /** * Default implementation of a conversion service. Will automatically register from string converters for @@ -23,12 +22,12 @@ import org.springframework.core.convert.support.StringToEnumFactory.StringToEnum * * @author Keith Donald */ -public class DefaultConversionService extends GenericConversionService { +public class DefaultTypeConverter extends GenericTypeConverter { /** * Creates a new default conversion service, installing the default converters. */ - public DefaultConversionService() { + public DefaultTypeConverter() { addDefaultConverters(); } @@ -49,6 +48,9 @@ public class DefaultConversionService extends GenericConversionService { addConverter(new StringToLocale()); addConverter(new NumberToCharacter()); addConverter(new ObjectToString()); + addConverterFactory(new StringToEnumFactory()); + addConverterFactory(new NumberToNumberFactory()); + addConverterFactory(new CharacterToNumberFactory()); } } \ No newline at end of file diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/GenericTypeConverter.java similarity index 89% rename from org.springframework.core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java rename to org.springframework.core/src/main/java/org/springframework/core/convert/support/GenericTypeConverter.java index 0e4d29f31a5..57eabf76450 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/GenericTypeConverter.java @@ -28,9 +28,11 @@ import java.util.Map; import org.springframework.core.GenericTypeResolver; import org.springframework.core.convert.ConverterNotFoundException; import org.springframework.core.convert.TypeConverter; -import org.springframework.core.convert.TypeDescriptor; +import org.springframework.core.convert.BindingPoint; import org.springframework.core.convert.converter.Converter; +import org.springframework.core.convert.converter.ConverterFactory; import org.springframework.core.convert.converter.ConverterInfo; +import org.springframework.core.convert.converter.ConverterRegistry; import org.springframework.util.Assert; /** @@ -43,7 +45,7 @@ import org.springframework.util.Assert; * @author Keith Donald */ @SuppressWarnings("unchecked") -public class GenericConversionService implements TypeConverter { +public class GenericTypeConverter implements TypeConverter, ConverterRegistry { /** * An indexed map of Converters. Each Map.Entry key is a source class (S) that can be converted from. Each Map.Entry @@ -82,10 +84,24 @@ public class GenericConversionService implements TypeConverter { Map sourceMap = getSourceMap(sourceType); sourceMap.put(targetType, converter); } + + public void addConverterFactory(ConverterFactory converter) { + } + + public void removeConverter(Converter converter) { + } + + public void removeConverterFactory(Converter converter) { + } // implementing ConversionService - public boolean canConvert(Class sourceType, TypeDescriptor targetType) { + + public boolean canConvert(Class sourceType, Class targetType) { + return canConvert(sourceType, BindingPoint.valueOf(targetType)); + } + + public boolean canConvert(Class sourceType, BindingPoint targetType) { ConversionExecutor executor = getConversionExecutor(sourceType, targetType); if (executor != null) { return true; @@ -98,16 +114,20 @@ public class GenericConversionService implements TypeConverter { } } - public Object convert(Object source, TypeDescriptor targetType) { + public T convert(S source, Class targetType) { + return convert(source, BindingPoint.valueOf(targetType)); + } + + public T convert(S source, BindingPoint targetType) { if (source == null) { return null; } if (source.getClass().isAssignableFrom(targetType.getType())) { - return source; + return (T) source; } ConversionExecutor executor = getConversionExecutor(source.getClass(), targetType); if (executor != null) { - return executor.execute(source); + return (T) executor.execute(source); } else { if (parent != null) { return parent.convert(source, targetType); @@ -119,11 +139,11 @@ public class GenericConversionService implements TypeConverter { } } - ConversionExecutor getConversionExecutor(Class sourceClass, TypeDescriptor targetType) + ConversionExecutor getConversionExecutor(Class sourceClass, BindingPoint targetType) throws ConverterNotFoundException { Assert.notNull(sourceClass, "The sourceType to convert from is required"); Assert.notNull(targetType, "The targetType to convert to is required"); - TypeDescriptor sourceType = TypeDescriptor.valueOf(sourceClass); + BindingPoint sourceType = BindingPoint.valueOf(sourceClass); if (sourceType.isArray()) { if (targetType.isArray()) { return new ArrayToArray(sourceType, targetType, this); diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/MapToMap.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/MapToMap.java index 5925d195fdb..e6c52a37d49 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/MapToMap.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/MapToMap.java @@ -21,20 +21,21 @@ import java.util.Map; import java.util.SortedMap; import java.util.TreeMap; -import org.springframework.core.convert.ConversionException; -import org.springframework.core.convert.TypeDescriptor; +import org.springframework.core.convert.ConversionFailedException; +import org.springframework.core.convert.BindingPoint; /** * Converts from one map to another map, with support for converting individual map elements based on generic type information. * @author Keith Donald */ +@SuppressWarnings("unchecked") class MapToMap implements ConversionExecutor { - private TypeDescriptor sourceType; + private BindingPoint sourceType; - private TypeDescriptor targetType; + private BindingPoint targetType; - private GenericConversionService conversionService; + private GenericTypeConverter conversionService; private EntryConverter entryConverter; @@ -44,7 +45,7 @@ class MapToMap implements ConversionExecutor { * @param targetType the target map type * @param conversionService the conversion service */ - public MapToMap(TypeDescriptor sourceType, TypeDescriptor targetType, GenericConversionService conversionService) { + public MapToMap(BindingPoint sourceType, BindingPoint targetType, GenericTypeConverter conversionService) { this.sourceType = sourceType; this.targetType = targetType; this.conversionService = conversionService; @@ -54,17 +55,16 @@ class MapToMap implements ConversionExecutor { private EntryConverter createEntryConverter() { if (sourceType.isMapEntryTypeKnown() && targetType.isMapEntryTypeKnown()) { ConversionExecutor keyConverter = conversionService.getConversionExecutor(sourceType.getMapKeyType(), - TypeDescriptor.valueOf(targetType.getMapKeyType())); + BindingPoint.valueOf(targetType.getMapKeyType())); ConversionExecutor valueConverter = conversionService.getConversionExecutor(sourceType.getMapValueType(), - TypeDescriptor.valueOf(targetType.getMapValueType())); + BindingPoint.valueOf(targetType.getMapValueType())); return new EntryConverter(keyConverter, valueConverter); } else { return EntryConverter.NO_OP_INSTANCE; } } - @SuppressWarnings("unchecked") - public Object execute(Object source) throws ConversionException { + public Object execute(Object source) throws ConversionFailedException { try { Map map = (Map) source; Map targetMap = (Map) getImpl(targetType.getType()).newInstance(); @@ -76,7 +76,7 @@ class MapToMap implements ConversionExecutor { } return targetMap; } catch (Exception e) { - throw new ConversionException(source, sourceType.getType(), targetType.getType(), e); + throw new ConversionFailedException(source, sourceType.getType(), targetType.getType(), e); } } @@ -94,11 +94,11 @@ class MapToMap implements ConversionExecutor { Object key = entry.getKey(); Object value = entry.getValue(); if (keyConverter == null && key != null) { - keyConverter = conversionService.getConversionExecutor(key.getClass(), TypeDescriptor + keyConverter = conversionService.getConversionExecutor(key.getClass(), BindingPoint .valueOf(targetKeyType)); } if (valueConverter == null && value != null) { - valueConverter = conversionService.getConversionExecutor(value.getClass(), TypeDescriptor + valueConverter = conversionService.getConversionExecutor(value.getClass(), BindingPoint .valueOf(targetValueType)); } if (keyConverter != null && valueConverter != null) { diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/NoOpConversionExecutor.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/NoOpConversionExecutor.java index 0295ec891c5..12e533480d3 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/NoOpConversionExecutor.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/NoOpConversionExecutor.java @@ -15,7 +15,7 @@ */ package org.springframework.core.convert.support; -import org.springframework.core.convert.ConversionException; +import org.springframework.core.convert.ConversionFailedException; /** * Conversion executor that does nothing. Access singleton at {@link #INSTANCE}.s @@ -28,7 +28,7 @@ class NoOpConversionExecutor implements ConversionExecutor { } - public Object execute(Object source) throws ConversionException { + public Object execute(Object source) throws ConversionFailedException { return source; } diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToString.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToString.java index e34ae930682..b2dd3461568 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToString.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToString.java @@ -4,7 +4,7 @@ import org.springframework.core.convert.converter.Converter; /** * Simply calls {@link Object#toString()} to convert any object to a string. - * Used by the {@link DefaultConversionService} as a fallback if there are no other explicit to string converters registered. + * Used by the {@link DefaultTypeConverter} as a fallback if there are no other explicit to string converters registered. * @author Keith Donald */ public class ObjectToString implements Converter { diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/StaticConversionExecutor.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/StaticConversionExecutor.java index 9facd0d5a09..1a15e4aec3c 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/StaticConversionExecutor.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/StaticConversionExecutor.java @@ -15,8 +15,8 @@ */ package org.springframework.core.convert.support; -import org.springframework.core.convert.ConversionException; -import org.springframework.core.convert.TypeDescriptor; +import org.springframework.core.convert.ConversionFailedException; +import org.springframework.core.convert.BindingPoint; import org.springframework.core.convert.converter.Converter; import org.springframework.core.style.ToStringCreator; @@ -27,30 +27,30 @@ import org.springframework.core.style.ToStringCreator; @SuppressWarnings("unchecked") class StaticConversionExecutor implements ConversionExecutor { - private final TypeDescriptor sourceType; + private final BindingPoint sourceType; - private final TypeDescriptor targetType; + private final BindingPoint targetType; private final Converter converter; - public StaticConversionExecutor(TypeDescriptor sourceType, TypeDescriptor targetType, Converter converter) { + public StaticConversionExecutor(BindingPoint sourceType, BindingPoint targetType, Converter converter) { this.sourceType = sourceType; this.targetType = targetType; this.converter = converter; } - public Object execute(Object source) throws ConversionException { + public Object execute(Object source) throws ConversionFailedException { if (source == null) { return null; } if (sourceType != null && !sourceType.isInstance(source)) { - throw new ConversionException(source, sourceType.getType(), targetType.getType(), "Source object " + throw new ConversionFailedException(source, sourceType.getType(), targetType.getType(), "Source object " + source + " to convert is expected to be an instance of [" + sourceType.getName() + "]"); } try { return converter.convert(source); } catch (Exception e) { - throw new ConversionException(source, sourceType.getType(), targetType.getType(), e); + throw new ConversionFailedException(source, sourceType.getType(), targetType.getType(), e); } } diff --git a/org.springframework.core/src/test/java/org/springframework/core/convert/TypeDescriptorTests.java b/org.springframework.core/src/test/java/org/springframework/core/convert/TypeDescriptorTests.java index a4447fedc3a..104c2c4cab1 100644 --- a/org.springframework.core/src/test/java/org/springframework/core/convert/TypeDescriptorTests.java +++ b/org.springframework.core/src/test/java/org/springframework/core/convert/TypeDescriptorTests.java @@ -34,13 +34,13 @@ public class TypeDescriptorTests { @Test public void testWrapperType() { - TypeDescriptor desc = TypeDescriptor.valueOf(int.class); + BindingPoint desc = BindingPoint.valueOf(int.class); assertEquals(Integer.class, desc.getType()); } @Test public void listDescriptors() throws Exception { - TypeDescriptor typeDescriptor = new TypeDescriptor(TypeDescriptorTests.class.getDeclaredField("listOfString")); + BindingPoint typeDescriptor = new BindingPoint(TypeDescriptorTests.class.getDeclaredField("listOfString")); assertFalse(typeDescriptor.isArray()); assertEquals(List.class,typeDescriptor.getType()); assertEquals(String.class,typeDescriptor.getElementType()); @@ -50,7 +50,7 @@ public class TypeDescriptorTests { @Test public void arrayTypeDescriptors() throws Exception { - TypeDescriptor typeDescriptor = new TypeDescriptor(TypeDescriptorTests.class.getDeclaredField("intArray")); + BindingPoint typeDescriptor = new BindingPoint(TypeDescriptorTests.class.getDeclaredField("intArray")); assertTrue(typeDescriptor.isArray()); assertEquals(Integer.TYPE,typeDescriptor.getElementType()); assertEquals("int[]",typeDescriptor.asString()); @@ -58,14 +58,14 @@ public class TypeDescriptorTests { @Test public void buildingArrayTypeDescriptors() throws Exception { - TypeDescriptor typeDescriptor = new TypeDescriptor(new int[0].getClass()); + BindingPoint typeDescriptor = new BindingPoint(new int[0].getClass()); assertTrue(typeDescriptor.isArray()); assertEquals(Integer.TYPE,typeDescriptor.getElementType()); } @Test public void complexTypeDescriptors() throws Exception { - TypeDescriptor typeDescriptor = new TypeDescriptor(TypeDescriptorTests.class.getDeclaredField("arrayOfListOfString")); + BindingPoint typeDescriptor = new BindingPoint(TypeDescriptorTests.class.getDeclaredField("arrayOfListOfString")); assertTrue(typeDescriptor.isArray()); assertEquals(List.class,typeDescriptor.getElementType()); // TODO asc notice that the type of the list elements is lost: typeDescriptor.getElementType() should return a TypeDescriptor diff --git a/org.springframework.core/src/test/java/org/springframework/core/convert/support/ArrayToArrayTests.java b/org.springframework.core/src/test/java/org/springframework/core/convert/support/ArrayToArrayTests.java index 727bada6b27..a2dfad6f6ed 100644 --- a/org.springframework.core/src/test/java/org/springframework/core/convert/support/ArrayToArrayTests.java +++ b/org.springframework.core/src/test/java/org/springframework/core/convert/support/ArrayToArrayTests.java @@ -3,16 +3,16 @@ package org.springframework.core.convert.support; import static org.junit.Assert.assertEquals; import org.junit.Test; -import org.springframework.core.convert.TypeDescriptor; +import org.springframework.core.convert.BindingPoint; import org.springframework.core.convert.support.ArrayToArray; -import org.springframework.core.convert.support.DefaultConversionService; +import org.springframework.core.convert.support.DefaultTypeConverter; public class ArrayToArrayTests { @Test public void testArrayToArrayConversion() { - DefaultConversionService service = new DefaultConversionService(); - ArrayToArray c = new ArrayToArray(TypeDescriptor.valueOf(String[].class), TypeDescriptor.valueOf(Integer[].class), service); + DefaultTypeConverter service = new DefaultTypeConverter(); + ArrayToArray c = new ArrayToArray(BindingPoint.valueOf(String[].class), BindingPoint.valueOf(Integer[].class), service); Integer[] result = (Integer[]) c.execute(new String[] { "1", "2", "3" }); assertEquals(new Integer(1), result[0]); assertEquals(new Integer(2), result[1]); diff --git a/org.springframework.core/src/test/java/org/springframework/core/convert/support/ArrayToCollectionTests.java b/org.springframework.core/src/test/java/org/springframework/core/convert/support/ArrayToCollectionTests.java index 2b5abfaa08f..6c90280e278 100644 --- a/org.springframework.core/src/test/java/org/springframework/core/convert/support/ArrayToCollectionTests.java +++ b/org.springframework.core/src/test/java/org/springframework/core/convert/support/ArrayToCollectionTests.java @@ -9,16 +9,16 @@ import java.util.Set; import java.util.SortedSet; import org.junit.Test; -import org.springframework.core.convert.TypeDescriptor; +import org.springframework.core.convert.BindingPoint; import org.springframework.core.convert.support.ArrayToCollection; -import org.springframework.core.convert.support.DefaultConversionService; +import org.springframework.core.convert.support.DefaultTypeConverter; public class ArrayToCollectionTests { @Test public void testArrayToCollectionConversion() throws Exception { - DefaultConversionService service = new DefaultConversionService(); - ArrayToCollection c = new ArrayToCollection(TypeDescriptor.valueOf(String[].class), new TypeDescriptor(getClass().getField("bindTarget")), service); + DefaultTypeConverter service = new DefaultTypeConverter(); + ArrayToCollection c = new ArrayToCollection(BindingPoint.valueOf(String[].class), new BindingPoint(getClass().getField("bindTarget")), service); List result = (List) c.execute(new String[] { "1", "2", "3" }); assertEquals(new Integer(1), result.get(0)); assertEquals(new Integer(2), result.get(1)); @@ -27,32 +27,32 @@ public class ArrayToCollectionTests { @Test public void testArrayToSetConversion() throws Exception { - DefaultConversionService service = new DefaultConversionService(); - ArrayToCollection c = new ArrayToCollection(TypeDescriptor.valueOf(String[].class), new TypeDescriptor(getClass().getField("setTarget")), service); + DefaultTypeConverter service = new DefaultTypeConverter(); + ArrayToCollection c = new ArrayToCollection(BindingPoint.valueOf(String[].class), new BindingPoint(getClass().getField("setTarget")), service); Set result = (Set) c.execute(new String[] { "1" }); assertEquals("1", result.iterator().next()); } @Test public void testArrayToSortedSetConversion() throws Exception { - DefaultConversionService service = new DefaultConversionService(); - ArrayToCollection c = new ArrayToCollection(TypeDescriptor.valueOf(String[].class), new TypeDescriptor(getClass().getField("sortedSetTarget")), service); + DefaultTypeConverter service = new DefaultTypeConverter(); + ArrayToCollection c = new ArrayToCollection(BindingPoint.valueOf(String[].class), new BindingPoint(getClass().getField("sortedSetTarget")), service); SortedSet result = (SortedSet) c.execute(new String[] { "1" }); assertEquals(new Integer(1), result.iterator().next()); } @Test public void testArrayToCollectionImplConversion() throws Exception { - DefaultConversionService service = new DefaultConversionService(); - ArrayToCollection c = new ArrayToCollection(TypeDescriptor.valueOf(String[].class), new TypeDescriptor(getClass().getField("implTarget")), service); + DefaultTypeConverter service = new DefaultTypeConverter(); + ArrayToCollection c = new ArrayToCollection(BindingPoint.valueOf(String[].class), new BindingPoint(getClass().getField("implTarget")), service); LinkedList result = (LinkedList) c.execute(new String[] { "1" }); assertEquals("1", result.iterator().next()); } @Test public void testArrayToNonGenericCollectionConversionNullElement() throws Exception { - DefaultConversionService service = new DefaultConversionService(); - ArrayToCollection c = new ArrayToCollection(TypeDescriptor.valueOf(String[].class), new TypeDescriptor(getClass().getField("listTarget")), service); + DefaultTypeConverter service = new DefaultTypeConverter(); + ArrayToCollection c = new ArrayToCollection(BindingPoint.valueOf(String[].class), new BindingPoint(getClass().getField("listTarget")), service); List result = (List) c.execute(new Integer[] { null, new Integer(1) }); assertEquals(null, result.get(0)); assertEquals(new Integer(1), result.get(1)); diff --git a/org.springframework.core/src/test/java/org/springframework/core/convert/support/CollectionToArrayTests.java b/org.springframework.core/src/test/java/org/springframework/core/convert/support/CollectionToArrayTests.java index 5c44f79a30e..b1824d007fc 100644 --- a/org.springframework.core/src/test/java/org/springframework/core/convert/support/CollectionToArrayTests.java +++ b/org.springframework.core/src/test/java/org/springframework/core/convert/support/CollectionToArrayTests.java @@ -6,17 +6,17 @@ import java.util.ArrayList; import java.util.Collection; import org.junit.Test; -import org.springframework.core.convert.TypeDescriptor; +import org.springframework.core.convert.BindingPoint; import org.springframework.core.convert.support.CollectionToArray; -import org.springframework.core.convert.support.DefaultConversionService; +import org.springframework.core.convert.support.DefaultTypeConverter; public class CollectionToArrayTests { @Test public void testCollectionToArrayConversion() throws Exception { - DefaultConversionService service = new DefaultConversionService(); - CollectionToArray c = new CollectionToArray(new TypeDescriptor(getClass().getField("bindTarget")), - TypeDescriptor.valueOf(Integer[].class), service); + DefaultTypeConverter service = new DefaultTypeConverter(); + CollectionToArray c = new CollectionToArray(new BindingPoint(getClass().getField("bindTarget")), + BindingPoint.valueOf(Integer[].class), service); bindTarget.add("1"); bindTarget.add("2"); bindTarget.add("3"); @@ -28,8 +28,8 @@ public class CollectionToArrayTests { @Test public void testCollectionToArrayConversionNoGenericInfo() throws Exception { - DefaultConversionService service = new DefaultConversionService(); - CollectionToArray c = new CollectionToArray(TypeDescriptor.valueOf(Collection.class), TypeDescriptor + DefaultTypeConverter service = new DefaultTypeConverter(); + CollectionToArray c = new CollectionToArray(BindingPoint.valueOf(Collection.class), BindingPoint .valueOf(Integer[].class), service); bindTarget.add("1"); bindTarget.add("2"); @@ -42,8 +42,8 @@ public class CollectionToArrayTests { @Test public void testCollectionToArrayConversionNoGenericInfoNullElement() throws Exception { - DefaultConversionService service = new DefaultConversionService(); - CollectionToArray c = new CollectionToArray(TypeDescriptor.valueOf(Collection.class), TypeDescriptor + DefaultTypeConverter service = new DefaultTypeConverter(); + CollectionToArray c = new CollectionToArray(BindingPoint.valueOf(Collection.class), BindingPoint .valueOf(Integer[].class), service); bindTarget.add(null); bindTarget.add("1"); diff --git a/org.springframework.core/src/test/java/org/springframework/core/convert/support/CollectionToCollectionTests.java b/org.springframework.core/src/test/java/org/springframework/core/convert/support/CollectionToCollectionTests.java index e6d90e6944c..8b90c3b64b1 100644 --- a/org.springframework.core/src/test/java/org/springframework/core/convert/support/CollectionToCollectionTests.java +++ b/org.springframework.core/src/test/java/org/springframework/core/convert/support/CollectionToCollectionTests.java @@ -8,17 +8,17 @@ import java.util.Collection; import java.util.List; import org.junit.Test; -import org.springframework.core.convert.TypeDescriptor; +import org.springframework.core.convert.BindingPoint; import org.springframework.core.convert.support.CollectionToCollection; -import org.springframework.core.convert.support.DefaultConversionService; +import org.springframework.core.convert.support.DefaultTypeConverter; public class CollectionToCollectionTests { @Test public void testCollectionToCollectionConversion() throws Exception { - DefaultConversionService service = new DefaultConversionService(); - CollectionToCollection c = new CollectionToCollection(new TypeDescriptor(getClass().getField("bindTarget")), - new TypeDescriptor(getClass().getField("integerTarget")), service); + DefaultTypeConverter service = new DefaultTypeConverter(); + CollectionToCollection c = new CollectionToCollection(new BindingPoint(getClass().getField("bindTarget")), + new BindingPoint(getClass().getField("integerTarget")), service); bindTarget.add("1"); bindTarget.add("2"); bindTarget.add("3"); @@ -30,9 +30,9 @@ public class CollectionToCollectionTests { @Test public void testCollectionToCollectionConversionNoGenericInfo() throws Exception { - DefaultConversionService service = new DefaultConversionService(); - CollectionToCollection c = new CollectionToCollection(TypeDescriptor.valueOf(Collection.class), - TypeDescriptor.valueOf(List.class), service); + DefaultTypeConverter service = new DefaultTypeConverter(); + CollectionToCollection c = new CollectionToCollection(BindingPoint.valueOf(Collection.class), + BindingPoint.valueOf(List.class), service); bindTarget.add("1"); bindTarget.add("2"); bindTarget.add("3"); @@ -44,9 +44,9 @@ public class CollectionToCollectionTests { @Test public void testCollectionToCollectionConversionNoGenericInfoSource() throws Exception { - DefaultConversionService service = new DefaultConversionService(); - CollectionToCollection c = new CollectionToCollection(TypeDescriptor.valueOf(Collection.class), - new TypeDescriptor(getClass().getField("integerTarget")), service); + DefaultTypeConverter service = new DefaultTypeConverter(); + CollectionToCollection c = new CollectionToCollection(BindingPoint.valueOf(Collection.class), + new BindingPoint(getClass().getField("integerTarget")), service); bindTarget.add("1"); bindTarget.add("2"); bindTarget.add("3"); @@ -58,9 +58,9 @@ public class CollectionToCollectionTests { @Test public void testCollectionToCollectionConversionNoGenericInfoSourceNullValues() throws Exception { - DefaultConversionService service = new DefaultConversionService(); - CollectionToCollection c = new CollectionToCollection(TypeDescriptor.valueOf(Collection.class), - new TypeDescriptor(getClass().getField("integerTarget")), service); + DefaultTypeConverter service = new DefaultTypeConverter(); + CollectionToCollection c = new CollectionToCollection(BindingPoint.valueOf(Collection.class), + new BindingPoint(getClass().getField("integerTarget")), service); bindTarget.add(null); bindTarget.add("1"); bindTarget.add("2"); @@ -76,9 +76,9 @@ public class CollectionToCollectionTests { @Test public void testCollectionToCollectionConversionNoGenericInfoSourceEmpty() throws Exception { - DefaultConversionService service = new DefaultConversionService(); - CollectionToCollection c = new CollectionToCollection(TypeDescriptor.valueOf(Collection.class), - new TypeDescriptor(getClass().getField("integerTarget")), service); + DefaultTypeConverter service = new DefaultTypeConverter(); + CollectionToCollection c = new CollectionToCollection(BindingPoint.valueOf(Collection.class), + new BindingPoint(getClass().getField("integerTarget")), service); List result = (List) c.execute(bindTarget); assertTrue(result.isEmpty()); } diff --git a/org.springframework.core/src/test/java/org/springframework/core/convert/converters/DefaultConverterTests.java b/org.springframework.core/src/test/java/org/springframework/core/convert/support/DefaultTypeConverterTests.java similarity index 97% rename from org.springframework.core/src/test/java/org/springframework/core/convert/converters/DefaultConverterTests.java rename to org.springframework.core/src/test/java/org/springframework/core/convert/support/DefaultTypeConverterTests.java index 3c44ef1ab64..02e813ff60a 100644 --- a/org.springframework.core/src/test/java/org/springframework/core/convert/converters/DefaultConverterTests.java +++ b/org.springframework.core/src/test/java/org/springframework/core/convert/support/DefaultTypeConverterTests.java @@ -1,4 +1,4 @@ -package org.springframework.core.convert.converters; +package org.springframework.core.convert.support; import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; @@ -28,7 +28,7 @@ import org.springframework.core.convert.support.StringToShort; /** * Tests for the default converters in the converters package. s */ -public class DefaultConverterTests { +public class DefaultTypeConverterTests { @Test public void testStringToByte() throws Exception { 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/GenericTypeConverterTests.java similarity index 50% rename from org.springframework.core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java rename to org.springframework.core/src/test/java/org/springframework/core/convert/support/GenericTypeConverterTests.java index d68c41c142c..6dd26e1115b 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/GenericTypeConverterTests.java @@ -21,7 +21,6 @@ import static junit.framework.Assert.fail; import java.util.AbstractList; import java.util.ArrayList; -import java.util.Collection; import java.util.HashMap; import java.util.LinkedList; import java.util.List; @@ -29,43 +28,35 @@ import java.util.Map; import org.junit.Ignore; import org.junit.Test; -import org.springframework.core.convert.ConversionException; +import org.springframework.core.convert.BindingPoint; +import org.springframework.core.convert.ConversionFailedException; import org.springframework.core.convert.ConverterNotFoundException; -import org.springframework.core.convert.TypeDescriptor; import org.springframework.core.convert.converter.Converter; -public class GenericConversionServiceTests { +public class GenericTypeConverterTests { - private GenericConversionService service = new GenericConversionService(); + private GenericTypeConverter converter = new GenericTypeConverter(); @Test public void executeConversion() { - service.addConverter(new StringToInteger()); - assertEquals(new Integer(3), service.convert("3", type(Integer.class))); + converter.addConverter(new StringToInteger()); + assertEquals(new Integer(3), converter.convert("3", Integer.class)); } @Test public void executeConversionNullSource() { - assertEquals(null, service.convert(null, type(Integer.class))); + assertEquals(null, converter.convert(null, Integer.class)); } @Test public void executeCompatibleSource() { - assertEquals(false, service.convert(false, type(boolean.class))); - } - - @Test - public void converterConvert() { - service.addConverter(new StringToInteger()); - ConversionExecutor executor = service.getConversionExecutor(String.class, type(Integer.class)); - Integer three = (Integer) executor.execute("3"); - assertEquals(3, three.intValue()); + assertEquals(Boolean.FALSE, converter.convert(false, boolean.class)); } @Test - public void convertExecutorNotFound() { + public void converterNotFound() { try { - service.convert("3", type(Integer.class)); + converter.convert("3", Integer.class); fail("Should have thrown an exception"); } catch (ConverterNotFoundException e) { } @@ -74,14 +65,10 @@ public class GenericConversionServiceTests { @Test public void addConverterNoSourceTargetClassInfoAvailable() { try { - service.addConverter(new Converter() { + converter.addConverter(new Converter() { public Object convert(Object source) throws Exception { return source; } - - public Object convertBack(Object target) throws Exception { - return target; - } }); fail("Should have failed"); } catch (IllegalArgumentException e) { @@ -91,51 +78,40 @@ public class GenericConversionServiceTests { @Test public void convertNull() { - service.addConverter(new StringToInteger()); - ConversionExecutor executor = service.getConversionExecutor(String.class, type(Integer.class)); - assertNull(executor.execute(null)); + assertNull(converter.convert(null, Integer.class)); } @Test public void convertWrongTypeArgument() { - service.addConverter(new StringToInteger()); + converter.addConverter(new StringToInteger()); try { - service.convert("BOGUS", type(Integer.class)); + converter.convert("BOGUS", Integer.class); fail("Should have failed"); - } catch (ConversionException e) { + } catch (ConversionFailedException e) { } } @Test public void convertSuperSourceType() { - service.addConverter(new Converter() { + converter.addConverter(new Converter() { public Integer convert(CharSequence source) throws Exception { return Integer.valueOf(source.toString()); } - - public CharSequence convertBack(Integer target) throws Exception { - return target.toString(); - } }); - ConversionExecutor executor = service.getConversionExecutor(String.class, type(Integer.class)); - Integer result = (Integer) executor.execute("3"); + Integer result = converter.convert("3", Integer.class); assertEquals(new Integer(3), result); } @Test public void convertNoSuperTargetType() { - service.addConverter(new Converter() { + converter.addConverter(new Converter() { public Integer convert(CharSequence source) throws Exception { return Integer.valueOf(source.toString()); } - - public CharSequence convertBack(Number target) throws Exception { - return target.toString(); - } }); try { - service.convert("3", type(Integer.class)); + converter.convert("3", Integer.class); fail("Should have failed"); } catch (ConverterNotFoundException e) { @@ -144,17 +120,15 @@ public class GenericConversionServiceTests { @Test public void convertObjectToPrimitive() { - service.addConverter(new StringToInteger()); - ConversionExecutor executor = service.getConversionExecutor(String.class, type(int.class)); - Integer three = (Integer) executor.execute("3"); + converter.addConverter(new StringToInteger()); + Integer three = converter.convert("3", int.class); assertEquals(3, three.intValue()); } @Test public void convertArrayToArray() { - service.addConverter(new StringToInteger()); - ConversionExecutor executor = service.getConversionExecutor(String[].class, type(Integer[].class)); - Integer[] result = (Integer[]) executor.execute(new String[] { "1", "2", "3" }); + converter.addConverter(new StringToInteger()); + Integer[] result = converter.convert(new String[] { "1", "2", "3" }, Integer[].class); assertEquals(new Integer(1), result[0]); assertEquals(new Integer(2), result[1]); assertEquals(new Integer(3), result[2]); @@ -162,9 +136,8 @@ public class GenericConversionServiceTests { @Test public void convertArrayToPrimitiveArray() { - service.addConverter(new StringToInteger()); - ConversionExecutor executor = service.getConversionExecutor(String[].class, type(int[].class)); - int[] result = (int[]) executor.execute(new String[] { "1", "2", "3" }); + converter.addConverter(new StringToInteger()); + int[] result = (int[]) converter.convert(new String[] { "1", "2", "3" }, int[].class); assertEquals(1, result[0]); assertEquals(2, result[1]); assertEquals(3, result[2]); @@ -172,8 +145,7 @@ public class GenericConversionServiceTests { @Test public void convertArrayToListInterface() { - ConversionExecutor executor = service.getConversionExecutor(String[].class, type(List.class)); - List result = (List) executor.execute(new String[] { "1", "2", "3" }); + List result = converter.convert(new String[] { "1", "2", "3" }, List.class); assertEquals("1", result.get(0)); assertEquals("2", result.get(1)); assertEquals("3", result.get(2)); @@ -183,10 +155,8 @@ public class GenericConversionServiceTests { @Test public void convertArrayToListGenericTypeConversion() throws Exception { - service.addConverter(new StringToInteger()); - ConversionExecutor executor = service.getConversionExecutor(String[].class, new TypeDescriptor(getClass() - .getDeclaredField("genericList"))); - List result = (List) executor.execute(new String[] { "1", "2", "3" }); + converter.addConverter(new StringToInteger()); + List result = converter.convert(new String[] { "1", "2", "3" }, new BindingPoint>(getClass().getDeclaredField("genericList"))); assertEquals(new Integer("1"), result.get(0)); assertEquals(new Integer("2"), result.get(1)); assertEquals(new Integer("3"), result.get(2)); @@ -194,8 +164,7 @@ public class GenericConversionServiceTests { @Test public void convertArrayToListImpl() { - ConversionExecutor executor = service.getConversionExecutor(String[].class, type(LinkedList.class)); - LinkedList result = (LinkedList) executor.execute(new String[] { "1", "2", "3" }); + LinkedList result = converter.convert(new String[] { "1", "2", "3" }, LinkedList.class); assertEquals("1", result.get(0)); assertEquals("2", result.get(1)); assertEquals("3", result.get(2)); @@ -204,7 +173,7 @@ public class GenericConversionServiceTests { @Test public void convertArrayToAbstractList() { try { - service.getConversionExecutor(String[].class, type(AbstractList.class)); + converter.convert(new String[] { "1", "2", "3" }, AbstractList.class); } catch (IllegalArgumentException e) { } @@ -212,12 +181,11 @@ public class GenericConversionServiceTests { @Test public void convertListToArray() { - ConversionExecutor executor = service.getConversionExecutor(Collection.class, type(String[].class)); - List list = new ArrayList(); + List list = new ArrayList(); list.add("1"); list.add("2"); list.add("3"); - String[] result = (String[]) executor.execute(list); + String[] result = (String[]) converter.convert(list, String[].class); assertEquals("1", result[0]); assertEquals("2", result[1]); assertEquals("3", result[2]); @@ -225,13 +193,12 @@ public class GenericConversionServiceTests { @Test public void convertListToArrayWithComponentConversion() { - service.addConverter(new StringToInteger()); - ConversionExecutor executor = service.getConversionExecutor(Collection.class, type(Integer[].class)); - List list = new ArrayList(); + converter.addConverter(new StringToInteger()); + List list = new ArrayList(); list.add("1"); list.add("2"); list.add("3"); - Integer[] result = (Integer[]) executor.execute(list); + Integer[] result = converter.convert(list, Integer[].class); assertEquals(new Integer(1), result[0]); assertEquals(new Integer(2), result[1]); assertEquals(new Integer(3), result[2]); @@ -245,16 +212,17 @@ public class GenericConversionServiceTests { Map foo = new HashMap(); foo.put("1", "BAR"); foo.put("2", "BAZ"); - service.addConverter(new StringToInteger()); - service.addConverter(new StringToEnumFactory().getConverter(FooEnum.class)); - service.convert(foo, new TypeDescriptor(getClass().getField("genericMap"))); + converter.addConverter(new StringToInteger()); + converter.addConverter(new StringToEnumFactory().getConverter(FooEnum.class)); + Map map = converter.convert(foo, new BindingPoint>(getClass().getField("genericMap"))); + assertEquals(map.get(1), FooEnum.BAR); + assertEquals(map.get(2), FooEnum.BAZ); } @Ignore @Test public void convertObjectToArray() { - ConversionExecutor executor = service.getConversionExecutor(String.class, type(String[].class)); - String[] result = (String[]) executor.execute("1,2,3"); + String[] result = (String[]) converter.convert("1,2,3", String[].class); assertEquals(1, result.length); assertEquals("1,2,3", result[0]); } @@ -262,9 +230,8 @@ public class GenericConversionServiceTests { @Ignore @Test public void convertObjectToArrayWithElementConversion() { - service.addConverter(new StringToInteger()); - ConversionExecutor executor = service.getConversionExecutor(String.class, type(Integer[].class)); - Integer[] result = (Integer[]) executor.execute("123"); + converter.addConverter(new StringToInteger()); + Integer[] result = converter.convert("123", Integer[].class); assertEquals(1, result.length); assertEquals(new Integer(123), result[0]); } @@ -273,8 +240,4 @@ public class GenericConversionServiceTests { BAR, BAZ } - private TypeDescriptor type(Class clazz) { - return TypeDescriptor.valueOf(clazz); - } - } \ No newline at end of file diff --git a/org.springframework.core/src/test/java/org/springframework/core/convert/support/MapToMapTests.java b/org.springframework.core/src/test/java/org/springframework/core/convert/support/MapToMapTests.java index aa6042ed101..7846eae0550 100644 --- a/org.springframework.core/src/test/java/org/springframework/core/convert/support/MapToMapTests.java +++ b/org.springframework.core/src/test/java/org/springframework/core/convert/support/MapToMapTests.java @@ -6,29 +6,29 @@ import java.util.HashMap; import java.util.Map; import org.junit.Test; -import org.springframework.core.convert.TypeDescriptor; -import org.springframework.core.convert.support.DefaultConversionService; +import org.springframework.core.convert.BindingPoint; +import org.springframework.core.convert.support.DefaultTypeConverter; import org.springframework.core.convert.support.MapToMap; public class MapToMapTests { @Test public void testMapToMapConversion() throws Exception { - DefaultConversionService service = new DefaultConversionService(); - MapToMap c = new MapToMap(new TypeDescriptor(getClass().getField("source")), - new TypeDescriptor(getClass().getField("bindTarget")), service); + DefaultTypeConverter converter = new DefaultTypeConverter(); + MapToMap c = new MapToMap(new BindingPoint>(getClass().getField("source")), + new BindingPoint>(getClass().getField("bindTarget")), converter); source.put("1", "BAR"); source.put("2", "BAZ"); - Map result = (Map) c.execute(source); + Map result = (Map) c.execute(source); assertEquals(FooEnum.BAR, result.get(1)); assertEquals(FooEnum.BAZ, result.get(2)); } @Test public void testMapToMapConversionNoGenericInfoOnSource() throws Exception { - DefaultConversionService service = new DefaultConversionService(); - MapToMap c = new MapToMap(TypeDescriptor.valueOf(Map.class), - new TypeDescriptor(getClass().getField("bindTarget")), service); + DefaultTypeConverter service = new DefaultTypeConverter(); + MapToMap c = new MapToMap(BindingPoint.valueOf(Map.class), + new BindingPoint(getClass().getField("bindTarget")), service); source.put("1", "BAR"); source.put("2", "BAZ"); Map result = (Map) c.execute(source); @@ -38,9 +38,9 @@ public class MapToMapTests { @Test public void testMapToMapConversionNoGenericInfo() throws Exception { - DefaultConversionService service = new DefaultConversionService(); - MapToMap c = new MapToMap(TypeDescriptor.valueOf(Map.class), - TypeDescriptor.valueOf(Map.class), service); + DefaultTypeConverter service = new DefaultTypeConverter(); + MapToMap c = new MapToMap(BindingPoint.valueOf(Map.class), + BindingPoint.valueOf(Map.class), service); source.put("1", "BAR"); source.put("2", "BAZ"); Map result = (Map) c.execute(source);