Support conversion from Enum Interface
EnumToStringConverter in now conditional and only matches Enums that do not implement interfaces that can be converted. Issue: SPR-9692
This commit is contained in:
committed by
Chris Beams
parent
138957b148
commit
f82c6ed7cc
@@ -59,6 +59,7 @@ public class DefaultConversionService extends GenericConversionService {
|
||||
// internal helpers
|
||||
|
||||
private static void addScalarConverters(ConverterRegistry converterRegistry) {
|
||||
ConversionService conversionService = (ConversionService) converterRegistry;
|
||||
converterRegistry.addConverter(new StringToBooleanConverter());
|
||||
converterRegistry.addConverter(Boolean.class, String.class, new ObjectToStringConverter());
|
||||
|
||||
@@ -74,7 +75,7 @@ public class DefaultConversionService extends GenericConversionService {
|
||||
converterRegistry.addConverterFactory(new CharacterToNumberFactory());
|
||||
|
||||
converterRegistry.addConverterFactory(new StringToEnumConverterFactory());
|
||||
converterRegistry.addConverter(Enum.class, String.class, new EnumToStringConverter());
|
||||
converterRegistry.addConverter(Enum.class, String.class, new EnumToStringConverter(conversionService));
|
||||
|
||||
converterRegistry.addConverter(new StringToLocaleConverter());
|
||||
converterRegistry.addConverter(Locale.class, String.class, new ObjectToStringConverter());
|
||||
|
||||
@@ -16,14 +16,37 @@
|
||||
|
||||
package org.springframework.core.convert.support;
|
||||
|
||||
import org.springframework.core.convert.ConversionService;
|
||||
import org.springframework.core.convert.TypeDescriptor;
|
||||
import org.springframework.core.convert.converter.ConditionalConversion;
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
/**
|
||||
* Simply calls {@link Enum#name()} to convert a source Enum to a String.
|
||||
* Calls {@link Enum#name()} to convert a source Enum to a String. This converter will
|
||||
* not match enums with interfaces that can be converterd.
|
||||
* @author Keith Donald
|
||||
* @author Phillip Webb
|
||||
* @since 3.0
|
||||
*/
|
||||
final class EnumToStringConverter implements Converter<Enum<?>, String> {
|
||||
final class EnumToStringConverter implements Converter<Enum<?>, String>, ConditionalConversion {
|
||||
|
||||
private final ConversionService conversionService;
|
||||
|
||||
public EnumToStringConverter(ConversionService conversionService) {
|
||||
this.conversionService = conversionService;
|
||||
}
|
||||
|
||||
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
|
||||
for (Class<?> interfaceType : ClassUtils.getAllInterfacesForClass(sourceType.getType())) {
|
||||
if (conversionService.canConvert(TypeDescriptor.valueOf(interfaceType),
|
||||
targetType)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public String convert(Enum<?> source) {
|
||||
return source.name();
|
||||
|
||||
Reference in New Issue
Block a user