提交 0a17e417 编写于 作者: J Juergen Hoeller

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

上级 055c343c
...@@ -170,9 +170,10 @@ public class GenericConversionService implements ConversionService, ConverterReg ...@@ -170,9 +170,10 @@ public class GenericConversionService implements ConversionService, ConverterReg
logger.debug("Converted to null"); logger.debug("Converted to null");
return null; return null;
} }
Assert.isTrue(source == null || sourceType.getObjectType().isInstance(source));
GenericConverter converter = getConverter(sourceType, targetType); GenericConverter converter = getConverter(sourceType, targetType);
if (converter == null) { 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"); logger.debug("No converter found - returning assignable source object as-is");
return source; return source;
} }
......
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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; ...@@ -30,6 +30,7 @@ import org.springframework.core.convert.converter.ConditionalGenericConverter;
* Will convert the Object to the target Collection's parameterized type if necessary. * Will convert the Object to the target Collection's parameterized type if necessary.
* *
* @author Keith Donald * @author Keith Donald
* @author Juergen Hoeller
* @since 3.0 * @since 3.0
*/ */
final class ObjectToCollectionConverter implements ConditionalGenericConverter { final class ObjectToCollectionConverter implements ConditionalGenericConverter {
...@@ -54,8 +55,14 @@ final class ObjectToCollectionConverter implements ConditionalGenericConverter { ...@@ -54,8 +55,14 @@ final class ObjectToCollectionConverter implements ConditionalGenericConverter {
return null; return null;
} }
Collection target = CollectionFactory.createCollection(targetType.getType(), 1); Collection target = CollectionFactory.createCollection(targetType.getType(), 1);
Object targetElement = this.conversionService.convert(source, sourceType, targetType.getElementTypeDescriptor(source)); TypeDescriptor elementType = targetType.getElementTypeDescriptor(source);
target.add(targetElement); // Avoid potential recursion...
if (!Collection.class.isAssignableFrom(elementType.getType())) {
target.add(this.conversionService.convert(source, sourceType, elementType));
}
else {
target.add(source);
}
return target; return target;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册