|
|
|
|
@ -17,16 +17,12 @@
@@ -17,16 +17,12 @@
|
|
|
|
|
package org.springframework.core.convert.support; |
|
|
|
|
|
|
|
|
|
import java.lang.reflect.Array; |
|
|
|
|
import java.util.Arrays; |
|
|
|
|
import java.util.Collection; |
|
|
|
|
|
|
|
|
|
import org.springframework.core.CollectionFactory; |
|
|
|
|
import org.springframework.core.convert.TypeDescriptor; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* A generic converter that can convert from one array type to another. |
|
|
|
|
* |
|
|
|
|
* @author Keith Donald |
|
|
|
|
* @author Juergen Hoeller |
|
|
|
|
* @since 3.0 |
|
|
|
|
*/ |
|
|
|
|
class ArrayGenericConverter implements GenericConverter { |
|
|
|
|
|
|
|
|
|
private GenericConversionService conversionService; |
|
|
|
|
@ -36,21 +32,81 @@ class ArrayGenericConverter implements GenericConverter {
@@ -36,21 +32,81 @@ class ArrayGenericConverter implements GenericConverter {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public boolean canConvert(TypeDescriptor sourceType, TypeDescriptor targetType) { |
|
|
|
|
return sourceType.isArray() && targetType.isArray(); |
|
|
|
|
return sourceType.isArray() || targetType.isArray(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { |
|
|
|
|
if (isArrayToArray(sourceType, targetType)) { |
|
|
|
|
return convertArrayToArray(source, sourceType, targetType); |
|
|
|
|
} else if (isArrayToCollection(sourceType, targetType)) { |
|
|
|
|
return convertArrayToCollection(source, sourceType, targetType); |
|
|
|
|
} else if (isCollectionToArray(sourceType, targetType)) { |
|
|
|
|
return convertCollectionToArray(source, sourceType, targetType); |
|
|
|
|
} else if (isArrayToObject(sourceType, targetType)) { |
|
|
|
|
return convertArrayToObject(source, sourceType, targetType); |
|
|
|
|
} else { |
|
|
|
|
return convertObjectToArray(source, sourceType, targetType); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private boolean isArrayToArray(TypeDescriptor sourceType, TypeDescriptor targetType) { |
|
|
|
|
return sourceType.isArray() && targetType.isArray(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private Object convertArrayToArray(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { |
|
|
|
|
if (sourceType.isAssignableTo(targetType)) { |
|
|
|
|
return source; |
|
|
|
|
} |
|
|
|
|
TypeDescriptor targetElementType = targetType.getElementTypeDescriptor(); |
|
|
|
|
TypeDescriptor sourceElementType = sourceType.getElementTypeDescriptor(); |
|
|
|
|
TypeDescriptor targetElementType = targetType.getElementTypeDescriptor(); |
|
|
|
|
Object target = Array.newInstance(targetElementType.getType(), Array.getLength(source)); |
|
|
|
|
GenericConverter converter = conversionService.getConverter(sourceElementType, targetElementType); |
|
|
|
|
for (int i = 0; i < Array.getLength(target); i++) { |
|
|
|
|
Array.set(target, i, converter.convert(Array.get(source, i), sourceElementType, targetElementType)); |
|
|
|
|
} |
|
|
|
|
return target; |
|
|
|
|
return target; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private boolean isArrayToCollection(TypeDescriptor sourceType, TypeDescriptor targetType) { |
|
|
|
|
return sourceType.isArray() && targetType.isCollection(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private Object convertArrayToCollection(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { |
|
|
|
|
int length = Array.getLength(source); |
|
|
|
|
Collection collection = CollectionFactory.createCollection(targetType.getType(), length); |
|
|
|
|
TypeDescriptor targetElementType = targetType.getElementTypeDescriptor(); |
|
|
|
|
if (targetElementType == TypeDescriptor.NULL) { |
|
|
|
|
for (int i = 0; i < length; i++) { |
|
|
|
|
collection.add(Array.get(source, i)); |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
TypeDescriptor sourceElementType = sourceType.getElementTypeDescriptor(); |
|
|
|
|
GenericConverter converter = conversionService.getConverter(sourceElementType, targetElementType); |
|
|
|
|
for (int i = 0; i < length; i++) { |
|
|
|
|
collection.add(converter.convert(Array.get(source, i), sourceElementType, targetElementType)); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return collection; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private boolean isCollectionToArray(TypeDescriptor sourceType, TypeDescriptor targetType) { |
|
|
|
|
return sourceType.isCollection() && targetType.isArray(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private Object convertCollectionToArray(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { |
|
|
|
|
throw new UnsupportedOperationException("Not yet implemented"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private boolean isArrayToObject(TypeDescriptor sourceType, TypeDescriptor targetType) { |
|
|
|
|
return sourceType.isArray(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private Object convertArrayToObject(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { |
|
|
|
|
throw new UnsupportedOperationException("Not yet implemented"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private Object convertObjectToArray(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { |
|
|
|
|
throw new UnsupportedOperationException("Not yet implemented"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |