SPR-8718 Prevent ClassCastException when the target of Converter<?,?> is a super-class of the actual target.

This commit is contained in:
Rossen Stoyanchev
2011-09-26 12:30:38 +00:00
parent f6483cad3c
commit 1d7a6c53da
2 changed files with 29 additions and 10 deletions

View File

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