removed custom converter for m3 to add back in rc1; also ensure type descriptor get type always returns wrapper types if primitive
This commit is contained in:
@@ -54,21 +54,6 @@ public interface ConversionService {
|
||||
public Object executeConversion(Object source, TypeDescriptor targetType) throws ConversionExecutorNotFoundException,
|
||||
ConversionException;
|
||||
|
||||
/**
|
||||
* Convert the source to targetType using a custom converter.
|
||||
* @param converterId the id of the custom converter, which must be registered with this conversion service and
|
||||
* capable of converting to the targetType
|
||||
* @param source the source to convert from (may be null)
|
||||
* @param targetType the target type to convert to
|
||||
* @return the converted object, an instance of the <code>targetType</code>, or <code>null</code> if a null source
|
||||
* was provided
|
||||
* @throws ConversionExecutorNotFoundException if no suitable conversion executor could be found to convert the
|
||||
* source to an instance of targetType
|
||||
* @throws ConversionException if an exception occurred during the conversion process
|
||||
*/
|
||||
public Object executeConversion(String converterId, Object source, TypeDescriptor targetType)
|
||||
throws ConversionExecutorNotFoundException, ConversionException;
|
||||
|
||||
/**
|
||||
* Get a ConversionExecutor that converts objects from sourceType to targetType.
|
||||
* The returned ConversionExecutor is thread-safe and may safely be cached for later use by client code.
|
||||
@@ -80,19 +65,6 @@ public interface ConversionService {
|
||||
public ConversionExecutor getConversionExecutor(Class<?> sourceType, TypeDescriptor targetType)
|
||||
throws ConversionExecutorNotFoundException;
|
||||
|
||||
/**
|
||||
* Get a ConversionExecutor that converts objects from from sourceType to targetType using a custom converter.
|
||||
* The returned ConversionExecutor is thread-safe and may safely be cached for use in client code.
|
||||
* @param converterId the id of the custom converter, which must be registered with this conversion service and
|
||||
* capable of converting from sourceType to targetType (required)
|
||||
* @param sourceType the source type to convert from (required)
|
||||
* @param targetType the target type to convert to (required)
|
||||
* @return the executor that can execute instance type conversion, never null
|
||||
* @throws ConversionExecutorNotFoundException when no suitable conversion executor could be found
|
||||
*/
|
||||
public ConversionExecutor getConversionExecutor(String converterId, Class<?> sourceType,
|
||||
TypeDescriptor targetType) throws ConversionExecutorNotFoundException;
|
||||
|
||||
/**
|
||||
* Get a type by its name; may be the fully-qualified class name or a registered type alias such as 'int'.
|
||||
* @return the class, or <code>null</code> if no such name exists
|
||||
|
||||
@@ -31,13 +31,11 @@ import org.springframework.util.Assert;
|
||||
*
|
||||
* @author Keith Donald
|
||||
* @author Andy Clement
|
||||
*
|
||||
* @since 3.0
|
||||
*/
|
||||
public class TypeDescriptor {
|
||||
|
||||
/**
|
||||
* 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);
|
||||
|
||||
@@ -84,50 +82,16 @@ public class TypeDescriptor {
|
||||
*/
|
||||
public Class<?> getType() {
|
||||
if (type != null) {
|
||||
return type;
|
||||
return wrapperType(type);
|
||||
} else if (field != null) {
|
||||
return field.getType();
|
||||
return wrapperType(field.getType());
|
||||
} else if (methodParameter != null) {
|
||||
return methodParameter.getParameterType();
|
||||
return wrapperType(methodParameter.getParameterType());
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* If the actual type is a primitive, returns its wrapper type, else just returns {@link #getType()}.
|
||||
* @return the wrapper type if the underlying type is a primitive, else the actual type as-is
|
||||
*/
|
||||
public Class<?> getWrapperTypeIfPrimitive() {
|
||||
Class<?> type = getType();
|
||||
if (type == null) {
|
||||
return null;
|
||||
}
|
||||
if (type.isPrimitive()) {
|
||||
if (type.equals(int.class)) {
|
||||
return Integer.class;
|
||||
} else if (type.equals(short.class)) {
|
||||
return Short.class;
|
||||
} else if (type.equals(long.class)) {
|
||||
return Long.class;
|
||||
} else if (type.equals(float.class)) {
|
||||
return Float.class;
|
||||
} else if (type.equals(double.class)) {
|
||||
return Double.class;
|
||||
} else if (type.equals(byte.class)) {
|
||||
return Byte.class;
|
||||
} else if (type.equals(boolean.class)) {
|
||||
return Boolean.class;
|
||||
} else if (type.equals(char.class)) {
|
||||
return Character.class;
|
||||
} else {
|
||||
throw new IllegalStateException("Should never happen - primitive type is not a primitive?");
|
||||
}
|
||||
} else {
|
||||
return type;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the name of this type; the fully qualified classname.
|
||||
*/
|
||||
@@ -343,7 +307,33 @@ public class TypeDescriptor {
|
||||
}
|
||||
|
||||
// internal helpers
|
||||
|
||||
|
||||
private Class<?> wrapperType(Class<?> type) {
|
||||
if (type.isPrimitive()) {
|
||||
if (type.equals(int.class)) {
|
||||
return Integer.class;
|
||||
} else if (type.equals(short.class)) {
|
||||
return Short.class;
|
||||
} else if (type.equals(long.class)) {
|
||||
return Long.class;
|
||||
} else if (type.equals(float.class)) {
|
||||
return Float.class;
|
||||
} else if (type.equals(double.class)) {
|
||||
return Double.class;
|
||||
} else if (type.equals(byte.class)) {
|
||||
return Byte.class;
|
||||
} else if (type.equals(boolean.class)) {
|
||||
return Boolean.class;
|
||||
} else if (type.equals(char.class)) {
|
||||
return Character.class;
|
||||
} else {
|
||||
throw new IllegalStateException("Should never happen - primitive type is not a primitive?");
|
||||
}
|
||||
} else {
|
||||
return type;
|
||||
}
|
||||
}
|
||||
|
||||
private Class<?> getArrayComponentType() {
|
||||
return getType().getComponentType();
|
||||
}
|
||||
|
||||
@@ -51,21 +51,15 @@ public class GenericConversionService implements ConversionService {
|
||||
|
||||
/**
|
||||
* An indexed map of Converters. Each Map.Entry key is a source class (S) that can be converted from. Each Map.Entry
|
||||
* value is a Map that defines the targetClass-to-Converter mappings for that source.
|
||||
* value is a Map that defines the targetType-to-Converter mappings for that source.
|
||||
*/
|
||||
private final Map sourceClassConverters = new HashMap();
|
||||
private final Map sourceTypeConverters = new HashMap();
|
||||
|
||||
/**
|
||||
* An indexed map of SuperConverters. Each Map.Entry key is a source class (S) that can be converted from. Each
|
||||
* Map.Entry value is a Map that defines the targetClass-to-SuperConverter mappings for that source.
|
||||
* Map.Entry value is a Map that defines the targetType-to-SuperConverter mappings for that source.
|
||||
*/
|
||||
private final Map sourceClassSuperConverters = new HashMap();
|
||||
|
||||
/**
|
||||
* A map of custom converters. Custom converters are assigned a unique identifier that can be used to lookup the
|
||||
* converter. This allows multiple converters for the same source->target class to be registered.
|
||||
*/
|
||||
private final Map customConverters = new HashMap();
|
||||
private final Map sourceTypeSuperConverters = new HashMap();
|
||||
|
||||
/**
|
||||
* Indexes classes by well-known aliases.
|
||||
@@ -97,14 +91,14 @@ public class GenericConversionService implements ConversionService {
|
||||
*/
|
||||
public void addConverter(Converter converter) {
|
||||
List typeInfo = getRequiredTypeInfo(converter);
|
||||
Class sourceClass = (Class) typeInfo.get(0);
|
||||
Class targetClass = (Class) typeInfo.get(1);
|
||||
Class sourceType = (Class) typeInfo.get(0);
|
||||
Class targetType = (Class) typeInfo.get(1);
|
||||
// index forward
|
||||
Map sourceMap = getSourceMap(sourceClass);
|
||||
sourceMap.put(targetClass, converter);
|
||||
Map sourceMap = getSourceMap(sourceType);
|
||||
sourceMap.put(targetType, converter);
|
||||
// index reverse
|
||||
sourceMap = getSourceMap(targetClass);
|
||||
sourceMap.put(sourceClass, new ReverseConverter(converter));
|
||||
sourceMap = getSourceMap(targetType);
|
||||
sourceMap.put(sourceType, new ReverseConverter(converter));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -113,38 +107,29 @@ public class GenericConversionService implements ConversionService {
|
||||
*/
|
||||
public void addConverter(SuperConverter converter) {
|
||||
List typeInfo = getRequiredTypeInfo(converter);
|
||||
Class sourceClass = (Class) typeInfo.get(0);
|
||||
Class targetClass = (Class) typeInfo.get(1);
|
||||
Class sourceType = (Class) typeInfo.get(0);
|
||||
Class targetType = (Class) typeInfo.get(1);
|
||||
// index forward
|
||||
Map sourceMap = getSourceSuperConverterMap(sourceClass);
|
||||
sourceMap.put(targetClass, converter);
|
||||
Map sourceMap = getSourceSuperConverterMap(sourceType);
|
||||
sourceMap.put(targetType, converter);
|
||||
if (converter instanceof SuperTwoWayConverter) {
|
||||
// index reverse
|
||||
sourceMap = getSourceSuperConverterMap(targetClass);
|
||||
sourceMap.put(sourceClass, new ReverseSuperConverter((SuperTwoWayConverter) converter));
|
||||
sourceMap = getSourceSuperConverterMap(targetType);
|
||||
sourceMap.put(sourceType, new ReverseSuperConverter((SuperTwoWayConverter) converter));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the converter as a custom converter with this conversion service.
|
||||
* @param id the id to assign the converter
|
||||
* @param converter the converter to use a custom converter
|
||||
*/
|
||||
public void addConverter(String id, Converter converter) {
|
||||
customConverters.put(id, converter);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adapts a {@link SuperTwoWayConverter} that converts between BS and BT class hierarchies to a {@link Converter}
|
||||
* that converts between the specific BS/BT sub types S and T.
|
||||
* @param sourceClass the source class S to convert from, which must be equal or extend BS
|
||||
* @param targetClass the target type T to convert to, which must equal or extend BT
|
||||
* @param sourceType the source class S to convert from, which must be equal or extend BS
|
||||
* @param targetType the target type T to convert to, which must equal or extend BT
|
||||
* @param converter the super two way converter
|
||||
* @return a converter that converts from S to T by delegating to the super converter
|
||||
*/
|
||||
public static <S, T> Converter<S, T> converterFor(Class<S> sourceClass, Class<T> targetClass,
|
||||
public static <S, T> Converter<S, T> converterFor(Class<S> sourceType, Class<T> targetType,
|
||||
SuperTwoWayConverter converter) {
|
||||
return new SuperTwoWayConverterConverter(converter, sourceClass, targetClass);
|
||||
return new SuperTwoWayConverterConverter(converter, sourceType, targetType);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -158,12 +143,25 @@ public class GenericConversionService implements ConversionService {
|
||||
|
||||
// implementing ConversionService
|
||||
|
||||
public boolean canConvert(Class<?> source, TypeDescriptor targetType) {
|
||||
return false;
|
||||
public boolean canConvert(Class<?> sourceType, TypeDescriptor targetType) {
|
||||
try {
|
||||
getConversionExecutor(sourceType, targetType);
|
||||
return true;
|
||||
} catch (ConversionExecutorNotFoundException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean canConvert(Object source, TypeDescriptor targetType) {
|
||||
return false;
|
||||
if (source == null) {
|
||||
return true;
|
||||
}
|
||||
try {
|
||||
getConversionExecutor(source.getClass(), targetType);
|
||||
return true;
|
||||
} catch (ConversionExecutorNotFoundException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public Object executeConversion(Object source, TypeDescriptor targetType)
|
||||
@@ -174,14 +172,6 @@ public class GenericConversionService implements ConversionService {
|
||||
return getConversionExecutor(source.getClass(), targetType).execute(source);
|
||||
}
|
||||
|
||||
public Object executeConversion(String converterId, Object source, TypeDescriptor targetType)
|
||||
throws ConversionExecutorNotFoundException, ConversionException {
|
||||
if (source == null) {
|
||||
return null;
|
||||
}
|
||||
return getConversionExecutor(converterId, source.getClass(), targetType).execute(source);
|
||||
}
|
||||
|
||||
public ConversionExecutor getConversionExecutor(Class sourceClass, TypeDescriptor targetType)
|
||||
throws ConversionExecutorNotFoundException {
|
||||
Assert.notNull(sourceClass, "The sourceType to convert from is required");
|
||||
@@ -220,11 +210,11 @@ public class GenericConversionService implements ConversionService {
|
||||
throw new UnsupportedOperationException("Object to Map conversion not yet supported");
|
||||
}
|
||||
}
|
||||
Converter converter = findRegisteredConverter(sourceType, targetType);
|
||||
Converter converter = findRegisteredConverter(sourceClass, targetType.getType());
|
||||
if (converter != null) {
|
||||
return new StaticConversionExecutor(sourceType, targetType, converter);
|
||||
} else {
|
||||
SuperConverter superConverter = findRegisteredSuperConverter(sourceType, targetType);
|
||||
SuperConverter superConverter = findRegisteredSuperConverter(sourceClass, targetType.getType());
|
||||
if (superConverter != null) {
|
||||
return new StaticSuperConversionExecutor(sourceType, targetType, superConverter);
|
||||
}
|
||||
@@ -241,11 +231,6 @@ public class GenericConversionService implements ConversionService {
|
||||
}
|
||||
}
|
||||
|
||||
public ConversionExecutor getConversionExecutor(String converterId, Class sourceType,
|
||||
TypeDescriptor targetType) throws ConversionExecutorNotFoundException {
|
||||
throw new UnsupportedOperationException("Not yet implemented");
|
||||
}
|
||||
|
||||
public Class getType(String name) throws IllegalArgumentException {
|
||||
Class clazz = (Class) aliasMap.get(name);
|
||||
if (clazz != null) {
|
||||
@@ -305,34 +290,32 @@ public class GenericConversionService implements ConversionService {
|
||||
+ "] on Converter [" + converterClass.getName() + "]");
|
||||
}
|
||||
|
||||
private Map getSourceMap(Class sourceClass) {
|
||||
Map sourceMap = (Map) sourceClassConverters.get(sourceClass);
|
||||
private Map getSourceMap(Class sourceType) {
|
||||
Map sourceMap = (Map) sourceTypeConverters.get(sourceType);
|
||||
if (sourceMap == null) {
|
||||
sourceMap = new HashMap();
|
||||
sourceClassConverters.put(sourceClass, sourceMap);
|
||||
sourceTypeConverters.put(sourceType, sourceMap);
|
||||
}
|
||||
return sourceMap;
|
||||
}
|
||||
|
||||
private Map getSourceSuperConverterMap(Class sourceClass) {
|
||||
Map sourceMap = (Map) sourceClassSuperConverters.get(sourceClass);
|
||||
private Map getSourceSuperConverterMap(Class sourceType) {
|
||||
Map sourceMap = (Map) sourceTypeSuperConverters.get(sourceType);
|
||||
if (sourceMap == null) {
|
||||
sourceMap = new HashMap();
|
||||
sourceClassSuperConverters.put(sourceClass, sourceMap);
|
||||
sourceTypeSuperConverters.put(sourceType, sourceMap);
|
||||
}
|
||||
return sourceMap;
|
||||
}
|
||||
|
||||
private Converter findRegisteredConverter(TypeDescriptor sourceType, TypeDescriptor targetType) {
|
||||
Class<?> sourceClass = sourceType.getWrapperTypeIfPrimitive();
|
||||
Class<?> targetClass = targetType.getWrapperTypeIfPrimitive();
|
||||
if (sourceClass.isInterface()) {
|
||||
private Converter findRegisteredConverter(Class<?> sourceType, Class<?> targetType) {
|
||||
if (sourceType.isInterface()) {
|
||||
LinkedList classQueue = new LinkedList();
|
||||
classQueue.addFirst(sourceClass);
|
||||
classQueue.addFirst(sourceType);
|
||||
while (!classQueue.isEmpty()) {
|
||||
Class currentClass = (Class) classQueue.removeLast();
|
||||
Map converters = getConvertersForSource(currentClass);
|
||||
Converter converter = getConverter(converters, targetClass);
|
||||
Converter converter = getConverter(converters, targetType);
|
||||
if (converter != null) {
|
||||
return converter;
|
||||
}
|
||||
@@ -342,14 +325,14 @@ public class GenericConversionService implements ConversionService {
|
||||
}
|
||||
}
|
||||
Map objectConverters = getConvertersForSource(Object.class);
|
||||
return getConverter(objectConverters, targetClass);
|
||||
return getConverter(objectConverters, targetType);
|
||||
} else {
|
||||
LinkedList classQueue = new LinkedList();
|
||||
classQueue.addFirst(sourceClass);
|
||||
classQueue.addFirst(sourceType);
|
||||
while (!classQueue.isEmpty()) {
|
||||
Class currentClass = (Class) classQueue.removeLast();
|
||||
Map converters = getConvertersForSource(currentClass);
|
||||
Converter converter = getConverter(converters, targetClass);
|
||||
Converter converter = getConverter(converters, targetType);
|
||||
if (converter != null) {
|
||||
return converter;
|
||||
}
|
||||
@@ -365,25 +348,23 @@ public class GenericConversionService implements ConversionService {
|
||||
}
|
||||
}
|
||||
|
||||
private Map getConvertersForSource(Class sourceClass) {
|
||||
Map converters = (Map) sourceClassConverters.get(sourceClass);
|
||||
private Map getConvertersForSource(Class sourceType) {
|
||||
Map converters = (Map) sourceTypeConverters.get(sourceType);
|
||||
return converters != null ? converters : Collections.emptyMap();
|
||||
}
|
||||
|
||||
private Converter getConverter(Map converters, Class targetClass) {
|
||||
return (Converter) converters.get(targetClass);
|
||||
private Converter getConverter(Map converters, Class targetType) {
|
||||
return (Converter) converters.get(targetType);
|
||||
}
|
||||
|
||||
private SuperConverter findRegisteredSuperConverter(TypeDescriptor sourceType, TypeDescriptor targetType) {
|
||||
Class<?> sourceClass = sourceType.getWrapperTypeIfPrimitive();
|
||||
Class<?> targetClass = targetType.getWrapperTypeIfPrimitive();
|
||||
if (sourceClass.isInterface()) {
|
||||
private SuperConverter findRegisteredSuperConverter(Class<?> sourceType, Class<?> targetType) {
|
||||
if (sourceType.isInterface()) {
|
||||
LinkedList classQueue = new LinkedList();
|
||||
classQueue.addFirst(sourceClass);
|
||||
classQueue.addFirst(sourceType);
|
||||
while (!classQueue.isEmpty()) {
|
||||
Class currentClass = (Class) classQueue.removeLast();
|
||||
Map converters = getSuperConvertersForSource(currentClass);
|
||||
SuperConverter converter = findSuperConverter(converters, targetClass);
|
||||
SuperConverter converter = findSuperConverter(converters, targetType);
|
||||
if (converter != null) {
|
||||
return converter;
|
||||
}
|
||||
@@ -393,14 +374,14 @@ public class GenericConversionService implements ConversionService {
|
||||
}
|
||||
}
|
||||
Map objectConverters = getSuperConvertersForSource(Object.class);
|
||||
return findSuperConverter(objectConverters, targetClass);
|
||||
return findSuperConverter(objectConverters, targetType);
|
||||
} else {
|
||||
LinkedList classQueue = new LinkedList();
|
||||
classQueue.addFirst(sourceClass);
|
||||
classQueue.addFirst(sourceType);
|
||||
while (!classQueue.isEmpty()) {
|
||||
Class currentClass = (Class) classQueue.removeLast();
|
||||
Map converters = getSuperConvertersForSource(currentClass);
|
||||
SuperConverter converter = findSuperConverter(converters, targetClass);
|
||||
SuperConverter converter = findSuperConverter(converters, targetType);
|
||||
if (converter != null) {
|
||||
return converter;
|
||||
}
|
||||
@@ -416,18 +397,18 @@ public class GenericConversionService implements ConversionService {
|
||||
}
|
||||
}
|
||||
|
||||
private Map getSuperConvertersForSource(Class sourceClass) {
|
||||
Map converters = (Map) sourceClassSuperConverters.get(sourceClass);
|
||||
private Map getSuperConvertersForSource(Class sourceType) {
|
||||
Map converters = (Map) sourceTypeSuperConverters.get(sourceType);
|
||||
return converters != null ? converters : Collections.emptyMap();
|
||||
}
|
||||
|
||||
private SuperConverter findSuperConverter(Map converters, Class targetClass) {
|
||||
private SuperConverter findSuperConverter(Map converters, Class targetType) {
|
||||
if (converters.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
if (targetClass.isInterface()) {
|
||||
if (targetType.isInterface()) {
|
||||
LinkedList classQueue = new LinkedList();
|
||||
classQueue.addFirst(targetClass);
|
||||
classQueue.addFirst(targetType);
|
||||
while (!classQueue.isEmpty()) {
|
||||
Class currentClass = (Class) classQueue.removeLast();
|
||||
SuperConverter converter = (SuperConverter) converters.get(currentClass);
|
||||
@@ -442,7 +423,7 @@ public class GenericConversionService implements ConversionService {
|
||||
return (SuperConverter) converters.get(Object.class);
|
||||
} else {
|
||||
LinkedList classQueue = new LinkedList();
|
||||
classQueue.addFirst(targetClass);
|
||||
classQueue.addFirst(targetType);
|
||||
while (!classQueue.isEmpty()) {
|
||||
Class currentClass = (Class) classQueue.removeLast();
|
||||
SuperConverter converter = (SuperConverter) converters.get(currentClass);
|
||||
@@ -461,9 +442,4 @@ public class GenericConversionService implements ConversionService {
|
||||
}
|
||||
}
|
||||
|
||||
public ConversionExecutor getElementConverter(Class<?> sourceElementType, Class<?> targetElementType) {
|
||||
return getConversionExecutor(sourceElementType, TypeDescriptor
|
||||
.valueOf(targetElementType));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user