diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayGenericConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayGenericConverter.java index fcac427afbd..32e4edb9ccf 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayGenericConverter.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayGenericConverter.java @@ -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 { } 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"); } } \ No newline at end of file diff --git a/org.springframework.core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java b/org.springframework.core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java index 59685165ddd..d1e91ca48e6 100644 --- a/org.springframework.core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java +++ b/org.springframework.core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java @@ -153,7 +153,6 @@ public class GenericConversionServiceTests { } @Test - @Ignore public void convertArrayToListInterface() { List result = converter.convert(new String[] { "1", "2", "3" }, List.class); assertEquals("1", result.get(0)); @@ -164,7 +163,6 @@ public class GenericConversionServiceTests { public List genericList = new ArrayList(); @Test - @Ignore public void convertArrayToListGenericTypeConversion() throws Exception { converter.addConverterFactory(new StringToNumberConverterFactory()); List result = (List) converter.convert(new String[] { "1", "2", "3" }, TypeDescriptor @@ -175,7 +173,6 @@ public class GenericConversionServiceTests { } @Test - @Ignore public void convertArrayToListImpl() { LinkedList result = converter.convert(new String[] { "1", "2", "3" }, LinkedList.class); assertEquals("1", result.get(0)); @@ -183,14 +180,9 @@ public class GenericConversionServiceTests { assertEquals("3", result.get(2)); } - @Test - @Ignore + @Test(expected=ConversionFailedException.class) public void convertArrayToAbstractList() { - try { - converter.convert(new String[] { "1", "2", "3" }, AbstractList.class); - } catch (IllegalArgumentException e) { - - } + converter.convert(new String[] { "1", "2", "3" }, AbstractList.class); } @Test