问题描述
我为 Apache BeanUtils 库实现了一个转换器,用于将 String 转换为枚举常量:
I've implemented a converter for Apache BeanUtils library for converting String to an enum constant:
class EnumConverter implements Converter {
@Override
public <T> T convert(Class<T> tClass, Object o) {
String enumValName = (String) o;
Enum[] enumConstants = (Enum[]) tClass.getEnumConstants();
for (Enum enumConstant : enumConstants) {
if (enumConstant.name().equals(enumValName)) {
return (T) enumConstant;
}
}
throw new ConversionException(String.format("Failed to convert %s value to %s class", enumValName, tClass.toString()));
}
}
我以如下方式使用它:
// Register my converter
ConvertUtils.register(new EnumConverter(), Enum.class);
Map<String, String> propMap = new HashMap<String, String>();
// fill property map
BeanUtils.populate(myBean, propMap);
不幸的是,除了 ConcreteEnumClass 枚举之外,myBean 实例中有一个 setter,而不是 java.lang.Enum,这就是为什么我在 BeanUtils.populate 方法调用期间收到以下异常:
Unfortunatelly a setter in myBean instance except ConcreteEnumClass enum, instead of java.lang.Enum, that is why I receive the following exception during BeanUtils.populate method call:
org.apache.commons.beanutils.ConversionException: Default conversion to ConcreteEnumClass failed.
at org.apache.commons.beanutils.converters.AbstractConverter.handleMissing(AbstractConverter.java:314)
at org.apache.commons.beanutils.converters.AbstractConverter.handleError(AbstractConverter.java:269)
at org.apache.commons.beanutils.converters.AbstractConverter.convert(AbstractConverter.java:177)
at org.apache.commons.beanutils.converters.ConverterFacade.convert(ConverterFacade.java:61)
at org.apache.commons.beanutils.ConvertUtilsBean.convert(ConvertUtilsBean.java:491)
at org.apache.commons.beanutils.BeanUtilsBean.setProperty(BeanUtilsBean.java:1000)
at org.apache.commons.beanutils.BeanUtilsBean.populate(BeanUtilsBean.java:821)
at org.apache.commons.beanutils.BeanUtils.populate(BeanUtils.java:431)
如果我通过以下方式注册 EnumConverter:
If I register EnumConverter in the following way:
ConvertUtils.register(new EnumConverter(), ConcreteEnumClass.class);
一切正常.
因为我想在一般情况下使用我的 EnumConverter,所以我更喜欢使用它来将 String 转换为任何枚举类.
Since I would like to use my EnumConverter in general case, I would prefer it be used to convert String to any enum class.
有可能吗?我该怎么做?
Is it possible? How should I do it?
推荐答案
从当前的 BeanUtils v1.9.2 开始,我不相信在使用静态单例 BeanUtils
时有任何方法可以做到这一点和 ConvertUtils
类.
As of the current BeanUtils v1.9.2 I don't believe there is any way to do this when using the static singleton BeanUtils
and ConvertUtils
classes.
您可以创建一个 BeanUtilsBean
的实例,传递一个自定义的 ConvertUtilsBean
实例,该实例对 Enum 目标进行特殊处理.
You could create an instance of BeanUtilsBean
, passing a custom ConvertUtilsBean
instance that has special handling for Enum targets.
此处显示了一个示例(不是我的示例,归功于其作者jeremychone"):http://www.bitsandpix.com/entry/java-beanutils-enum-support-generic-enum-converter/
An example is shown here (not my example, credit to its author "jeremychone"):http://www.bitsandpix.com/entry/java-beanutils-enum-support-generic-enum-converter/
Jeremy 的简单实现如下:
Jeremy's simple implementation is as follows:
BeanUtilsBean beanUtilsBean = new BeanUtilsBean(new ConvertUtilsBean() {
@Override
public Object convert(String value, Class clazz) {
if (clazz.isEnum()) {
return Enum.valueOf(clazz, value);
} else {
return super.convert(value, clazz);
}
}
});
这篇关于使用 Apache BeanUtils 将字符串转换为枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!