diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionGenericConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionGenericConverter.java index d9290109aa4..a7f88072f77 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionGenericConverter.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionGenericConverter.java @@ -171,7 +171,15 @@ class CollectionGenericConverter implements GenericConverter { } private Object convertObjectToArray(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { - throw new UnsupportedOperationException("Not yet implemented"); + TypeDescriptor targetElementType = targetType.getElementTypeDescriptor(); + Object array = Array.newInstance(targetType.getElementType(), 1); + if (sourceType.isAssignableTo(targetElementType)) { + Array.set(array, 0, source); + } else { + GenericConverter converter = conversionService.getConverter(sourceType, targetElementType); + Array.set(array, 0, converter.convert(source, sourceType, targetElementType)); + } + return array; } private boolean isCollectionToObject(TypeDescriptor sourceType, TypeDescriptor targetType) { 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 a6631ae0c61..b88585e0780 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 @@ -240,13 +240,18 @@ public class GenericConversionServiceTests { } @Test - @Ignore - public void convertStringToArray() { - String[] result = (String[]) converter.convert("1,2,3", String[].class); - assertEquals(3, result.length); - assertEquals("1", result[0]); - assertEquals("2", result[1]); - assertEquals("3", result[2]); + public void convertObjectToArray() { + String[] result = converter.convert("test", String[].class); + assertEquals(1, result.length); + assertEquals("test", result[0]); + } + + @Test + public void convertObjectToArrayWithElementConversion() { + converter.addConverterFactory(new StringToNumberConverterFactory()); + Integer[] result = converter.convert("1", Integer[].class); + assertEquals(1, result.length); + assertEquals(new Integer(1), result[0]); } @Test