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,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;
}

View File

@@ -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);