INT-1441 reverted back to calling canConvert(), but added check to see if source is empty Collection and target is a Collection and simply returning back what was passed for conversion since empty Collection could be cast to any other typed Collection, added test

This commit is contained in:
Oleg Zhurakousky
2010-10-13 08:13:02 -04:00
parent dbf76ee73a
commit c7fab44e0c
2 changed files with 39 additions and 6 deletions

View File

@@ -16,6 +16,7 @@
package org.springframework.integration.util;
import java.beans.PropertyEditor;
import java.util.Collection;
import org.springframework.beans.BeansException;
import org.springframework.beans.SimpleTypeConverter;
@@ -27,6 +28,7 @@ import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.support.ConversionServiceFactory;
import org.springframework.expression.TypeConverter;
import org.springframework.util.CollectionUtils;
/**
*
* @author Dave Syer
@@ -95,13 +97,15 @@ public class BeanFactoryTypeConverter implements TypeConverter, BeanFactoryAware
if (targetType.getType() == Void.class || targetType.getType() == Void.TYPE) {
return null;
}
try {
return conversionService.convert(value, sourceType, targetType);
} catch (ConversionFailedException e) {
throw e;
} catch (Exception ex){
// ignore because we have a fallback strategy, see SPR-7548 for more details
if (value instanceof Collection<?>
&& CollectionUtils.isEmpty((Collection<?>) value)
&& Collection.class.isAssignableFrom(targetType.getObjectType())){
return value;
}
if (conversionService.canConvert(sourceType, targetType)) {
return conversionService.convert(value, sourceType, targetType);
}
if (!String.class.isAssignableFrom(sourceType.getType())) {
PropertyEditor editor = delegate.findCustomEditor(sourceType.getType(), null);
if (editor != null){ // INT-1441

View File

@@ -0,0 +1,29 @@
/**
*
*/
package org.springframework.integration.util;
import static junit.framework.Assert.assertEquals;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import org.springframework.core.convert.TypeDescriptor;
/**
* @author Oleg Zhurakousky
*
*/
public class BeanFactoryTypeConverterTests {
@Test
public void testEmptyCollectionConversion(){
BeanFactoryTypeConverter typeConverter = new BeanFactoryTypeConverter();
List<String> sourceObject = new ArrayList<String>();
// source type doesn't even matter
ArrayList<BeanFactoryTypeConverterTests> convertedCollection =
(ArrayList<BeanFactoryTypeConverterTests>) typeConverter.convertValue(sourceObject, null, TypeDescriptor.forObject(new ArrayList<BeanFactoryTypeConverterTests>()));
assertEquals(sourceObject, convertedCollection);
}
}