使用Apache BeanUtils将字符串转换为Enum [英] Convert String to Enum using Apache BeanUtils

查看:654
本文介绍了使用Apache BeanUtils将字符串转换为Enum的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为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()));
    }
}

我通过以下方式使用它:

I use it in the following way:

// Register my converter    
ConvertUtils.register(new EnumConverter(), Enum.class);
Map<String, String> propMap = new HashMap<String, String>();
// fill property map    
BeanUtils.populate(myBean, propMap);

不幸的是,myBean实例中的设置器(除了ConcreteEnumClass枚举),而不是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开始,使用静态单例BeanUtilsConvertUtils类时,我不认为有任何方法可以做到这一点

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的实例,并传递对Enum目标进行特殊处理的自定义ConvertUtilsBean实例.

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将字符串转换为Enum的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆