Skip to content

Commit 9b2da37

Browse files
committed
GenericConversionService detects enum subclasses as well
Issue: SPR-12181
1 parent 553930a commit 9b2da37

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

spring-core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -568,14 +568,14 @@ private List<Class<?>> getClassHierarchy(Class<?> type) {
568568
Class<?> candidate = hierarchy.get(i);
569569
candidate = (array ? candidate.getComponentType() : ClassUtils.resolvePrimitiveIfNecessary(candidate));
570570
Class<?> superclass = candidate.getSuperclass();
571-
if (candidate.getSuperclass() != null && superclass != Object.class && superclass != Enum.class) {
571+
if (superclass != null && superclass != Object.class && superclass != Enum.class) {
572572
addToClassHierarchy(i + 1, candidate.getSuperclass(), array, hierarchy, visited);
573573
}
574574
addInterfacesToClassHierarchy(candidate, array, hierarchy, visited);
575575
i++;
576576
}
577577

578-
if (type.isEnum()) {
578+
if (Enum.class.isAssignableFrom(type)) {
579579
addToClassHierarchy(hierarchy.size(), Enum.class, array, hierarchy, visited);
580580
addToClassHierarchy(hierarchy.size(), Enum.class, false, hierarchy, visited);
581581
addInterfacesToClassHierarchy(Enum.class, array, hierarchy, visited);

spring-core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java

+31-4
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public class GenericConversionServiceTests {
6363

6464
private GenericConversionService conversionService = new GenericConversionService();
6565

66+
6667
@Test
6768
public void canConvert() {
6869
assertFalse(conversionService.canConvert(String.class, Integer.class));
@@ -750,6 +751,13 @@ public void testEnumToStringConversion() {
750751
assertEquals("A", result);
751752
}
752753

754+
@Test
755+
public void testSubclassOfEnumToString() throws Exception {
756+
conversionService.addConverter(new EnumToStringConverter(conversionService));
757+
String result = conversionService.convert(EnumWithSubclass.FIRST, String.class);
758+
assertEquals("FIRST", result);
759+
}
760+
753761
@Test
754762
public void testEnumWithInterfaceToStringConversion() {
755763
// SPR-9692
@@ -865,6 +873,7 @@ public void rawCollectionAsSource() throws Exception {
865873
@ExampleAnnotation
866874
public String annotatedString;
867875

876+
868877
@Retention(RetentionPolicy.RUNTIME)
869878
public static @interface ExampleAnnotation {
870879
}
@@ -907,8 +916,7 @@ public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
907916
}
908917

909918
@Override
910-
public Object convert(Object source, TypeDescriptor sourceType,
911-
TypeDescriptor targetType) {
919+
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
912920
return null;
913921
}
914922

@@ -945,14 +953,19 @@ public int getNestedMatchAttempts() {
945953
}
946954
}
947955

956+
948957
interface MyEnumBaseInterface {
958+
949959
String getBaseCode();
950960
}
951961

962+
952963
interface MyEnumInterface extends MyEnumBaseInterface {
964+
953965
String getCode();
954966
}
955967

968+
956969
public static enum MyEnum implements MyEnumInterface {
957970

958971
A("1"),
@@ -977,6 +990,17 @@ public String getBaseCode() {
977990
}
978991

979992

993+
public enum EnumWithSubclass {
994+
995+
FIRST {
996+
@Override
997+
public String toString() {
998+
return "1st";
999+
}
1000+
}
1001+
}
1002+
1003+
9801004
public static class MyStringToRawCollectionConverter implements Converter<String, Collection> {
9811005

9821006
@Override
@@ -985,6 +1009,7 @@ public Collection convert(String source) {
9851009
}
9861010
}
9871011

1012+
9881013
public static class MyStringToGenericCollectionConverter implements Converter<String, Collection<?>> {
9891014

9901015
@Override
@@ -993,6 +1018,7 @@ public Collection<?> convert(String source) {
9931018
}
9941019
}
9951020

1021+
9961022
private static class MyEnumInterfaceToStringConverter<T extends MyEnumInterface> implements Converter<T, String> {
9971023

9981024
@Override
@@ -1001,6 +1027,7 @@ public String convert(T source) {
10011027
}
10021028
}
10031029

1030+
10041031
private static class StringToMyEnumInterfaceConverterFactory implements ConverterFactory<String, MyEnumInterface> {
10051032

10061033
@SuppressWarnings("unchecked")
@@ -1024,9 +1051,9 @@ public T convert(String source) {
10241051
return null;
10251052
}
10261053
}
1027-
10281054
}
10291055

1056+
10301057
private static class StringToMyEnumBaseInterfaceConverterFactory implements ConverterFactory<String, MyEnumBaseInterface> {
10311058

10321059
@SuppressWarnings("unchecked")
@@ -1050,7 +1077,6 @@ public T convert(String source) {
10501077
return null;
10511078
}
10521079
}
1053-
10541080
}
10551081

10561082

@@ -1062,6 +1088,7 @@ public Collection<String> convert(String source) {
10621088
}
10631089
}
10641090

1091+
10651092
public static class MyStringToIntegerCollectionConverter implements Converter<String, Collection<Integer>> {
10661093

10671094
@Override

0 commit comments

Comments
 (0)