SPR-8465
This commit is contained in:
parent
1fd8f77989
commit
c9d73e2bde
|
|
@ -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<ConvertiblePair> 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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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<String> source = new ArrayList<String>();
|
||||
source.add("foo");
|
||||
Object result = conversionService.convert(source, new TypeDescriptor(getClass().getField("assignableTarget")));
|
||||
assertEquals(source, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertObjectToCollection() {
|
||||
List<String> result = (List<String>) 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;
|
||||
|
|
|
|||
Loading…
Reference in New Issue