GenericConversionService detects enum subclasses as well
Issue: SPR-12181
This commit is contained in:
@@ -568,14 +568,14 @@ public class GenericConversionService implements ConfigurableConversionService {
|
||||
Class<?> candidate = hierarchy.get(i);
|
||||
candidate = (array ? candidate.getComponentType() : ClassUtils.resolvePrimitiveIfNecessary(candidate));
|
||||
Class<?> superclass = candidate.getSuperclass();
|
||||
if (candidate.getSuperclass() != null && superclass != Object.class && superclass != Enum.class) {
|
||||
if (superclass != null && superclass != Object.class && superclass != Enum.class) {
|
||||
addToClassHierarchy(i + 1, candidate.getSuperclass(), array, hierarchy, visited);
|
||||
}
|
||||
addInterfacesToClassHierarchy(candidate, array, hierarchy, visited);
|
||||
i++;
|
||||
}
|
||||
|
||||
if (type.isEnum()) {
|
||||
if (Enum.class.isAssignableFrom(type)) {
|
||||
addToClassHierarchy(hierarchy.size(), Enum.class, array, hierarchy, visited);
|
||||
addToClassHierarchy(hierarchy.size(), Enum.class, false, hierarchy, visited);
|
||||
addInterfacesToClassHierarchy(Enum.class, array, hierarchy, visited);
|
||||
|
||||
@@ -63,6 +63,7 @@ public class GenericConversionServiceTests {
|
||||
|
||||
private GenericConversionService conversionService = new GenericConversionService();
|
||||
|
||||
|
||||
@Test
|
||||
public void canConvert() {
|
||||
assertFalse(conversionService.canConvert(String.class, Integer.class));
|
||||
@@ -750,6 +751,13 @@ public class GenericConversionServiceTests {
|
||||
assertEquals("A", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSubclassOfEnumToString() throws Exception {
|
||||
conversionService.addConverter(new EnumToStringConverter(conversionService));
|
||||
String result = conversionService.convert(EnumWithSubclass.FIRST, String.class);
|
||||
assertEquals("FIRST", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEnumWithInterfaceToStringConversion() {
|
||||
// SPR-9692
|
||||
@@ -865,6 +873,7 @@ public class GenericConversionServiceTests {
|
||||
@ExampleAnnotation
|
||||
public String annotatedString;
|
||||
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public static @interface ExampleAnnotation {
|
||||
}
|
||||
@@ -907,8 +916,7 @@ public class GenericConversionServiceTests {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object convert(Object source, TypeDescriptor sourceType,
|
||||
TypeDescriptor targetType) {
|
||||
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -945,14 +953,19 @@ public class GenericConversionServiceTests {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
interface MyEnumBaseInterface {
|
||||
|
||||
String getBaseCode();
|
||||
}
|
||||
|
||||
|
||||
interface MyEnumInterface extends MyEnumBaseInterface {
|
||||
|
||||
String getCode();
|
||||
}
|
||||
|
||||
|
||||
public static enum MyEnum implements MyEnumInterface {
|
||||
|
||||
A("1"),
|
||||
@@ -977,6 +990,17 @@ public class GenericConversionServiceTests {
|
||||
}
|
||||
|
||||
|
||||
public enum EnumWithSubclass {
|
||||
|
||||
FIRST {
|
||||
@Override
|
||||
public String toString() {
|
||||
return "1st";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class MyStringToRawCollectionConverter implements Converter<String, Collection> {
|
||||
|
||||
@Override
|
||||
@@ -985,6 +1009,7 @@ public class GenericConversionServiceTests {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class MyStringToGenericCollectionConverter implements Converter<String, Collection<?>> {
|
||||
|
||||
@Override
|
||||
@@ -993,6 +1018,7 @@ public class GenericConversionServiceTests {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static class MyEnumInterfaceToStringConverter<T extends MyEnumInterface> implements Converter<T, String> {
|
||||
|
||||
@Override
|
||||
@@ -1001,6 +1027,7 @@ public class GenericConversionServiceTests {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static class StringToMyEnumInterfaceConverterFactory implements ConverterFactory<String, MyEnumInterface> {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@@ -1024,9 +1051,9 @@ public class GenericConversionServiceTests {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
private static class StringToMyEnumBaseInterfaceConverterFactory implements ConverterFactory<String, MyEnumBaseInterface> {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@@ -1050,7 +1077,6 @@ public class GenericConversionServiceTests {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -1062,6 +1088,7 @@ public class GenericConversionServiceTests {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class MyStringToIntegerCollectionConverter implements Converter<String, Collection<Integer>> {
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user