使用Commons BeanUtils时,为特定类的所有子类注册Converter的最佳方法是什么? [英] What's the best way to register a Converter for all subclasses of a particular class when using Commons BeanUtils?

查看:347
本文介绍了使用Commons BeanUtils时,为特定类的所有子类注册Converter的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,如果我希望为java.util.Map的所有实例注册一个Converter,是否有一些方法可以做到这一点:

For example, if I wished to register a Converter for all instances of java.util.Map, is there some way of doing this:

new BeanUtilsBean().getConvertUtils().register(new MyConverter(), Map.class);

对于Map的任何实例(例如HashMap),将调用MyConverter#convert()方法的地方吗?

where the MyConverter#convert() method would be called for any instance of a Map (for instance a HashMap)?

此操作的背景是我正在使用BeanUtils从数据库填充各种不同的bean.它们的某些属性是实现特定接口的枚举,并且需要设置自定义例程来设置其值.我希望为有问题的接口的所有实现注册一个转换器类,但是找不到做到这一点的方法,因此最终不得不通过检查Bean和如果它们恰好是此接口的实例,请注册我的转换器类:

The background to this is that I'm using BeanUtils to populate various different beans from a database. Some of their properties are enums that implement a particular interface, and to set their values a custom routine is needed. I'd hoped to register a single converter class for all implementations of the interface in question but couldn't find a way of doing this, so ended up having to do it on the fly by inspecting the class of every property in the beans and registering my converter class if they happened to be instances of this interface:

BeanUtilsBean b = new BeanUtilsBean();
Class< ? > propertyType = pu.getPropertyType(this, setterName);

if (isImplementationOfMyInterface(propertyType)) {
    b.getConvertUtils().register(new MyConverter(), propertyType);
}

b.setProperty(this, setterName, value);

这似乎很讨厌,我确定必须有更好的方法来做到这一点?

This seems rather nasty, and I'm sure there must be a better way of doing this?

推荐答案

您可以覆盖ConvertUtilsBean.以下代码添加了对Enum的支持,但是您可以对Map做同样的事情:

You can override ConvertUtilsBean. The following code adds support for Enum, but you can do the same for Map:

BeanUtilsBean.setInstance(new BeanUtilsBean(new EnumAwareConvertUtilsBean()));

类定义:

public class EnumAwareConvertUtilsBean extends ConvertUtilsBean2 {

    private static final EnumConverter ENUM_CONVERTER = new EnumConverter();

    @Override
    public Converter lookup(Class pClazz) {
        final Converter converter = super.lookup(pClazz);

        if (converter == null && pClazz.isEnum()) {
            return ENUM_CONVERTER;
        } else {
            return converter;
        }
    }

}

public class EnumConverter extends AbstractConverter {

    private static final Logger LOGGER = LoggerFactory.getLogger(EnumConverter.class);

    @Override
    protected String convertToString(final Object pValue) throws Throwable {
        return ((Enum) pValue).name();
    }

    @Override
    protected Object convertToType(final Class pType, final Object pValue)
        throws Throwable
    {
        // NOTE: Convert to String is handled elsewhere

        final Class<? extends Enum> type = pType;
        try {
            return Enum.valueOf(type, pValue.toString());
        } catch (final IllegalArgumentException e) {
            LOGGER.warn("No enum value \""
                + pValue
                + "\" for "
                + type.getName());
        }

        return null;
    }

    @Override
    protected Class getDefaultType() {
        return null;
    }

}

阅读博客文章和 查看全文

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