renamed back to TypeDescriptor after ConversionContext proved not good enough name for any change

This commit is contained in:
Keith Donald
2009-05-23 21:23:10 +00:00
parent b9aa82a0c4
commit b093094c50
35 changed files with 200 additions and 205 deletions

View File

@@ -19,8 +19,7 @@ package org.springframework.core.convert;
* A service interface for type conversion. This is the entry point into the convert system.
* <p>
* Call {@link #convert(Object, Class)} to perform a thread-safe type conversion using this system.<br>
* Call {@link #convert(Object, ConversionContext)} to perform a conversion with additional context about the point
* where conversion needs to occur.
* Call {@link #convert(Object, TypeDescriptor)} to perform a conversion with additional context about the targetType to convert to.
*
* @author Keith Donald
*/
@@ -37,10 +36,10 @@ public interface TypeConverter {
/**
* Returns true if objects of sourceType can be converted to the type of the conversion point.
* @param source the source to convert from (may be null)
* @param context context about the point where conversion would occur
* @param targetType context about the target type to convert to
* @return true if a conversion can be performed, false if not
*/
boolean canConvert(Class<?> sourceType, ConversionContext<?> context);
boolean canConvert(Class<?> sourceType, TypeDescriptor<?> targetType);
/**
* Convert the source to targetType.
@@ -54,10 +53,10 @@ public interface TypeConverter {
/**
* Convert the source to type T needed by the conversion point.
* @param source the source to convert from (may be null)
* @param context context about the point where conversion will occur
* @return the converted object, an instance of {@link ConversionContext#getType()}</code>, or <code>null</code> if a null source was provided
* @param targetType context about 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 was provided
* @throws ConvertException if an exception occurred
*/
<S, T> T convert(S source, ConversionContext<T> context);
<S, T> T convert(S source, TypeDescriptor<T> targetType);
}

View File

@@ -27,19 +27,17 @@ import org.springframework.util.Assert;
// TODO doesn't support more than depth of one (eg. Map<String,List<Foo>> or List<String>[])
/**
* Context about a point where conversion needs to be performed. Provides context about the point such
* as field or method parameter information.
*
* Context about a type to convert to.
* @author Keith Donald
* @author Andy Clement
*/
public class ConversionContext<T> {
public class TypeDescriptor<T> {
/**
* Constant value for the null object
*/
@SuppressWarnings("unchecked")
public final static ConversionContext NULL = new ConversionContext((Class<?>) null);
public final static TypeDescriptor NULL = new TypeDescriptor((Class<?>) null);
private MethodParameter methodParameter;
@@ -50,29 +48,30 @@ public class ConversionContext<T> {
private Class<?> type;
/**
* Creates a new context for the given type. Use this constructor when a conversion point comes from a source such as
* a Map or collection, where no additional binding metadata is available.
* Creates a new descriptor for the given type.
* Use this constructor when a conversion point comes from a source such as a Map or collection, where no additional context is available.
* @param type the actual type
*/
public ConversionContext(Class<?> type) {
public TypeDescriptor(Class<?> type) {
this.type = type;
}
/**
* Create a new context for a method or constructor parameter. Use this constructor when a conversion point originates
* from a method parameter, such as a setter method argument.
* Create a new type descriptor from a method or constructor parameter.
* Use this constructor when a target conversion point originates from a method parameter, such as a setter method argument.
* @param methodParameter the MethodParameter to wrap
*/
public ConversionContext(MethodParameter methodParameter) {
public TypeDescriptor(MethodParameter methodParameter) {
Assert.notNull(methodParameter, "MethodParameter must not be null");
this.methodParameter = methodParameter;
}
/**
* Create a new context for a field. Use this constructor when a conversion point originates from a field.
* Create a new type descriptor for a field.
* Use this constructor when a target conversion point originates from a field.
* @param field the field to wrap
*/
public ConversionContext(Field field) {
public TypeDescriptor(Field field) {
Assert.notNull(field, "Field must not be null");
this.field = field;
}
@@ -246,7 +245,7 @@ public class ConversionContext<T> {
* @return true if this type is assignable to the target
*/
@SuppressWarnings("unchecked")
public boolean isAssignableTo(ConversionContext targetType) {
public boolean isAssignableTo(TypeDescriptor targetType) {
return targetType.getType().isAssignableFrom(getType());
}
@@ -255,9 +254,9 @@ public class ConversionContext<T> {
* @param type the class
* @return the type descriptor
*/
public static <T> ConversionContext<T> valueOf(Class<T> type) {
public static <T> TypeDescriptor<T> valueOf(Class<T> type) {
// TODO needs a cache for common type descriptors
return new ConversionContext<T>(type);
return new TypeDescriptor<T>(type);
}
/**
@@ -266,7 +265,7 @@ public class ConversionContext<T> {
* @return the type descriptor
*/
@SuppressWarnings("unchecked")
public static ConversionContext forObject(Object object) {
public static TypeDescriptor forObject(Object object) {
if (object == null) {
return NULL;
} else {

View File

@@ -16,7 +16,7 @@
package org.springframework.core.convert.support;
import org.springframework.core.convert.ConversionFailedException;
import org.springframework.core.convert.ConversionContext;
import org.springframework.core.convert.TypeDescriptor;
/**
* Base class for converters that convert to and from collection types (arrays and java.util.Collection types)
@@ -28,18 +28,18 @@ abstract class AbstractCollectionConverter implements ConversionExecutor {
private ConversionExecutor elementConverter;
private ConversionContext sourceCollectionType;
private TypeDescriptor sourceCollectionType;
private ConversionContext targetCollectionType;
private TypeDescriptor targetCollectionType;
public AbstractCollectionConverter(ConversionContext sourceCollectionType, ConversionContext targetCollectionType, GenericTypeConverter conversionService) {
public AbstractCollectionConverter(TypeDescriptor sourceCollectionType, TypeDescriptor 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, ConversionContext.valueOf(targetElementType));
elementConverter = conversionService.getConversionExecutor(sourceElementType, TypeDescriptor.valueOf(targetElementType));
} else {
elementConverter = NoOpConversionExecutor.INSTANCE;
}

View File

@@ -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.ConversionContext;
import org.springframework.core.convert.TypeDescriptor;
/**
* 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.ConversionContext;
*/
class ArrayToArray extends AbstractCollectionConverter {
public ArrayToArray(ConversionContext sourceArrayType, ConversionContext targetArrayType, GenericTypeConverter conversionService) {
public ArrayToArray(TypeDescriptor sourceArrayType, TypeDescriptor targetArrayType, GenericTypeConverter conversionService) {
super(sourceArrayType, targetArrayType, conversionService);
}

View File

@@ -18,7 +18,7 @@ package org.springframework.core.convert.support;
import java.lang.reflect.Array;
import java.util.Collection;
import org.springframework.core.convert.ConversionContext;
import org.springframework.core.convert.TypeDescriptor;
/**
* Special converter that converts from a source array to a target collection. Supports the selection of an
@@ -29,7 +29,7 @@ import org.springframework.core.convert.ConversionContext;
*/
class ArrayToCollection extends AbstractCollectionConverter {
public ArrayToCollection(ConversionContext sourceArrayType, ConversionContext targetCollectionType,
public ArrayToCollection(TypeDescriptor sourceArrayType, TypeDescriptor targetCollectionType,
GenericTypeConverter conversionService) {
super(sourceArrayType, targetCollectionType, conversionService);
}

View File

@@ -19,7 +19,7 @@ import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Iterator;
import org.springframework.core.convert.ConversionContext;
import org.springframework.core.convert.TypeDescriptor;
/**
* Special converter that converts from target collection to a source array.
@@ -28,7 +28,7 @@ import org.springframework.core.convert.ConversionContext;
*/
class CollectionToArray extends AbstractCollectionConverter {
public CollectionToArray(ConversionContext sourceArrayType, ConversionContext targetCollectionType,
public CollectionToArray(TypeDescriptor sourceArrayType, TypeDescriptor 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(), ConversionContext.valueOf(getTargetElementType()));
elementConverter = getConversionService().getConversionExecutor(value.getClass(), TypeDescriptor.valueOf(getTargetElementType()));
break;
}
}

View File

@@ -18,7 +18,7 @@ package org.springframework.core.convert.support;
import java.util.Collection;
import java.util.Iterator;
import org.springframework.core.convert.ConversionContext;
import org.springframework.core.convert.TypeDescriptor;
/**
* A converter that can convert from one collection type to another.
@@ -27,7 +27,7 @@ import org.springframework.core.convert.ConversionContext;
*/
class CollectionToCollection extends AbstractCollectionConverter {
public CollectionToCollection(ConversionContext sourceCollectionType, ConversionContext targetCollectionType,
public CollectionToCollection(TypeDescriptor sourceCollectionType, TypeDescriptor 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(), ConversionContext.valueOf(getTargetElementType()));
elementConverter = getConversionService().getConversionExecutor(value.getClass(), TypeDescriptor.valueOf(getTargetElementType()));
break;
}
}

View File

@@ -26,7 +26,7 @@ import java.util.List;
import java.util.Map;
import org.springframework.core.GenericTypeResolver;
import org.springframework.core.convert.ConversionContext;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.ConverterNotFoundException;
import org.springframework.core.convert.TypeConverter;
import org.springframework.core.convert.converter.Converter;
@@ -52,9 +52,6 @@ public class GenericTypeConverter implements TypeConverter, ConverterRegistry {
*/
private final Map sourceTypeConverters = new HashMap();
/**
* An optional parent conversion service.
*/
private TypeConverter parent;
/**
@@ -108,16 +105,16 @@ public class GenericTypeConverter implements TypeConverter, ConverterRegistry {
// implementing TypeConverter
public boolean canConvert(Class<?> sourceType, Class<?> targetType) {
return canConvert(sourceType, ConversionContext.valueOf(targetType));
return canConvert(sourceType, TypeDescriptor.valueOf(targetType));
}
public boolean canConvert(Class<?> sourceType, ConversionContext<?> context) {
ConversionExecutor executor = getConversionExecutor(sourceType, context);
public boolean canConvert(Class<?> sourceType, TypeDescriptor<?> targetType) {
ConversionExecutor executor = getConversionExecutor(sourceType, targetType);
if (executor != null) {
return true;
} else {
if (parent != null) {
return parent.canConvert(sourceType, context);
return parent.canConvert(sourceType, targetType);
} else {
return false;
}
@@ -125,28 +122,28 @@ public class GenericTypeConverter implements TypeConverter, ConverterRegistry {
}
public <S, T> T convert(S source, Class<T> targetType) {
return convert(source, ConversionContext.valueOf(targetType));
return convert(source, TypeDescriptor.valueOf(targetType));
}
public <S, T> T convert(S source, ConversionContext<T> context) {
public <S, T> T convert(S source, TypeDescriptor<T> targetType) {
if (source == null) {
return null;
}
ConversionExecutor executor = getConversionExecutor(source.getClass(), context);
ConversionExecutor executor = getConversionExecutor(source.getClass(), targetType);
if (executor != null) {
return (T) executor.execute(source);
} else {
if (parent != null) {
return parent.convert(source, context);
return parent.convert(source, targetType);
} else {
throw new ConverterNotFoundException(source.getClass(), context.getType(),
throw new ConverterNotFoundException(source.getClass(), targetType.getType(),
"No converter found that can convert from sourceType [" + source.getClass().getName()
+ "] to targetType [" + context.getName() + "]");
+ "] to targetType [" + targetType.getName() + "]");
}
}
}
ConversionExecutor getConversionExecutor(Class sourceClass, ConversionContext targetType)
ConversionExecutor getConversionExecutor(Class sourceClass, TypeDescriptor targetType)
throws ConverterNotFoundException {
Assert.notNull(sourceClass, "The sourceType to convert from is required");
Assert.notNull(targetType, "The targetType to convert to is required");
@@ -154,7 +151,7 @@ public class GenericTypeConverter implements TypeConverter, ConverterRegistry {
// TODO for Andy - is this correct way to handle the Null TypedValue?
return NoOpConversionExecutor.INSTANCE;
}
ConversionContext sourceType = ConversionContext.valueOf(sourceClass);
TypeDescriptor sourceType = TypeDescriptor.valueOf(sourceClass);
if (sourceType.isArray()) {
if (targetType.isArray()) {
return new ArrayToArray(sourceType, targetType, this);

View File

@@ -22,7 +22,7 @@ import java.util.SortedMap;
import java.util.TreeMap;
import org.springframework.core.convert.ConversionFailedException;
import org.springframework.core.convert.ConversionContext;
import org.springframework.core.convert.TypeDescriptor;
/**
* Converts from one map to another map, with support for converting individual map elements based on generic type information.
@@ -31,9 +31,9 @@ import org.springframework.core.convert.ConversionContext;
@SuppressWarnings("unchecked")
class MapToMap implements ConversionExecutor {
private ConversionContext sourceType;
private TypeDescriptor sourceType;
private ConversionContext targetType;
private TypeDescriptor targetType;
private GenericTypeConverter conversionService;
@@ -45,7 +45,7 @@ class MapToMap implements ConversionExecutor {
* @param targetType the target map type
* @param conversionService the conversion service
*/
public MapToMap(ConversionContext sourceType, ConversionContext targetType, GenericTypeConverter conversionService) {
public MapToMap(TypeDescriptor sourceType, TypeDescriptor targetType, GenericTypeConverter conversionService) {
this.sourceType = sourceType;
this.targetType = targetType;
this.conversionService = conversionService;
@@ -55,9 +55,9 @@ class MapToMap implements ConversionExecutor {
private EntryConverter createEntryConverter() {
if (sourceType.isMapEntryTypeKnown() && targetType.isMapEntryTypeKnown()) {
ConversionExecutor keyConverter = conversionService.getConversionExecutor(sourceType.getMapKeyType(),
ConversionContext.valueOf(targetType.getMapKeyType()));
TypeDescriptor.valueOf(targetType.getMapKeyType()));
ConversionExecutor valueConverter = conversionService.getConversionExecutor(sourceType.getMapValueType(),
ConversionContext.valueOf(targetType.getMapValueType()));
TypeDescriptor.valueOf(targetType.getMapValueType()));
return new EntryConverter(keyConverter, valueConverter);
} else {
return EntryConverter.NO_OP_INSTANCE;
@@ -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(), ConversionContext
keyConverter = conversionService.getConversionExecutor(key.getClass(), TypeDescriptor
.valueOf(targetKeyType));
}
if (valueConverter == null && value != null) {
valueConverter = conversionService.getConversionExecutor(value.getClass(), ConversionContext
valueConverter = conversionService.getConversionExecutor(value.getClass(), TypeDescriptor
.valueOf(targetValueType));
}
if (keyConverter != null && valueConverter != null) {

View File

@@ -16,7 +16,7 @@
package org.springframework.core.convert.support;
import org.springframework.core.convert.ConversionFailedException;
import org.springframework.core.convert.ConversionContext;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.style.ToStringCreator;
@@ -27,13 +27,13 @@ import org.springframework.core.style.ToStringCreator;
@SuppressWarnings("unchecked")
class StaticConversionExecutor implements ConversionExecutor {
private final ConversionContext sourceType;
private final TypeDescriptor sourceType;
private final ConversionContext targetType;
private final TypeDescriptor targetType;
private final Converter converter;
public StaticConversionExecutor(ConversionContext sourceType, ConversionContext targetType, Converter converter) {
public StaticConversionExecutor(TypeDescriptor sourceType, TypeDescriptor targetType, Converter converter) {
this.sourceType = sourceType;
this.targetType = targetType;
this.converter = converter;