Browse Source

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

pull/7/head
Rossen Stoyanchev 13 years ago
parent
commit
1d7a6c53da
  1. 8
      org.springframework.core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java
  2. 31
      org.springframework.core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java

8
org.springframework.core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java

@ -485,7 +485,7 @@ public class GenericConversionService implements ConfigurableConversionService { @@ -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 { @@ -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 { @@ -511,6 +516,7 @@ public class GenericConversionService implements ConfigurableConversionService {
return this.typeInfo.getSourceType().getName() + " -> " + this.typeInfo.getTargetType().getName() +
" : " + this.converter.toString();
}
}

31
org.springframework.core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java

@ -16,6 +16,16 @@ @@ -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; @@ -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; @@ -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 { @@ -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…
Cancel
Save