SPR-8718 Prevent Converter<?,?> from converting target sub-type.

This commit is contained in:
Rossen Stoyanchev
2011-10-06 14:17:12 +00:00
parent dfda4c32d5
commit 00a726b098
2 changed files with 11 additions and 2 deletions

View File

@@ -500,6 +500,10 @@ public class GenericConversionService implements ConfigurableConversionService {
return Collections.singleton(this.typeInfo); 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) { public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
if (source == null) { if (source == null) {
return convertNullSource(sourceType, targetType); return convertNullSource(sourceType, targetType);
@@ -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; return this.defaultConverter;
} }

View File

@@ -210,7 +210,6 @@ public class GenericConversionServiceTests {
// SPR-8718 // SPR-8718
@Test(expected=ConverterNotFoundException.class) @Test(expected=ConverterNotFoundException.class)
@Ignore("TODO")
public void convertSuperTarget() { public void convertSuperTarget() {
conversionService.addConverter(new ColorConverter()); conversionService.addConverter(new ColorConverter());
conversionService.convert("#000000", SystemColor.class); conversionService.convert("#000000", SystemColor.class);