Browse Source

array to object

git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@1943 50f2f4bb-b051-0410-bef5-90022cba6387
pull/1/head
Keith Donald 17 years ago
parent
commit
2ae21e90a1
  1. 10
      org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionGenericConverter.java
  2. 19
      org.springframework.core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java

10
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) { 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) { private boolean isCollectionToObject(TypeDescriptor sourceType, TypeDescriptor targetType) {

19
org.springframework.core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java

@ -240,13 +240,18 @@ public class GenericConversionServiceTests {
} }
@Test @Test
@Ignore public void convertObjectToArray() {
public void convertStringToArray() { String[] result = converter.convert("test", String[].class);
String[] result = (String[]) converter.convert("1,2,3", String[].class); assertEquals(1, result.length);
assertEquals(3, result.length); assertEquals("test", result[0]);
assertEquals("1", result[0]); }
assertEquals("2", result[1]);
assertEquals("3", result[2]); @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 @Test

Loading…
Cancel
Save