SPR-8718 Prevent ClassCastException when the target of Converter<?,?> is a super-class of the actual target.
This commit is contained in:
parent
f6483cad3c
commit
1d7a6c53da
|
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -16,6 +16,16 @@
|
|||
|
||||
package org.springframework.core.convert.support;
|
||||
|
||||
import static junit.framework.Assert.assertTrue;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.SystemColor;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
|
|
@ -28,7 +38,6 @@ import java.util.Map;
|
|||
import java.util.Set;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.core.convert.ConversionFailedException;
|
||||
import org.springframework.core.convert.ConverterNotFoundException;
|
||||
import org.springframework.core.convert.TypeDescriptor;
|
||||
|
|
@ -39,14 +48,6 @@ import org.springframework.core.io.Resource;
|
|||
import org.springframework.util.StopWatch;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import static junit.framework.Assert.assertTrue;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
/**
|
||||
* @author Keith Donald
|
||||
* @author Juergen Hoeller
|
||||
|
|
@ -205,6 +206,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);
|
||||
}
|
||||
|
||||
public class ColorConverter implements Converter<String, Color> {
|
||||
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));
|
||||
|
|
|
|||
Loading…
Reference in New Issue