提交 6f74369c 编写于 作者: K Keith Donald

polish

上级 acf17381
...@@ -32,12 +32,12 @@ import org.springframework.util.Assert; ...@@ -32,12 +32,12 @@ import org.springframework.util.Assert;
* @author Keith Donald * @author Keith Donald
* @author Andy Clement * @author Andy Clement
*/ */
public class TypeDescriptor<T> { public class BindingPoint<T> {
/** /**
* Constant value typeDescriptor for the type of a null value * 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; private MethodParameter methodParameter;
...@@ -52,7 +52,7 @@ public class TypeDescriptor<T> { ...@@ -52,7 +52,7 @@ public class TypeDescriptor<T> {
* a Map or collection, where no additional binding metadata is available. * a Map or collection, where no additional binding metadata is available.
* @param type the actual type * @param type the actual type
*/ */
public TypeDescriptor(Class<?> type) { public BindingPoint(Class<?> type) {
this.type = type; this.type = type;
} }
...@@ -61,7 +61,7 @@ public class TypeDescriptor<T> { ...@@ -61,7 +61,7 @@ public class TypeDescriptor<T> {
* from a method parameter, such as a setter method argument. * from a method parameter, such as a setter method argument.
* @param methodParameter the MethodParameter to wrap * @param methodParameter the MethodParameter to wrap
*/ */
public TypeDescriptor(MethodParameter methodParameter) { public BindingPoint(MethodParameter methodParameter) {
Assert.notNull(methodParameter, "MethodParameter must not be null"); Assert.notNull(methodParameter, "MethodParameter must not be null");
this.methodParameter = methodParameter; this.methodParameter = methodParameter;
} }
...@@ -70,7 +70,7 @@ public class TypeDescriptor<T> { ...@@ -70,7 +70,7 @@ public class TypeDescriptor<T> {
* Create a new descriptor for a field. Use this constructor when a bound value originates from a field. * Create a new descriptor for a field. Use this constructor when a bound value originates from a field.
* @param field the field to wrap * @param field the field to wrap
*/ */
public TypeDescriptor(Field field) { public BindingPoint(Field field) {
Assert.notNull(field, "Field must not be null"); Assert.notNull(field, "Field must not be null");
this.field = field; this.field = field;
} }
...@@ -248,7 +248,7 @@ public class TypeDescriptor<T> { ...@@ -248,7 +248,7 @@ public class TypeDescriptor<T> {
* @param targetType the target type * @param targetType the target type
* @return true if this type is assignable to the target * @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()); return targetType.getType().isAssignableFrom(getType());
} }
...@@ -257,9 +257,9 @@ public class TypeDescriptor<T> { ...@@ -257,9 +257,9 @@ public class TypeDescriptor<T> {
* @param type the class * @param type the class
* @return the type descriptor * @return the type descriptor
*/ */
public static TypeDescriptor valueOf(Class<? extends Object> type) { public static <T> BindingPoint<T> valueOf(Class<T> type) {
// TODO needs a cache for common type descriptors // TODO needs a cache for common type descriptors
return new TypeDescriptor(type); return new BindingPoint<T>(type);
} }
/** /**
...@@ -267,7 +267,7 @@ public class TypeDescriptor<T> { ...@@ -267,7 +267,7 @@ public class TypeDescriptor<T> {
* @param object the object * @param object the object
* @return the type descriptor * @return the type descriptor
*/ */
public static TypeDescriptor forObject(Object object) { public static BindingPoint forObject(Object object) {
if (object == null) { if (object == null) {
return NULL_TYPE_DESCRIPTOR; return NULL_TYPE_DESCRIPTOR;
} else { } else {
......
...@@ -22,7 +22,7 @@ import org.springframework.core.style.StylerUtils; ...@@ -22,7 +22,7 @@ import org.springframework.core.style.StylerUtils;
* *
* @author Keith Donald * @author Keith Donald
*/ */
public class ConversionException extends ConvertException { public class ConversionFailedException extends ConvertException {
private transient Object value; private transient Object value;
...@@ -37,7 +37,7 @@ public class ConversionException extends ConvertException { ...@@ -37,7 +37,7 @@ public class ConversionException extends ConvertException {
* @param targetType the value's target type * @param targetType the value's target type
* @param cause the cause of the conversion failure * @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); super(defaultMessage(value, sourceType, targetType, cause), cause);
this.value = value; this.value = value;
this.sourceType = sourceType; this.sourceType = sourceType;
...@@ -51,7 +51,7 @@ public class ConversionException extends ConvertException { ...@@ -51,7 +51,7 @@ public class ConversionException extends ConvertException {
* @param targetType the value's target type * @param targetType the value's target type
* @param message a descriptive message of what went wrong. * @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); super(message);
this.value = value; this.value = value;
this.sourceType = sourceType; this.sourceType = sourceType;
......
...@@ -18,7 +18,7 @@ package org.springframework.core.convert; ...@@ -18,7 +18,7 @@ package org.springframework.core.convert;
/** /**
* A service interface for type conversion. This is the entry point into the convert system. * A service interface for type conversion. This is the entry point into the convert system.
* <p> * <p>
* 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. * this system.
* *
* @author Keith Donald * @author Keith Donald
...@@ -28,19 +28,35 @@ public interface TypeConverter { ...@@ -28,19 +28,35 @@ public interface TypeConverter {
/** /**
* Returns true if objects of sourceType can be converted to targetType. * Returns true if objects of sourceType can be converted to targetType.
* @param source the source to convert from (may be null) * @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 * @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. * Convert the source to targetType.
* @param source the source to convert from (may be null) * @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 the converted object, an instance of {@link TypeDescriptor#getType()}</code>, or <code>null</code> if a null source * @return the converted object, an instance of targetType, or <code>null</code> if a null source was provided
* was provided * @throws ConvertException if an exception occurred
*/
<S, T> T convert(S source, Class<T> 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()}</code>, or <code>null</code> if a null source was provided
* @throws ConvertException if an exception occurred * @throws ConvertException if an exception occurred
*/ */
<S, T> T convert(S source, TypeDescriptor<T> targetType); <S, T> T convert(S source, BindingPoint<T> point);
} }
\ No newline at end of file
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
...@@ -15,8 +15,8 @@ ...@@ -15,8 +15,8 @@
*/ */
package org.springframework.core.convert.support; package org.springframework.core.convert.support;
import org.springframework.core.convert.ConversionException; import org.springframework.core.convert.ConversionFailedException;
import org.springframework.core.convert.TypeDescriptor; import org.springframework.core.convert.BindingPoint;
/** /**
* Base class for converters that convert to and from collection types (arrays and java.util.Collection types) * 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; ...@@ -24,22 +24,22 @@ import org.springframework.core.convert.TypeDescriptor;
*/ */
abstract class AbstractCollectionConverter implements ConversionExecutor { abstract class AbstractCollectionConverter implements ConversionExecutor {
private GenericConversionService conversionService; private GenericTypeConverter conversionService;
private ConversionExecutor elementConverter; 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.conversionService = conversionService;
this.sourceCollectionType = sourceCollectionType; this.sourceCollectionType = sourceCollectionType;
this.targetCollectionType = targetCollectionType; this.targetCollectionType = targetCollectionType;
Class<?> sourceElementType = sourceCollectionType.getElementType(); Class<?> sourceElementType = sourceCollectionType.getElementType();
Class<?> targetElementType = targetCollectionType.getElementType(); Class<?> targetElementType = targetCollectionType.getElementType();
if (sourceElementType != null && targetElementType != null) { if (sourceElementType != null && targetElementType != null) {
elementConverter = conversionService.getConversionExecutor(sourceElementType, TypeDescriptor.valueOf(targetElementType)); elementConverter = conversionService.getConversionExecutor(sourceElementType, BindingPoint.valueOf(targetElementType));
} else { } else {
elementConverter = NoOpConversionExecutor.INSTANCE; elementConverter = NoOpConversionExecutor.INSTANCE;
} }
...@@ -59,7 +59,7 @@ abstract class AbstractCollectionConverter implements ConversionExecutor { ...@@ -59,7 +59,7 @@ abstract class AbstractCollectionConverter implements ConversionExecutor {
return targetCollectionType.getElementType(); return targetCollectionType.getElementType();
} }
protected GenericConversionService getConversionService() { protected GenericTypeConverter getConversionService() {
return conversionService; return conversionService;
} }
...@@ -75,7 +75,7 @@ abstract class AbstractCollectionConverter implements ConversionExecutor { ...@@ -75,7 +75,7 @@ abstract class AbstractCollectionConverter implements ConversionExecutor {
try { try {
return doExecute(source); return doExecute(source);
} catch (Exception e) { } catch (Exception e) {
throw new ConversionException(source, sourceCollectionType.getType(), targetCollectionType.getType(), e); throw new ConversionFailedException(source, sourceCollectionType.getType(), targetCollectionType.getType(), e);
} }
} }
......
...@@ -18,7 +18,7 @@ package org.springframework.core.convert.support; ...@@ -18,7 +18,7 @@ package org.springframework.core.convert.support;
import java.lang.reflect.Array; import java.lang.reflect.Array;
import org.springframework.core.convert.TypeConverter; 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 * 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; ...@@ -29,7 +29,7 @@ import org.springframework.core.convert.TypeDescriptor;
*/ */
class ArrayToArray extends AbstractCollectionConverter { class ArrayToArray extends AbstractCollectionConverter {
public ArrayToArray(TypeDescriptor sourceArrayType, TypeDescriptor targetArrayType, GenericConversionService conversionService) { public ArrayToArray(BindingPoint sourceArrayType, BindingPoint targetArrayType, GenericTypeConverter conversionService) {
super(sourceArrayType, targetArrayType, conversionService); super(sourceArrayType, targetArrayType, conversionService);
} }
......
...@@ -18,7 +18,7 @@ package org.springframework.core.convert.support; ...@@ -18,7 +18,7 @@ package org.springframework.core.convert.support;
import java.lang.reflect.Array; import java.lang.reflect.Array;
import java.util.Collection; 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 * 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; ...@@ -29,8 +29,8 @@ import org.springframework.core.convert.TypeDescriptor;
*/ */
class ArrayToCollection extends AbstractCollectionConverter { class ArrayToCollection extends AbstractCollectionConverter {
public ArrayToCollection(TypeDescriptor sourceArrayType, TypeDescriptor targetCollectionType, public ArrayToCollection(BindingPoint sourceArrayType, BindingPoint targetCollectionType,
GenericConversionService conversionService) { GenericTypeConverter conversionService) {
super(sourceArrayType, targetCollectionType, conversionService); super(sourceArrayType, targetCollectionType, conversionService);
} }
......
...@@ -19,7 +19,7 @@ import java.lang.reflect.Array; ...@@ -19,7 +19,7 @@ import java.lang.reflect.Array;
import java.util.Collection; import java.util.Collection;
import java.util.Iterator; 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. * Special converter that converts from target collection to a source array.
...@@ -28,8 +28,8 @@ import org.springframework.core.convert.TypeDescriptor; ...@@ -28,8 +28,8 @@ import org.springframework.core.convert.TypeDescriptor;
*/ */
class CollectionToArray extends AbstractCollectionConverter { class CollectionToArray extends AbstractCollectionConverter {
public CollectionToArray(TypeDescriptor sourceArrayType, TypeDescriptor targetCollectionType, public CollectionToArray(BindingPoint sourceArrayType, BindingPoint targetCollectionType,
GenericConversionService conversionService) { GenericTypeConverter conversionService) {
super(sourceArrayType, targetCollectionType, conversionService); super(sourceArrayType, targetCollectionType, conversionService);
} }
...@@ -52,7 +52,7 @@ class CollectionToArray extends AbstractCollectionConverter { ...@@ -52,7 +52,7 @@ class CollectionToArray extends AbstractCollectionConverter {
while (it.hasNext()) { while (it.hasNext()) {
Object value = it.next(); Object value = it.next();
if (value != null) { if (value != null) {
elementConverter = getConversionService().getConversionExecutor(value.getClass(), TypeDescriptor.valueOf(getTargetElementType())); elementConverter = getConversionService().getConversionExecutor(value.getClass(), BindingPoint.valueOf(getTargetElementType()));
break; break;
} }
} }
......
...@@ -18,7 +18,7 @@ package org.springframework.core.convert.support; ...@@ -18,7 +18,7 @@ package org.springframework.core.convert.support;
import java.util.Collection; import java.util.Collection;
import java.util.Iterator; 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. * A converter that can convert from one collection type to another.
...@@ -27,8 +27,8 @@ import org.springframework.core.convert.TypeDescriptor; ...@@ -27,8 +27,8 @@ import org.springframework.core.convert.TypeDescriptor;
*/ */
class CollectionToCollection extends AbstractCollectionConverter { class CollectionToCollection extends AbstractCollectionConverter {
public CollectionToCollection(TypeDescriptor sourceCollectionType, TypeDescriptor targetCollectionType, public CollectionToCollection(BindingPoint sourceCollectionType, BindingPoint targetCollectionType,
GenericConversionService conversionService) { GenericTypeConverter conversionService) {
super(sourceCollectionType, targetCollectionType, conversionService); super(sourceCollectionType, targetCollectionType, conversionService);
} }
...@@ -53,7 +53,7 @@ class CollectionToCollection extends AbstractCollectionConverter { ...@@ -53,7 +53,7 @@ class CollectionToCollection extends AbstractCollectionConverter {
while (it.hasNext()) { while (it.hasNext()) {
Object value = it.next(); Object value = it.next();
if (value != null) { if (value != null) {
elementConverter = getConversionService().getConversionExecutor(value.getClass(), TypeDescriptor.valueOf(getTargetElementType())); elementConverter = getConversionService().getConversionExecutor(value.getClass(), BindingPoint.valueOf(getTargetElementType()));
break; break;
} }
} }
......
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
*/ */
package org.springframework.core.convert.support; 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 * A command parameterized with the information necessary to perform a conversion of a source input to a
...@@ -29,7 +29,7 @@ public interface ConversionExecutor { ...@@ -29,7 +29,7 @@ public interface ConversionExecutor {
/** /**
* Convert the source. * Convert the source.
* @param source the source to convert * @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); public Object execute(Object source);
......
...@@ -15,7 +15,6 @@ ...@@ -15,7 +15,6 @@
*/ */
package org.springframework.core.convert.support; package org.springframework.core.convert.support;
import org.springframework.core.convert.support.StringToEnumFactory.StringToEnum;
/** /**
* Default implementation of a conversion service. Will automatically register <i>from string</i> converters for * Default implementation of a conversion service. Will automatically register <i>from string</i> converters for
...@@ -23,12 +22,12 @@ import org.springframework.core.convert.support.StringToEnumFactory.StringToEnum ...@@ -23,12 +22,12 @@ import org.springframework.core.convert.support.StringToEnumFactory.StringToEnum
* *
* @author Keith Donald * @author Keith Donald
*/ */
public class DefaultConversionService extends GenericConversionService { public class DefaultTypeConverter extends GenericTypeConverter {
/** /**
* Creates a new default conversion service, installing the default converters. * Creates a new default conversion service, installing the default converters.
*/ */
public DefaultConversionService() { public DefaultTypeConverter() {
addDefaultConverters(); addDefaultConverters();
} }
...@@ -49,6 +48,9 @@ public class DefaultConversionService extends GenericConversionService { ...@@ -49,6 +48,9 @@ public class DefaultConversionService extends GenericConversionService {
addConverter(new StringToLocale()); addConverter(new StringToLocale());
addConverter(new NumberToCharacter()); addConverter(new NumberToCharacter());
addConverter(new ObjectToString()); addConverter(new ObjectToString());
addConverterFactory(new StringToEnumFactory());
addConverterFactory(new NumberToNumberFactory());
addConverterFactory(new CharacterToNumberFactory());
} }
} }
\ No newline at end of file
...@@ -28,9 +28,11 @@ import java.util.Map; ...@@ -28,9 +28,11 @@ import java.util.Map;
import org.springframework.core.GenericTypeResolver; import org.springframework.core.GenericTypeResolver;
import org.springframework.core.convert.ConverterNotFoundException; import org.springframework.core.convert.ConverterNotFoundException;
import org.springframework.core.convert.TypeConverter; 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.Converter;
import org.springframework.core.convert.converter.ConverterFactory;
import org.springframework.core.convert.converter.ConverterInfo; import org.springframework.core.convert.converter.ConverterInfo;
import org.springframework.core.convert.converter.ConverterRegistry;
import org.springframework.util.Assert; import org.springframework.util.Assert;
/** /**
...@@ -43,7 +45,7 @@ import org.springframework.util.Assert; ...@@ -43,7 +45,7 @@ import org.springframework.util.Assert;
* @author Keith Donald * @author Keith Donald
*/ */
@SuppressWarnings("unchecked") @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 * An indexed map of Converters. Each Map.Entry key is a source class (S) that can be converted from. Each Map.Entry
...@@ -83,9 +85,23 @@ public class GenericConversionService implements TypeConverter { ...@@ -83,9 +85,23 @@ public class GenericConversionService implements TypeConverter {
sourceMap.put(targetType, converter); sourceMap.put(targetType, converter);
} }
public void addConverterFactory(ConverterFactory<?, ?> converter) {
}
public void removeConverter(Converter<?, ?> converter) {
}
public void removeConverterFactory(Converter<?, ?> converter) {
}
// implementing ConversionService // 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); ConversionExecutor executor = getConversionExecutor(sourceType, targetType);
if (executor != null) { if (executor != null) {
return true; return true;
...@@ -98,16 +114,20 @@ public class GenericConversionService implements TypeConverter { ...@@ -98,16 +114,20 @@ public class GenericConversionService implements TypeConverter {
} }
} }
public Object convert(Object source, TypeDescriptor targetType) { public <S, T> T convert(S source, Class<T> targetType) {
return convert(source, BindingPoint.valueOf(targetType));
}
public <S, T> T convert(S source, BindingPoint<T> targetType) {
if (source == null) { if (source == null) {
return null; return null;
} }
if (source.getClass().isAssignableFrom(targetType.getType())) { if (source.getClass().isAssignableFrom(targetType.getType())) {
return source; return (T) source;
} }
ConversionExecutor executor = getConversionExecutor(source.getClass(), targetType); ConversionExecutor executor = getConversionExecutor(source.getClass(), targetType);
if (executor != null) { if (executor != null) {
return executor.execute(source); return (T) executor.execute(source);
} else { } else {
if (parent != null) { if (parent != null) {
return parent.convert(source, targetType); return parent.convert(source, targetType);
...@@ -119,11 +139,11 @@ public class GenericConversionService implements TypeConverter { ...@@ -119,11 +139,11 @@ public class GenericConversionService implements TypeConverter {
} }
} }
ConversionExecutor getConversionExecutor(Class sourceClass, TypeDescriptor targetType) ConversionExecutor getConversionExecutor(Class sourceClass, BindingPoint targetType)
throws ConverterNotFoundException { throws ConverterNotFoundException {
Assert.notNull(sourceClass, "The sourceType to convert from is required"); Assert.notNull(sourceClass, "The sourceType to convert from is required");
Assert.notNull(targetType, "The targetType to convert to 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 (sourceType.isArray()) {
if (targetType.isArray()) { if (targetType.isArray()) {
return new ArrayToArray(sourceType, targetType, this); return new ArrayToArray(sourceType, targetType, this);
......
...@@ -21,20 +21,21 @@ import java.util.Map; ...@@ -21,20 +21,21 @@ import java.util.Map;
import java.util.SortedMap; import java.util.SortedMap;
import java.util.TreeMap; import java.util.TreeMap;
import org.springframework.core.convert.ConversionException; import org.springframework.core.convert.ConversionFailedException;
import org.springframework.core.convert.TypeDescriptor; 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. * Converts from one map to another map, with support for converting individual map elements based on generic type information.
* @author Keith Donald * @author Keith Donald
*/ */
@SuppressWarnings("unchecked")
class MapToMap implements ConversionExecutor { 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; private EntryConverter entryConverter;
...@@ -44,7 +45,7 @@ class MapToMap implements ConversionExecutor { ...@@ -44,7 +45,7 @@ class MapToMap implements ConversionExecutor {
* @param targetType the target map type * @param targetType the target map type
* @param conversionService the conversion service * @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.sourceType = sourceType;
this.targetType = targetType; this.targetType = targetType;
this.conversionService = conversionService; this.conversionService = conversionService;
...@@ -54,17 +55,16 @@ class MapToMap implements ConversionExecutor { ...@@ -54,17 +55,16 @@ class MapToMap implements ConversionExecutor {
private EntryConverter createEntryConverter() { private EntryConverter createEntryConverter() {
if (sourceType.isMapEntryTypeKnown() && targetType.isMapEntryTypeKnown()) { if (sourceType.isMapEntryTypeKnown() && targetType.isMapEntryTypeKnown()) {
ConversionExecutor keyConverter = conversionService.getConversionExecutor(sourceType.getMapKeyType(), ConversionExecutor keyConverter = conversionService.getConversionExecutor(sourceType.getMapKeyType(),
TypeDescriptor.valueOf(targetType.getMapKeyType())); BindingPoint.valueOf(targetType.getMapKeyType()));
ConversionExecutor valueConverter = conversionService.getConversionExecutor(sourceType.getMapValueType(), ConversionExecutor valueConverter = conversionService.getConversionExecutor(sourceType.getMapValueType(),
TypeDescriptor.valueOf(targetType.getMapValueType())); BindingPoint.valueOf(targetType.getMapValueType()));
return new EntryConverter(keyConverter, valueConverter); return new EntryConverter(keyConverter, valueConverter);
} else { } else {
return EntryConverter.NO_OP_INSTANCE; return EntryConverter.NO_OP_INSTANCE;
} }
} }
@SuppressWarnings("unchecked") public Object execute(Object source) throws ConversionFailedException {
public Object execute(Object source) throws ConversionException {
try { try {
Map map = (Map) source; Map map = (Map) source;
Map targetMap = (Map) getImpl(targetType.getType()).newInstance(); Map targetMap = (Map) getImpl(targetType.getType()).newInstance();
...@@ -76,7 +76,7 @@ class MapToMap implements ConversionExecutor { ...@@ -76,7 +76,7 @@ class MapToMap implements ConversionExecutor {
} }
return targetMap; return targetMap;
} catch (Exception e) { } 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 { ...@@ -94,11 +94,11 @@ class MapToMap implements ConversionExecutor {
Object key = entry.getKey(); Object key = entry.getKey();
Object value = entry.getValue(); Object value = entry.getValue();
if (keyConverter == null && key != null) { if (keyConverter == null && key != null) {
keyConverter = conversionService.getConversionExecutor(key.getClass(), TypeDescriptor keyConverter = conversionService.getConversionExecutor(key.getClass(), BindingPoint
.valueOf(targetKeyType)); .valueOf(targetKeyType));
} }
if (valueConverter == null && value != null) { if (valueConverter == null && value != null) {
valueConverter = conversionService.getConversionExecutor(value.getClass(), TypeDescriptor valueConverter = conversionService.getConversionExecutor(value.getClass(), BindingPoint
.valueOf(targetValueType)); .valueOf(targetValueType));
} }
if (keyConverter != null && valueConverter != null) { if (keyConverter != null && valueConverter != null) {
......
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
*/ */
package org.springframework.core.convert.support; 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 * Conversion executor that does nothing. Access singleton at {@link #INSTANCE}.s
...@@ -28,7 +28,7 @@ class NoOpConversionExecutor implements ConversionExecutor { ...@@ -28,7 +28,7 @@ class NoOpConversionExecutor implements ConversionExecutor {
} }
public Object execute(Object source) throws ConversionException { public Object execute(Object source) throws ConversionFailedException {
return source; return source;
} }
......
...@@ -4,7 +4,7 @@ import org.springframework.core.convert.converter.Converter; ...@@ -4,7 +4,7 @@ import org.springframework.core.convert.converter.Converter;
/** /**
* Simply calls {@link Object#toString()} to convert any object to a string. * 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 * @author Keith Donald
*/ */
public class ObjectToString implements Converter<Object, String> { public class ObjectToString implements Converter<Object, String> {
......
...@@ -15,8 +15,8 @@ ...@@ -15,8 +15,8 @@
*/ */
package org.springframework.core.convert.support; package org.springframework.core.convert.support;
import org.springframework.core.convert.ConversionException; import org.springframework.core.convert.ConversionFailedException;
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.Converter;
import org.springframework.core.style.ToStringCreator; import org.springframework.core.style.ToStringCreator;
...@@ -27,30 +27,30 @@ import org.springframework.core.style.ToStringCreator; ...@@ -27,30 +27,30 @@ import org.springframework.core.style.ToStringCreator;
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
class StaticConversionExecutor implements ConversionExecutor { class StaticConversionExecutor implements ConversionExecutor {
private final TypeDescriptor sourceType; private final BindingPoint sourceType;
private final TypeDescriptor targetType; private final BindingPoint targetType;
private final Converter converter; private final Converter converter;
public StaticConversionExecutor(TypeDescriptor sourceType, TypeDescriptor targetType, Converter converter) { public StaticConversionExecutor(BindingPoint sourceType, BindingPoint targetType, Converter converter) {
this.sourceType = sourceType; this.sourceType = sourceType;
this.targetType = targetType; this.targetType = targetType;
this.converter = converter; this.converter = converter;
} }
public Object execute(Object source) throws ConversionException { public Object execute(Object source) throws ConversionFailedException {
if (source == null) { if (source == null) {
return null; return null;
} }
if (sourceType != null && !sourceType.isInstance(source)) { 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() + "]"); + source + " to convert is expected to be an instance of [" + sourceType.getName() + "]");
} }
try { try {
return converter.convert(source); return converter.convert(source);
} catch (Exception e) { } catch (Exception e) {
throw new ConversionException(source, sourceType.getType(), targetType.getType(), e); throw new ConversionFailedException(source, sourceType.getType(), targetType.getType(), e);
} }
} }
......
...@@ -34,13 +34,13 @@ public class TypeDescriptorTests { ...@@ -34,13 +34,13 @@ public class TypeDescriptorTests {
@Test @Test
public void testWrapperType() { public void testWrapperType() {
TypeDescriptor desc = TypeDescriptor.valueOf(int.class); BindingPoint desc = BindingPoint.valueOf(int.class);
assertEquals(Integer.class, desc.getType()); assertEquals(Integer.class, desc.getType());
} }
@Test @Test
public void listDescriptors() throws Exception { public void listDescriptors() throws Exception {
TypeDescriptor typeDescriptor = new TypeDescriptor(TypeDescriptorTests.class.getDeclaredField("listOfString")); BindingPoint typeDescriptor = new BindingPoint(TypeDescriptorTests.class.getDeclaredField("listOfString"));
assertFalse(typeDescriptor.isArray()); assertFalse(typeDescriptor.isArray());
assertEquals(List.class,typeDescriptor.getType()); assertEquals(List.class,typeDescriptor.getType());
assertEquals(String.class,typeDescriptor.getElementType()); assertEquals(String.class,typeDescriptor.getElementType());
...@@ -50,7 +50,7 @@ public class TypeDescriptorTests { ...@@ -50,7 +50,7 @@ public class TypeDescriptorTests {
@Test @Test
public void arrayTypeDescriptors() throws Exception { public void arrayTypeDescriptors() throws Exception {
TypeDescriptor typeDescriptor = new TypeDescriptor(TypeDescriptorTests.class.getDeclaredField("intArray")); BindingPoint typeDescriptor = new BindingPoint(TypeDescriptorTests.class.getDeclaredField("intArray"));
assertTrue(typeDescriptor.isArray()); assertTrue(typeDescriptor.isArray());
assertEquals(Integer.TYPE,typeDescriptor.getElementType()); assertEquals(Integer.TYPE,typeDescriptor.getElementType());
assertEquals("int[]",typeDescriptor.asString()); assertEquals("int[]",typeDescriptor.asString());
...@@ -58,14 +58,14 @@ public class TypeDescriptorTests { ...@@ -58,14 +58,14 @@ public class TypeDescriptorTests {
@Test @Test
public void buildingArrayTypeDescriptors() throws Exception { public void buildingArrayTypeDescriptors() throws Exception {
TypeDescriptor typeDescriptor = new TypeDescriptor(new int[0].getClass()); BindingPoint typeDescriptor = new BindingPoint(new int[0].getClass());
assertTrue(typeDescriptor.isArray()); assertTrue(typeDescriptor.isArray());
assertEquals(Integer.TYPE,typeDescriptor.getElementType()); assertEquals(Integer.TYPE,typeDescriptor.getElementType());
} }
@Test @Test
public void complexTypeDescriptors() throws Exception { public void complexTypeDescriptors() throws Exception {
TypeDescriptor typeDescriptor = new TypeDescriptor(TypeDescriptorTests.class.getDeclaredField("arrayOfListOfString")); BindingPoint typeDescriptor = new BindingPoint(TypeDescriptorTests.class.getDeclaredField("arrayOfListOfString"));
assertTrue(typeDescriptor.isArray()); assertTrue(typeDescriptor.isArray());
assertEquals(List.class,typeDescriptor.getElementType()); assertEquals(List.class,typeDescriptor.getElementType());
// TODO asc notice that the type of the list elements is lost: typeDescriptor.getElementType() should return a TypeDescriptor // TODO asc notice that the type of the list elements is lost: typeDescriptor.getElementType() should return a TypeDescriptor
......
...@@ -3,16 +3,16 @@ package org.springframework.core.convert.support; ...@@ -3,16 +3,16 @@ package org.springframework.core.convert.support;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import org.junit.Test; 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.ArrayToArray;
import org.springframework.core.convert.support.DefaultConversionService; import org.springframework.core.convert.support.DefaultTypeConverter;
public class ArrayToArrayTests { public class ArrayToArrayTests {
@Test @Test
public void testArrayToArrayConversion() { public void testArrayToArrayConversion() {
DefaultConversionService service = new DefaultConversionService(); DefaultTypeConverter service = new DefaultTypeConverter();
ArrayToArray c = new ArrayToArray(TypeDescriptor.valueOf(String[].class), TypeDescriptor.valueOf(Integer[].class), service); ArrayToArray c = new ArrayToArray(BindingPoint.valueOf(String[].class), BindingPoint.valueOf(Integer[].class), service);
Integer[] result = (Integer[]) c.execute(new String[] { "1", "2", "3" }); Integer[] result = (Integer[]) c.execute(new String[] { "1", "2", "3" });
assertEquals(new Integer(1), result[0]); assertEquals(new Integer(1), result[0]);
assertEquals(new Integer(2), result[1]); assertEquals(new Integer(2), result[1]);
......
...@@ -9,16 +9,16 @@ import java.util.Set; ...@@ -9,16 +9,16 @@ import java.util.Set;
import java.util.SortedSet; import java.util.SortedSet;
import org.junit.Test; 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.ArrayToCollection;
import org.springframework.core.convert.support.DefaultConversionService; import org.springframework.core.convert.support.DefaultTypeConverter;
public class ArrayToCollectionTests { public class ArrayToCollectionTests {
@Test @Test
public void testArrayToCollectionConversion() throws Exception { public void testArrayToCollectionConversion() throws Exception {
DefaultConversionService service = new DefaultConversionService(); DefaultTypeConverter service = new DefaultTypeConverter();
ArrayToCollection c = new ArrayToCollection(TypeDescriptor.valueOf(String[].class), new TypeDescriptor(getClass().getField("bindTarget")), service); ArrayToCollection c = new ArrayToCollection(BindingPoint.valueOf(String[].class), new BindingPoint(getClass().getField("bindTarget")), service);
List result = (List) c.execute(new String[] { "1", "2", "3" }); List result = (List) c.execute(new String[] { "1", "2", "3" });
assertEquals(new Integer(1), result.get(0)); assertEquals(new Integer(1), result.get(0));
assertEquals(new Integer(2), result.get(1)); assertEquals(new Integer(2), result.get(1));
...@@ -27,32 +27,32 @@ public class ArrayToCollectionTests { ...@@ -27,32 +27,32 @@ public class ArrayToCollectionTests {
@Test @Test
public void testArrayToSetConversion() throws Exception { public void testArrayToSetConversion() throws Exception {
DefaultConversionService service = new DefaultConversionService(); DefaultTypeConverter service = new DefaultTypeConverter();
ArrayToCollection c = new ArrayToCollection(TypeDescriptor.valueOf(String[].class), new TypeDescriptor(getClass().getField("setTarget")), service); ArrayToCollection c = new ArrayToCollection(BindingPoint.valueOf(String[].class), new BindingPoint(getClass().getField("setTarget")), service);
Set result = (Set) c.execute(new String[] { "1" }); Set result = (Set) c.execute(new String[] { "1" });
assertEquals("1", result.iterator().next()); assertEquals("1", result.iterator().next());
} }
@Test @Test
public void testArrayToSortedSetConversion() throws Exception { public void testArrayToSortedSetConversion() throws Exception {
DefaultConversionService service = new DefaultConversionService(); DefaultTypeConverter service = new DefaultTypeConverter();
ArrayToCollection c = new ArrayToCollection(TypeDescriptor.valueOf(String[].class), new TypeDescriptor(getClass().getField("sortedSetTarget")), service); ArrayToCollection c = new ArrayToCollection(BindingPoint.valueOf(String[].class), new BindingPoint(getClass().getField("sortedSetTarget")), service);
SortedSet result = (SortedSet) c.execute(new String[] { "1" }); SortedSet result = (SortedSet) c.execute(new String[] { "1" });
assertEquals(new Integer(1), result.iterator().next()); assertEquals(new Integer(1), result.iterator().next());
} }
@Test @Test
public void testArrayToCollectionImplConversion() throws Exception { public void testArrayToCollectionImplConversion() throws Exception {
DefaultConversionService service = new DefaultConversionService(); DefaultTypeConverter service = new DefaultTypeConverter();
ArrayToCollection c = new ArrayToCollection(TypeDescriptor.valueOf(String[].class), new TypeDescriptor(getClass().getField("implTarget")), service); ArrayToCollection c = new ArrayToCollection(BindingPoint.valueOf(String[].class), new BindingPoint(getClass().getField("implTarget")), service);
LinkedList result = (LinkedList) c.execute(new String[] { "1" }); LinkedList result = (LinkedList) c.execute(new String[] { "1" });
assertEquals("1", result.iterator().next()); assertEquals("1", result.iterator().next());
} }
@Test @Test
public void testArrayToNonGenericCollectionConversionNullElement() throws Exception { public void testArrayToNonGenericCollectionConversionNullElement() throws Exception {
DefaultConversionService service = new DefaultConversionService(); DefaultTypeConverter service = new DefaultTypeConverter();
ArrayToCollection c = new ArrayToCollection(TypeDescriptor.valueOf(String[].class), new TypeDescriptor(getClass().getField("listTarget")), service); 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) }); List result = (List) c.execute(new Integer[] { null, new Integer(1) });
assertEquals(null, result.get(0)); assertEquals(null, result.get(0));
assertEquals(new Integer(1), result.get(1)); assertEquals(new Integer(1), result.get(1));
......
...@@ -6,17 +6,17 @@ import java.util.ArrayList; ...@@ -6,17 +6,17 @@ import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import org.junit.Test; 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.CollectionToArray;
import org.springframework.core.convert.support.DefaultConversionService; import org.springframework.core.convert.support.DefaultTypeConverter;
public class CollectionToArrayTests { public class CollectionToArrayTests {
@Test @Test
public void testCollectionToArrayConversion() throws Exception { public void testCollectionToArrayConversion() throws Exception {
DefaultConversionService service = new DefaultConversionService(); DefaultTypeConverter service = new DefaultTypeConverter();
CollectionToArray c = new CollectionToArray(new TypeDescriptor(getClass().getField("bindTarget")), CollectionToArray c = new CollectionToArray(new BindingPoint(getClass().getField("bindTarget")),
TypeDescriptor.valueOf(Integer[].class), service); BindingPoint.valueOf(Integer[].class), service);
bindTarget.add("1"); bindTarget.add("1");
bindTarget.add("2"); bindTarget.add("2");
bindTarget.add("3"); bindTarget.add("3");
...@@ -28,8 +28,8 @@ public class CollectionToArrayTests { ...@@ -28,8 +28,8 @@ public class CollectionToArrayTests {
@Test @Test
public void testCollectionToArrayConversionNoGenericInfo() throws Exception { public void testCollectionToArrayConversionNoGenericInfo() throws Exception {
DefaultConversionService service = new DefaultConversionService(); DefaultTypeConverter service = new DefaultTypeConverter();
CollectionToArray c = new CollectionToArray(TypeDescriptor.valueOf(Collection.class), TypeDescriptor CollectionToArray c = new CollectionToArray(BindingPoint.valueOf(Collection.class), BindingPoint
.valueOf(Integer[].class), service); .valueOf(Integer[].class), service);
bindTarget.add("1"); bindTarget.add("1");
bindTarget.add("2"); bindTarget.add("2");
...@@ -42,8 +42,8 @@ public class CollectionToArrayTests { ...@@ -42,8 +42,8 @@ public class CollectionToArrayTests {
@Test @Test
public void testCollectionToArrayConversionNoGenericInfoNullElement() throws Exception { public void testCollectionToArrayConversionNoGenericInfoNullElement() throws Exception {
DefaultConversionService service = new DefaultConversionService(); DefaultTypeConverter service = new DefaultTypeConverter();
CollectionToArray c = new CollectionToArray(TypeDescriptor.valueOf(Collection.class), TypeDescriptor CollectionToArray c = new CollectionToArray(BindingPoint.valueOf(Collection.class), BindingPoint
.valueOf(Integer[].class), service); .valueOf(Integer[].class), service);
bindTarget.add(null); bindTarget.add(null);
bindTarget.add("1"); bindTarget.add("1");
......
...@@ -8,17 +8,17 @@ import java.util.Collection; ...@@ -8,17 +8,17 @@ import java.util.Collection;
import java.util.List; import java.util.List;
import org.junit.Test; 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.CollectionToCollection;
import org.springframework.core.convert.support.DefaultConversionService; import org.springframework.core.convert.support.DefaultTypeConverter;
public class CollectionToCollectionTests { public class CollectionToCollectionTests {
@Test @Test
public void testCollectionToCollectionConversion() throws Exception { public void testCollectionToCollectionConversion() throws Exception {
DefaultConversionService service = new DefaultConversionService(); DefaultTypeConverter service = new DefaultTypeConverter();
CollectionToCollection c = new CollectionToCollection(new TypeDescriptor(getClass().getField("bindTarget")), CollectionToCollection c = new CollectionToCollection(new BindingPoint(getClass().getField("bindTarget")),
new TypeDescriptor(getClass().getField("integerTarget")), service); new BindingPoint(getClass().getField("integerTarget")), service);
bindTarget.add("1"); bindTarget.add("1");
bindTarget.add("2"); bindTarget.add("2");
bindTarget.add("3"); bindTarget.add("3");
...@@ -30,9 +30,9 @@ public class CollectionToCollectionTests { ...@@ -30,9 +30,9 @@ public class CollectionToCollectionTests {
@Test @Test
public void testCollectionToCollectionConversionNoGenericInfo() throws Exception { public void testCollectionToCollectionConversionNoGenericInfo() throws Exception {
DefaultConversionService service = new DefaultConversionService(); DefaultTypeConverter service = new DefaultTypeConverter();
CollectionToCollection c = new CollectionToCollection(TypeDescriptor.valueOf(Collection.class), CollectionToCollection c = new CollectionToCollection(BindingPoint.valueOf(Collection.class),
TypeDescriptor.valueOf(List.class), service); BindingPoint.valueOf(List.class), service);
bindTarget.add("1"); bindTarget.add("1");
bindTarget.add("2"); bindTarget.add("2");
bindTarget.add("3"); bindTarget.add("3");
...@@ -44,9 +44,9 @@ public class CollectionToCollectionTests { ...@@ -44,9 +44,9 @@ public class CollectionToCollectionTests {
@Test @Test
public void testCollectionToCollectionConversionNoGenericInfoSource() throws Exception { public void testCollectionToCollectionConversionNoGenericInfoSource() throws Exception {
DefaultConversionService service = new DefaultConversionService(); DefaultTypeConverter service = new DefaultTypeConverter();
CollectionToCollection c = new CollectionToCollection(TypeDescriptor.valueOf(Collection.class), CollectionToCollection c = new CollectionToCollection(BindingPoint.valueOf(Collection.class),
new TypeDescriptor(getClass().getField("integerTarget")), service); new BindingPoint(getClass().getField("integerTarget")), service);
bindTarget.add("1"); bindTarget.add("1");
bindTarget.add("2"); bindTarget.add("2");
bindTarget.add("3"); bindTarget.add("3");
...@@ -58,9 +58,9 @@ public class CollectionToCollectionTests { ...@@ -58,9 +58,9 @@ public class CollectionToCollectionTests {
@Test @Test
public void testCollectionToCollectionConversionNoGenericInfoSourceNullValues() throws Exception { public void testCollectionToCollectionConversionNoGenericInfoSourceNullValues() throws Exception {
DefaultConversionService service = new DefaultConversionService(); DefaultTypeConverter service = new DefaultTypeConverter();
CollectionToCollection c = new CollectionToCollection(TypeDescriptor.valueOf(Collection.class), CollectionToCollection c = new CollectionToCollection(BindingPoint.valueOf(Collection.class),
new TypeDescriptor(getClass().getField("integerTarget")), service); new BindingPoint(getClass().getField("integerTarget")), service);
bindTarget.add(null); bindTarget.add(null);
bindTarget.add("1"); bindTarget.add("1");
bindTarget.add("2"); bindTarget.add("2");
...@@ -76,9 +76,9 @@ public class CollectionToCollectionTests { ...@@ -76,9 +76,9 @@ public class CollectionToCollectionTests {
@Test @Test
public void testCollectionToCollectionConversionNoGenericInfoSourceEmpty() throws Exception { public void testCollectionToCollectionConversionNoGenericInfoSourceEmpty() throws Exception {
DefaultConversionService service = new DefaultConversionService(); DefaultTypeConverter service = new DefaultTypeConverter();
CollectionToCollection c = new CollectionToCollection(TypeDescriptor.valueOf(Collection.class), CollectionToCollection c = new CollectionToCollection(BindingPoint.valueOf(Collection.class),
new TypeDescriptor(getClass().getField("integerTarget")), service); new BindingPoint(getClass().getField("integerTarget")), service);
List result = (List) c.execute(bindTarget); List result = (List) c.execute(bindTarget);
assertTrue(result.isEmpty()); assertTrue(result.isEmpty());
} }
......
package org.springframework.core.convert.converters; package org.springframework.core.convert.support;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
...@@ -28,7 +28,7 @@ import org.springframework.core.convert.support.StringToShort; ...@@ -28,7 +28,7 @@ import org.springframework.core.convert.support.StringToShort;
/** /**
* Tests for the default converters in the converters package. * Tests for the default converters in the converters package.
s */ s */
public class DefaultConverterTests { public class DefaultTypeConverterTests {
@Test @Test
public void testStringToByte() throws Exception { public void testStringToByte() throws Exception {
......
...@@ -21,7 +21,6 @@ import static junit.framework.Assert.fail; ...@@ -21,7 +21,6 @@ import static junit.framework.Assert.fail;
import java.util.AbstractList; import java.util.AbstractList;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
...@@ -29,43 +28,35 @@ import java.util.Map; ...@@ -29,43 +28,35 @@ import java.util.Map;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Test; 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.ConverterNotFoundException;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.Converter; import org.springframework.core.convert.converter.Converter;
public class GenericConversionServiceTests { public class GenericTypeConverterTests {
private GenericConversionService service = new GenericConversionService(); private GenericTypeConverter converter = new GenericTypeConverter();
@Test @Test
public void executeConversion() { public void executeConversion() {
service.addConverter(new StringToInteger()); converter.addConverter(new StringToInteger());
assertEquals(new Integer(3), service.convert("3", type(Integer.class))); assertEquals(new Integer(3), converter.convert("3", Integer.class));
} }
@Test @Test
public void executeConversionNullSource() { public void executeConversionNullSource() {
assertEquals(null, service.convert(null, type(Integer.class))); assertEquals(null, converter.convert(null, Integer.class));
} }
@Test @Test
public void executeCompatibleSource() { public void executeCompatibleSource() {
assertEquals(false, service.convert(false, type(boolean.class))); assertEquals(Boolean.FALSE, converter.convert(false, boolean.class));
} }
@Test @Test
public void converterConvert() { public void converterNotFound() {
service.addConverter(new StringToInteger());
ConversionExecutor executor = service.getConversionExecutor(String.class, type(Integer.class));
Integer three = (Integer) executor.execute("3");
assertEquals(3, three.intValue());
}
@Test
public void convertExecutorNotFound() {
try { try {
service.convert("3", type(Integer.class)); converter.convert("3", Integer.class);
fail("Should have thrown an exception"); fail("Should have thrown an exception");
} catch (ConverterNotFoundException e) { } catch (ConverterNotFoundException e) {
} }
...@@ -74,14 +65,10 @@ public class GenericConversionServiceTests { ...@@ -74,14 +65,10 @@ public class GenericConversionServiceTests {
@Test @Test
public void addConverterNoSourceTargetClassInfoAvailable() { public void addConverterNoSourceTargetClassInfoAvailable() {
try { try {
service.addConverter(new Converter() { converter.addConverter(new Converter() {
public Object convert(Object source) throws Exception { public Object convert(Object source) throws Exception {
return source; return source;
} }
public Object convertBack(Object target) throws Exception {
return target;
}
}); });
fail("Should have failed"); fail("Should have failed");
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
...@@ -91,51 +78,40 @@ public class GenericConversionServiceTests { ...@@ -91,51 +78,40 @@ public class GenericConversionServiceTests {
@Test @Test
public void convertNull() { public void convertNull() {
service.addConverter(new StringToInteger()); assertNull(converter.convert(null, Integer.class));
ConversionExecutor executor = service.getConversionExecutor(String.class, type(Integer.class));
assertNull(executor.execute(null));
} }
@Test @Test
public void convertWrongTypeArgument() { public void convertWrongTypeArgument() {
service.addConverter(new StringToInteger()); converter.addConverter(new StringToInteger());
try { try {
service.convert("BOGUS", type(Integer.class)); converter.convert("BOGUS", Integer.class);
fail("Should have failed"); fail("Should have failed");
} catch (ConversionException e) { } catch (ConversionFailedException e) {
} }
} }
@Test @Test
public void convertSuperSourceType() { public void convertSuperSourceType() {
service.addConverter(new Converter<CharSequence, Integer>() { converter.addConverter(new Converter<CharSequence, Integer>() {
public Integer convert(CharSequence source) throws Exception { public Integer convert(CharSequence source) throws Exception {
return Integer.valueOf(source.toString()); 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 = converter.convert("3", Integer.class);
Integer result = (Integer) executor.execute("3");
assertEquals(new Integer(3), result); assertEquals(new Integer(3), result);
} }
@Test @Test
public void convertNoSuperTargetType() { public void convertNoSuperTargetType() {
service.addConverter(new Converter<CharSequence, Number>() { converter.addConverter(new Converter<CharSequence, Number>() {
public Integer convert(CharSequence source) throws Exception { public Integer convert(CharSequence source) throws Exception {
return Integer.valueOf(source.toString()); return Integer.valueOf(source.toString());
} }
public CharSequence convertBack(Number target) throws Exception {
return target.toString();
}
}); });
try { try {
service.convert("3", type(Integer.class)); converter.convert("3", Integer.class);
fail("Should have failed"); fail("Should have failed");
} catch (ConverterNotFoundException e) { } catch (ConverterNotFoundException e) {
...@@ -144,17 +120,15 @@ public class GenericConversionServiceTests { ...@@ -144,17 +120,15 @@ public class GenericConversionServiceTests {
@Test @Test
public void convertObjectToPrimitive() { public void convertObjectToPrimitive() {
service.addConverter(new StringToInteger()); converter.addConverter(new StringToInteger());
ConversionExecutor executor = service.getConversionExecutor(String.class, type(int.class)); Integer three = converter.convert("3", int.class);
Integer three = (Integer) executor.execute("3");
assertEquals(3, three.intValue()); assertEquals(3, three.intValue());
} }
@Test @Test
public void convertArrayToArray() { public void convertArrayToArray() {
service.addConverter(new StringToInteger()); converter.addConverter(new StringToInteger());
ConversionExecutor executor = service.getConversionExecutor(String[].class, type(Integer[].class)); Integer[] result = converter.convert(new String[] { "1", "2", "3" }, Integer[].class);
Integer[] result = (Integer[]) executor.execute(new String[] { "1", "2", "3" });
assertEquals(new Integer(1), result[0]); assertEquals(new Integer(1), result[0]);
assertEquals(new Integer(2), result[1]); assertEquals(new Integer(2), result[1]);
assertEquals(new Integer(3), result[2]); assertEquals(new Integer(3), result[2]);
...@@ -162,9 +136,8 @@ public class GenericConversionServiceTests { ...@@ -162,9 +136,8 @@ public class GenericConversionServiceTests {
@Test @Test
public void convertArrayToPrimitiveArray() { public void convertArrayToPrimitiveArray() {
service.addConverter(new StringToInteger()); converter.addConverter(new StringToInteger());
ConversionExecutor executor = service.getConversionExecutor(String[].class, type(int[].class)); int[] result = (int[]) converter.convert(new String[] { "1", "2", "3" }, int[].class);
int[] result = (int[]) executor.execute(new String[] { "1", "2", "3" });
assertEquals(1, result[0]); assertEquals(1, result[0]);
assertEquals(2, result[1]); assertEquals(2, result[1]);
assertEquals(3, result[2]); assertEquals(3, result[2]);
...@@ -172,8 +145,7 @@ public class GenericConversionServiceTests { ...@@ -172,8 +145,7 @@ public class GenericConversionServiceTests {
@Test @Test
public void convertArrayToListInterface() { public void convertArrayToListInterface() {
ConversionExecutor executor = service.getConversionExecutor(String[].class, type(List.class)); List<?> result = converter.convert(new String[] { "1", "2", "3" }, List.class);
List result = (List) executor.execute(new String[] { "1", "2", "3" });
assertEquals("1", result.get(0)); assertEquals("1", result.get(0));
assertEquals("2", result.get(1)); assertEquals("2", result.get(1));
assertEquals("3", result.get(2)); assertEquals("3", result.get(2));
...@@ -183,10 +155,8 @@ public class GenericConversionServiceTests { ...@@ -183,10 +155,8 @@ public class GenericConversionServiceTests {
@Test @Test
public void convertArrayToListGenericTypeConversion() throws Exception { public void convertArrayToListGenericTypeConversion() throws Exception {
service.addConverter(new StringToInteger()); converter.addConverter(new StringToInteger());
ConversionExecutor executor = service.getConversionExecutor(String[].class, new TypeDescriptor(getClass() List<Integer> result = converter.convert(new String[] { "1", "2", "3" }, new BindingPoint<List<Integer>>(getClass().getDeclaredField("genericList")));
.getDeclaredField("genericList")));
List result = (List) executor.execute(new String[] { "1", "2", "3" });
assertEquals(new Integer("1"), result.get(0)); assertEquals(new Integer("1"), result.get(0));
assertEquals(new Integer("2"), result.get(1)); assertEquals(new Integer("2"), result.get(1));
assertEquals(new Integer("3"), result.get(2)); assertEquals(new Integer("3"), result.get(2));
...@@ -194,8 +164,7 @@ public class GenericConversionServiceTests { ...@@ -194,8 +164,7 @@ public class GenericConversionServiceTests {
@Test @Test
public void convertArrayToListImpl() { public void convertArrayToListImpl() {
ConversionExecutor executor = service.getConversionExecutor(String[].class, type(LinkedList.class)); LinkedList<?> result = converter.convert(new String[] { "1", "2", "3" }, LinkedList.class);
LinkedList result = (LinkedList) executor.execute(new String[] { "1", "2", "3" });
assertEquals("1", result.get(0)); assertEquals("1", result.get(0));
assertEquals("2", result.get(1)); assertEquals("2", result.get(1));
assertEquals("3", result.get(2)); assertEquals("3", result.get(2));
...@@ -204,7 +173,7 @@ public class GenericConversionServiceTests { ...@@ -204,7 +173,7 @@ public class GenericConversionServiceTests {
@Test @Test
public void convertArrayToAbstractList() { public void convertArrayToAbstractList() {
try { try {
service.getConversionExecutor(String[].class, type(AbstractList.class)); converter.convert(new String[] { "1", "2", "3" }, AbstractList.class);
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
} }
...@@ -212,12 +181,11 @@ public class GenericConversionServiceTests { ...@@ -212,12 +181,11 @@ public class GenericConversionServiceTests {
@Test @Test
public void convertListToArray() { public void convertListToArray() {
ConversionExecutor executor = service.getConversionExecutor(Collection.class, type(String[].class)); List<String> list = new ArrayList<String>();
List list = new ArrayList();
list.add("1"); list.add("1");
list.add("2"); list.add("2");
list.add("3"); list.add("3");
String[] result = (String[]) executor.execute(list); String[] result = (String[]) converter.convert(list, String[].class);
assertEquals("1", result[0]); assertEquals("1", result[0]);
assertEquals("2", result[1]); assertEquals("2", result[1]);
assertEquals("3", result[2]); assertEquals("3", result[2]);
...@@ -225,13 +193,12 @@ public class GenericConversionServiceTests { ...@@ -225,13 +193,12 @@ public class GenericConversionServiceTests {
@Test @Test
public void convertListToArrayWithComponentConversion() { public void convertListToArrayWithComponentConversion() {
service.addConverter(new StringToInteger()); converter.addConverter(new StringToInteger());
ConversionExecutor executor = service.getConversionExecutor(Collection.class, type(Integer[].class)); List<String> list = new ArrayList<String>();
List list = new ArrayList();
list.add("1"); list.add("1");
list.add("2"); list.add("2");
list.add("3"); 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(1), result[0]);
assertEquals(new Integer(2), result[1]); assertEquals(new Integer(2), result[1]);
assertEquals(new Integer(3), result[2]); assertEquals(new Integer(3), result[2]);
...@@ -245,16 +212,17 @@ public class GenericConversionServiceTests { ...@@ -245,16 +212,17 @@ public class GenericConversionServiceTests {
Map<String, String> foo = new HashMap<String, String>(); Map<String, String> foo = new HashMap<String, String>();
foo.put("1", "BAR"); foo.put("1", "BAR");
foo.put("2", "BAZ"); foo.put("2", "BAZ");
service.addConverter(new StringToInteger()); converter.addConverter(new StringToInteger());
service.addConverter(new StringToEnumFactory().getConverter(FooEnum.class)); converter.addConverter(new StringToEnumFactory().getConverter(FooEnum.class));
service.convert(foo, new TypeDescriptor(getClass().getField("genericMap"))); Map<String, FooEnum> map = converter.convert(foo, new BindingPoint<Map<String, FooEnum>>(getClass().getField("genericMap")));
assertEquals(map.get(1), FooEnum.BAR);
assertEquals(map.get(2), FooEnum.BAZ);
} }
@Ignore @Ignore
@Test @Test
public void convertObjectToArray() { public void convertObjectToArray() {
ConversionExecutor executor = service.getConversionExecutor(String.class, type(String[].class)); String[] result = (String[]) converter.convert("1,2,3", String[].class);
String[] result = (String[]) executor.execute("1,2,3");
assertEquals(1, result.length); assertEquals(1, result.length);
assertEquals("1,2,3", result[0]); assertEquals("1,2,3", result[0]);
} }
...@@ -262,9 +230,8 @@ public class GenericConversionServiceTests { ...@@ -262,9 +230,8 @@ public class GenericConversionServiceTests {
@Ignore @Ignore
@Test @Test
public void convertObjectToArrayWithElementConversion() { public void convertObjectToArrayWithElementConversion() {
service.addConverter(new StringToInteger()); converter.addConverter(new StringToInteger());
ConversionExecutor executor = service.getConversionExecutor(String.class, type(Integer[].class)); Integer[] result = converter.convert("123", Integer[].class);
Integer[] result = (Integer[]) executor.execute("123");
assertEquals(1, result.length); assertEquals(1, result.length);
assertEquals(new Integer(123), result[0]); assertEquals(new Integer(123), result[0]);
} }
...@@ -273,8 +240,4 @@ public class GenericConversionServiceTests { ...@@ -273,8 +240,4 @@ public class GenericConversionServiceTests {
BAR, BAZ BAR, BAZ
} }
private TypeDescriptor type(Class<?> clazz) {
return TypeDescriptor.valueOf(clazz);
}
} }
\ No newline at end of file
...@@ -6,29 +6,29 @@ import java.util.HashMap; ...@@ -6,29 +6,29 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
import org.junit.Test; import org.junit.Test;
import org.springframework.core.convert.TypeDescriptor; import org.springframework.core.convert.BindingPoint;
import org.springframework.core.convert.support.DefaultConversionService; import org.springframework.core.convert.support.DefaultTypeConverter;
import org.springframework.core.convert.support.MapToMap; import org.springframework.core.convert.support.MapToMap;
public class MapToMapTests { public class MapToMapTests {
@Test @Test
public void testMapToMapConversion() throws Exception { public void testMapToMapConversion() throws Exception {
DefaultConversionService service = new DefaultConversionService(); DefaultTypeConverter converter = new DefaultTypeConverter();
MapToMap c = new MapToMap(new TypeDescriptor(getClass().getField("source")), MapToMap c = new MapToMap(new BindingPoint<Map<String, String>>(getClass().getField("source")),
new TypeDescriptor(getClass().getField("bindTarget")), service); new BindingPoint<Map<String, FooEnum>>(getClass().getField("bindTarget")), converter);
source.put("1", "BAR"); source.put("1", "BAR");
source.put("2", "BAZ"); source.put("2", "BAZ");
Map result = (Map) c.execute(source); Map<String, FooEnum> result = (Map<String, FooEnum>) c.execute(source);
assertEquals(FooEnum.BAR, result.get(1)); assertEquals(FooEnum.BAR, result.get(1));
assertEquals(FooEnum.BAZ, result.get(2)); assertEquals(FooEnum.BAZ, result.get(2));
} }
@Test @Test
public void testMapToMapConversionNoGenericInfoOnSource() throws Exception { public void testMapToMapConversionNoGenericInfoOnSource() throws Exception {
DefaultConversionService service = new DefaultConversionService(); DefaultTypeConverter service = new DefaultTypeConverter();
MapToMap c = new MapToMap(TypeDescriptor.valueOf(Map.class), MapToMap c = new MapToMap(BindingPoint.valueOf(Map.class),
new TypeDescriptor(getClass().getField("bindTarget")), service); new BindingPoint(getClass().getField("bindTarget")), service);
source.put("1", "BAR"); source.put("1", "BAR");
source.put("2", "BAZ"); source.put("2", "BAZ");
Map result = (Map) c.execute(source); Map result = (Map) c.execute(source);
...@@ -38,9 +38,9 @@ public class MapToMapTests { ...@@ -38,9 +38,9 @@ public class MapToMapTests {
@Test @Test
public void testMapToMapConversionNoGenericInfo() throws Exception { public void testMapToMapConversionNoGenericInfo() throws Exception {
DefaultConversionService service = new DefaultConversionService(); DefaultTypeConverter service = new DefaultTypeConverter();
MapToMap c = new MapToMap(TypeDescriptor.valueOf(Map.class), MapToMap c = new MapToMap(BindingPoint.valueOf(Map.class),
TypeDescriptor.valueOf(Map.class), service); BindingPoint.valueOf(Map.class), service);
source.put("1", "BAR"); source.put("1", "BAR");
source.put("2", "BAZ"); source.put("2", "BAZ");
Map result = (Map) c.execute(source); Map result = (Map) c.execute(source);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册