Browse Source

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

git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@3631 50f2f4bb-b051-0410-bef5-90022cba6387
pull/1/head
Juergen Hoeller 16 years ago
parent
commit
4c8d2baa17
  1. 3
      org.springframework.core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java
  2. 13
      org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToCollectionConverter.java

3
org.springframework.core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java

@ -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");
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;
}

13
org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToCollectionConverter.java

@ -1,5 +1,5 @@ @@ -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; @@ -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 { @@ -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;
}

Loading…
Cancel
Save