From 00a726b09890726d12b13e7b300825b4aa8064ea Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Thu, 6 Oct 2011 14:17:12 +0000 Subject: [PATCH] SPR-8718 Prevent Converter from converting target sub-type. --- .../convert/support/GenericConversionService.java | 12 +++++++++++- .../support/GenericConversionServiceTests.java | 1 - 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java index 8c29687aef..6c4ed99e78 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java @@ -500,13 +500,17 @@ public class GenericConversionService implements ConfigurableConversionService { return Collections.singleton(this.typeInfo); } + public boolean matchesTargetType(TypeDescriptor targetType) { + return this.typeInfo.getTargetType().equals(targetType.getObjectType()); + } + public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { if (source == null) { return convertNullSource(sourceType, targetType); } return this.converter.convert(source); } - + public String toString() { return this.typeInfo.getSourceType().getName() + " -> " + this.typeInfo.getTargetType().getName() + " : " + this.converter.toString(); @@ -571,6 +575,12 @@ public class GenericConversionService implements ConfigurableConversionService { } } } + if (this.defaultConverter instanceof ConverterAdapter) { + ConverterAdapter adapter = (ConverterAdapter) this.defaultConverter; + if (!adapter.matchesTargetType(targetType)) { + return null; + } + } return this.defaultConverter; } diff --git a/org.springframework.core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java b/org.springframework.core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java index 7476e4c07c..454c044d22 100644 --- a/org.springframework.core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java +++ b/org.springframework.core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java @@ -210,7 +210,6 @@ public class GenericConversionServiceTests { // SPR-8718 @Test(expected=ConverterNotFoundException.class) - @Ignore("TODO") public void convertSuperTarget() { conversionService.addConverter(new ColorConverter()); conversionService.convert("#000000", SystemColor.class);