polish
This commit is contained in:
parent
cb54869726
commit
17f7796d42
|
|
@ -150,7 +150,7 @@ public class GenericConversionService implements ConversionService, ConverterReg
|
||||||
Assert.notNull(sourceType, "The source type to convert to is required");
|
Assert.notNull(sourceType, "The source type to convert to is required");
|
||||||
Assert.notNull(targetType, "The targetType to convert to is required");
|
Assert.notNull(targetType, "The targetType to convert to is required");
|
||||||
if (source == null) {
|
if (source == null) {
|
||||||
return null;
|
return convertNull(sourceType, targetType);
|
||||||
}
|
}
|
||||||
Assert.isTrue(sourceType != TypeDescriptor.NULL,
|
Assert.isTrue(sourceType != TypeDescriptor.NULL,
|
||||||
"The source TypeDescriptor must not be TypeDescriptor.NULL when source != 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);
|
getSourceMap(sourceType).put(targetType, converter);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected Object convertNull(TypeDescriptor sourceType, TypeDescriptor targetType) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
protected GenericConverter getConverter(TypeDescriptor sourceType, TypeDescriptor targetType) {
|
protected GenericConverter getConverter(TypeDescriptor sourceType, TypeDescriptor targetType) {
|
||||||
return findConverterByClassPair(sourceType.getObjectType(), targetType.getObjectType());
|
return findConverterByClassPair(sourceType.getObjectType(), targetType.getObjectType());
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -29,14 +29,14 @@ class ObjectToArrayGenericConverter implements GenericConverter {
|
||||||
|
|
||||||
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
|
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
|
||||||
TypeDescriptor targetElementType = targetType.getElementTypeDescriptor();
|
TypeDescriptor targetElementType = targetType.getElementTypeDescriptor();
|
||||||
Object array = Array.newInstance(targetType.getElementType(), 1);
|
Object target = Array.newInstance(targetElementType.getType(), 1);
|
||||||
if (sourceType.isAssignableTo(targetElementType)) {
|
if (sourceType.isAssignableTo(targetElementType)) {
|
||||||
Array.set(array, 0, source);
|
Array.set(target, 0, source);
|
||||||
} else {
|
} else {
|
||||||
GenericConverter converter = conversionService.getConverter(sourceType, targetElementType);
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -15,6 +15,9 @@
|
||||||
*/
|
*/
|
||||||
package org.springframework.core.convert.support;
|
package org.springframework.core.convert.support;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
|
import org.springframework.core.CollectionFactory;
|
||||||
import org.springframework.core.convert.TypeDescriptor;
|
import org.springframework.core.convert.TypeDescriptor;
|
||||||
|
|
||||||
class ObjectToCollectionGenericConverter implements GenericConverter {
|
class ObjectToCollectionGenericConverter implements GenericConverter {
|
||||||
|
|
@ -26,7 +29,15 @@ class ObjectToCollectionGenericConverter implements GenericConverter {
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue