Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert String to Enum using Apache BeanUtils

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);

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)

If I register EnumConverter in the following way:

ConvertUtils.register(new EnumConverter(), ConcreteEnumClass.class);

Everything woks perfectly.

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?

like image 421
Ivan Mushketyk Avatar asked Feb 20 '14 10:02

Ivan Mushketyk


People also ask

Can you convert string to Enum?

IsDefined() method to check if a given string name or integer value is defined in a specified enumeration. Thus, the conversion of String to Enum can be implemented using the Enum. Parse ( ) and Enum.

What is Enumutils in Java?

Creates a bit vector representation of the given subset of an Enum using as many long s as needed. This generates a value that is usable by processBitVectors(java.

What is the method to convert an Enum type to a string type?

There are two ways to convert an Enum to String in Java, first by using the name() method of Enum which is an implicit method and available to all Enum, and second by using toString() method.


1 Answers

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);
        }
    }
});
like image 189
Matthew Avatar answered Sep 18 '22 23:09

Matthew