catch invalid arguments early; avoid stack overflow in object-to-collection case (SPR-7488)

This commit is contained in:
Juergen Hoeller
2010-09-01 22:02:07 +00:00
parent 055c343ce0
commit 0a17e41755
2 changed files with 12 additions and 4 deletions

View File

@@ -170,9 +170,10 @@ public class GenericConversionService implements ConversionService, ConverterReg
logger.debug("Converted to null");
return null;
}
Assert.isTrue(source == null || sourceType.getObjectType().isInstance(source));
GenericConverter converter = getConverter(sourceType, targetType);
if (converter == null) {
if (source == null || targetType.getType().isInstance(source)) {
if (source == null || targetType.getObjectType().isInstance(source)) {
logger.debug("No converter found - returning assignable source object as-is");
return source;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2009 the original author or authors.
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -30,6 +30,7 @@ import org.springframework.core.convert.converter.ConditionalGenericConverter;
* Will convert the Object to the target Collection's parameterized type if necessary.
*
* @author Keith Donald
* @author Juergen Hoeller
* @since 3.0
*/
final class ObjectToCollectionConverter implements ConditionalGenericConverter {
@@ -54,8 +55,14 @@ final class ObjectToCollectionConverter implements ConditionalGenericConverter {
return null;
}
Collection target = CollectionFactory.createCollection(targetType.getType(), 1);
Object targetElement = this.conversionService.convert(source, sourceType, targetType.getElementTypeDescriptor(source));
target.add(targetElement);
TypeDescriptor elementType = targetType.getElementTypeDescriptor(source);
// Avoid potential recursion...
if (!Collection.class.isAssignableFrom(elementType.getType())) {
target.add(this.conversionService.convert(source, sourceType, elementType));
}
else {
target.add(source);
}
return target;
}