Convert String to Enum using Apache BeanUtils

允我心安 提交于 2019-11-29 13:40:19

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.

You could create an instance of BeanUtilsBean, passing a custom ConvertUtilsBean instance that has special handling for Enum targets.

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'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);
                  }
           }
        });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!