From 143cb50986678394444c7133d733c29bc137f2bf Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Fri, 2 Dec 2011 11:18:31 +0000 Subject: [PATCH] ConversionService prevents Converter from trying to convert to a subtype of its actual target type (SPR-8718) --- .../convert/support/GenericConversionService.java | 10 ++++++++++ .../support/GenericConversionServiceTests.java | 14 ++++++++++++++ 2 files changed, 24 insertions(+) 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));