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 7a008ec075..8b0a9d8ac6 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 @@ -428,6 +428,10 @@ public class GenericConversionService implements ConversionService, ConverterReg 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); @@ -498,6 +502,12 @@ public class GenericConversionService implements ConversionService, ConverterReg } } } + 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 cb06f31350..e98ac7b3e0 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,8 @@ package org.springframework.core.convert.support; +import java.awt.Color; +import java.awt.SystemColor; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; @@ -138,6 +140,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).getClass(); + } + + 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));