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 0aeab228796..b3be06e5f78 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 @@ -485,7 +485,7 @@ public class GenericConversionService implements ConfigurableConversionService { } @SuppressWarnings("unchecked") - private final class ConverterAdapter implements GenericConverter { + private final class ConverterAdapter implements ConditionalGenericConverter { private final ConvertiblePair typeInfo; @@ -500,6 +500,11 @@ public class GenericConversionService implements ConfigurableConversionService { return Collections.singleton(this.typeInfo); } + public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) { + return (typeInfo.getTargetType().equals(targetType.getObjectType()) && + typeInfo.getSourceType().isAssignableFrom(sourceType.getObjectType())); + } + public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { if (source == null) { return convertNullSource(sourceType, targetType); @@ -511,6 +516,7 @@ public class GenericConversionService implements ConfigurableConversionService { return this.typeInfo.getSourceType().getName() + " -> " + this.typeInfo.getTargetType().getName() + " : " + this.converter.toString(); } + } 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 eecfa3f0101..ded3c79b1b8 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 @@ -16,6 +16,16 @@ package org.springframework.core.convert.support; +import static junit.framework.Assert.assertTrue; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.fail; + +import java.awt.Color; +import java.awt.SystemColor; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -28,7 +38,6 @@ import java.util.Map; import java.util.Set; import org.junit.Test; - import org.springframework.core.convert.ConversionFailedException; import org.springframework.core.convert.ConverterNotFoundException; import org.springframework.core.convert.TypeDescriptor; @@ -39,14 +48,6 @@ import org.springframework.core.io.Resource; import org.springframework.util.StopWatch; import org.springframework.util.StringUtils; -import static junit.framework.Assert.assertTrue; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.fail; - /** * @author Keith Donald * @author Juergen Hoeller @@ -205,6 +206,18 @@ public class GenericConversionServiceTests { assertEquals(new Integer(3), result); } + // SPR-8718 + + @Test(expected=ConverterNotFoundException.class) + public void convertSuperTarget() { + conversionService.addConverter(new ColorConverter()); + conversionService.convert("#000000", SystemColor.class); + } + + public class ColorConverter implements Converter { + public Color convert(String source) { if (!source.startsWith("#")) source = "#" + source; return Color.decode(source); } + } + @Test public void convertObjectToPrimitive() { assertFalse(conversionService.canConvert(String.class, boolean.class));