This commit is contained in:
Keith Donald 2009-09-21 05:39:16 +00:00
parent cb54869726
commit 17f7796d42
3 changed files with 22 additions and 7 deletions

View File

@ -150,7 +150,7 @@ public class GenericConversionService implements ConversionService, ConverterReg
Assert.notNull(sourceType, "The source type to convert to is required");
Assert.notNull(targetType, "The targetType to convert to is required");
if (source == null) {
return null;
return convertNull(sourceType, targetType);
}
Assert.isTrue(sourceType != TypeDescriptor.NULL,
"The source TypeDescriptor must not be TypeDescriptor.NULL when source != null");
@ -197,6 +197,10 @@ public class GenericConversionService implements ConversionService, ConverterReg
getSourceMap(sourceType).put(targetType, converter);
}
protected Object convertNull(TypeDescriptor sourceType, TypeDescriptor targetType) {
return null;
}
protected GenericConverter getConverter(TypeDescriptor sourceType, TypeDescriptor targetType) {
return findConverterByClassPair(sourceType.getObjectType(), targetType.getObjectType());
}

View File

@ -29,14 +29,14 @@ class ObjectToArrayGenericConverter implements GenericConverter {
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
TypeDescriptor targetElementType = targetType.getElementTypeDescriptor();
Object array = Array.newInstance(targetType.getElementType(), 1);
Object target = Array.newInstance(targetElementType.getType(), 1);
if (sourceType.isAssignableTo(targetElementType)) {
Array.set(array, 0, source);
Array.set(target, 0, source);
} else {
GenericConverter converter = conversionService.getConverter(sourceType, targetElementType);
Array.set(array, 0, converter.convert(source, sourceType, targetElementType));
Array.set(target, 0, converter.convert(source, sourceType, targetElementType));
}
return array;
return target;
}
}

View File

@ -15,6 +15,9 @@
*/
package org.springframework.core.convert.support;
import java.util.Collection;
import org.springframework.core.CollectionFactory;
import org.springframework.core.convert.TypeDescriptor;
class ObjectToCollectionGenericConverter implements GenericConverter {
@ -26,7 +29,15 @@ class ObjectToCollectionGenericConverter implements GenericConverter {
}
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
throw new UnsupportedOperationException("Not yet implemented");
Collection target = CollectionFactory.createCollection(targetType.getType(), 1);
TypeDescriptor targetElementType = targetType.getElementTypeDescriptor();
if (targetElementType == TypeDescriptor.NULL || sourceType.isAssignableTo(targetElementType)) {
target.add(source);
} else {
GenericConverter converter = conversionService.getConverter(sourceType, targetElementType);
target.add(converter.convert(source, sourceType, targetElementType));
}
return target;
}
}