From f606eb3012497b43fbdda7ac067544529f4c8e3e Mon Sep 17 00:00:00 2001 From: Keith Donald Date: Mon, 27 Jun 2011 21:36:48 +0000 Subject: [PATCH] SPR-8465 git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@4632 50f2f4bb-b051-0410-bef5-90022cba6387 --- .../support/ArrayToObjectConverter.java | 21 +++++++++++++------ .../support/CollectionToObjectConverter.java | 3 +++ .../support/DefaultConversionTests.java | 21 +++++++++++++++++-- 3 files changed, 37 insertions(+), 8 deletions(-) diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToObjectConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToObjectConverter.java index 842a33605a4..966b0e05d2f 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToObjectConverter.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToObjectConverter.java @@ -16,14 +16,13 @@ package org.springframework.core.convert.support; -import java.util.Arrays; +import java.lang.reflect.Array; import java.util.Collections; import java.util.Set; import org.springframework.core.convert.ConversionService; import org.springframework.core.convert.TypeDescriptor; import org.springframework.core.convert.converter.ConditionalGenericConverter; -import org.springframework.util.ObjectUtils; /** * Converts an Array to an Object by returning the first array element after converting it to the desired targetType. @@ -34,10 +33,10 @@ import org.springframework.util.ObjectUtils; */ final class ArrayToObjectConverter implements ConditionalGenericConverter { - private final CollectionToObjectConverter helperConverter; + private final ConversionService conversionService; public ArrayToObjectConverter(ConversionService conversionService) { - this.helperConverter = new CollectionToObjectConverter(conversionService); + this.conversionService = conversionService; } public Set getConvertibleTypes() { @@ -45,11 +44,21 @@ final class ArrayToObjectConverter implements ConditionalGenericConverter { } public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) { - return this.helperConverter.matches(sourceType, targetType); + return ConversionUtils.canConvertElements(sourceType.getElementTypeDescriptor(), targetType, conversionService); } public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { - return this.helperConverter.convert(Arrays.asList(ObjectUtils.toObjectArray(source)), sourceType, targetType); + if (source == null) { + return null; + } + if (sourceType.isAssignableTo(targetType)) { + return source; + } + if (Array.getLength(source) == 0) { + return null; + } + Object firstElement = Array.get(source, 0); + return this.conversionService.convert(firstElement, sourceType.elementTypeDescriptor(firstElement), targetType); } } diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToObjectConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToObjectConverter.java index 59e5ee4ef55..627d595be71 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToObjectConverter.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToObjectConverter.java @@ -50,6 +50,9 @@ final class CollectionToObjectConverter implements ConditionalGenericConverter { if (source == null) { return null; } + if (sourceType.isAssignableTo(targetType)) { + return source; + } Collection sourceCollection = (Collection) source; if (sourceCollection.size() == 0) { return null; diff --git a/org.springframework.core/src/test/java/org/springframework/core/convert/support/DefaultConversionTests.java b/org.springframework.core/src/test/java/org/springframework/core/convert/support/DefaultConversionTests.java index 45016b5d003..0a26c9a26f6 100644 --- a/org.springframework.core/src/test/java/org/springframework/core/convert/support/DefaultConversionTests.java +++ b/org.springframework.core/src/test/java/org/springframework/core/convert/support/DefaultConversionTests.java @@ -388,7 +388,7 @@ public class DefaultConversionTests { @Test public void convertArrayToObject() { Object[] array = new Object[] { 3L }; - Object result = conversionService.convert(array, Object.class); + Object result = conversionService.convert(array, Long.class); assertEquals(3L, result); } @@ -399,6 +399,13 @@ public class DefaultConversionTests { assertEquals(new Integer(3), result); } + @Test + public void convertArrayToObjectAssignableTargetType() { + Long[] array = new Long[] { 3L }; + Long[] result = (Long[]) conversionService.convert(array, Object.class); + assertEquals(array, result); + } + @Test public void convertObjectToArray() { Object[] result = conversionService.convert(3L, Object[].class); @@ -491,6 +498,14 @@ public class DefaultConversionTests { assertEquals(new Integer(3), result); } + @Test + public void convertCollectionToObjectAssignableTarget() throws Exception { + Collection source = new ArrayList(); + source.add("foo"); + Object result = conversionService.convert(source, new TypeDescriptor(getClass().getField("assignableTarget"))); + assertEquals(source, result); + } + @Test public void convertObjectToCollection() { List result = (List) conversionService.convert(3L, List.class); @@ -670,7 +685,9 @@ public class DefaultConversionTests { public void convertObjectToObjectNoValueOFMethodOrConstructor() { conversionService.convert(new Long(3), SSN.class); } - + + public Object assignableTarget; + private static class SSN { private String value;