<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>
<channel>
	<title>Comments on: Java: BeanUtils Enum Support - generic Enum converter</title>
	<atom:link href="http://www.bitsandpix.com/entry/java-beanutils-enum-support-generic-enum-converter/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.bitsandpix.com/entry/java-beanutils-enum-support-generic-enum-converter/</link>
	<description>No Buzz, just Code, Design, and Sys-Admin stuff.</description>
	<pubDate>Thu, 09 Feb 2012 05:46:37 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.2</generator>
		<item>
		<title>By: steff</title>
		<link>http://www.bitsandpix.com/entry/java-beanutils-enum-support-generic-enum-converter/#comment-115052</link>
		<dc:creator>steff</dc:creator>
		<pubDate>Wed, 14 Dec 2011 08:28:37 +0000</pubDate>
		<guid isPermaLink="false">http://www.bitsandpix.com/?p=632#comment-115052</guid>
		<description>Another way is to overrider register and lookup methods:

    @Override
    public void register(Converter converter, Class aClass) {
        if (aClass.equals(Enum.class)){
            enumConverter = converter;
        }
        super.register(converter, aClass);


    }

    @Override
    public Converter lookup(Class aClass) {

        if (aClass.isEnum() &#38;&#38; enumConverter != null){
            return enumConverter;            
        }
        return super.lookup(aClass);    
    }

In that scenario you are free to implement and regiseter more complex converters.</description>
		<content:encoded><![CDATA[<p>Another way is to overrider register and lookup methods:</p>
<p>    @Override<br />
    public void register(Converter converter, Class aClass) {<br />
        if (aClass.equals(Enum.class)){<br />
            enumConverter = converter;<br />
        }<br />
        super.register(converter, aClass);</p>
<p>    }</p>
<p>    @Override<br />
    public Converter lookup(Class aClass) {</p>
<p>        if (aClass.isEnum() &amp;&amp; enumConverter != null){<br />
            return enumConverter;<br />
        }<br />
        return super.lookup(aClass);<br />
    }</p>
<p>In that scenario you are free to implement and regiseter more complex converters.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jeremychone</title>
		<link>http://www.bitsandpix.com/entry/java-beanutils-enum-support-generic-enum-converter/#comment-1555</link>
		<dc:creator>jeremychone</dc:creator>
		<pubDate>Sat, 25 Jul 2009 05:03:43 +0000</pubDate>
		<guid isPermaLink="false">http://www.bitsandpix.com/?p=632#comment-1555</guid>
		<description>@greg, tx for your comments. I like your proposal. (I also fixed your layout).</description>
		<content:encoded><![CDATA[<p>@greg, tx for your comments. I like your proposal. (I also fixed your layout).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: greg</title>
		<link>http://www.bitsandpix.com/entry/java-beanutils-enum-support-generic-enum-converter/#comment-1434</link>
		<dc:creator>greg</dc:creator>
		<pubDate>Fri, 17 Jul 2009 20:04:57 +0000</pubDate>
		<guid isPermaLink="false">http://www.bitsandpix.com/?p=632#comment-1434</guid>
		<description>While writing the above, I came up with a possibly slightly more elegant and performant version. It should avoid checking for class.isEnum for each and every conversion. 
&lt;pre&gt;
class EnumAwareConvertUtilsBean extends ConvertUtilsBean {
    private final EnumConverter enumConverter = new EnumConverter();

    public Converter lookup(Class clazz) {
        final Converter converter = super.lookup(clazz);
        // no specific converter for this class, so it's neither a String, (which has a default converter),
        // nor any known object that has a custom converter for it. It might be an enum !
        if (converter == null &#38;&#38; clazz.isEnum()) {
            return enumConverter;
        } else {
            return converter;
        }
    }

    private class EnumConverter implements Converter {
        public Object convert(Class type, Object value) {
            return Enum.valueOf(type, (String) value);
        }
    }
}
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>While writing the above, I came up with a possibly slightly more elegant and performant version. It should avoid checking for class.isEnum for each and every conversion. </p>
<pre>
class EnumAwareConvertUtilsBean extends ConvertUtilsBean {
    private final EnumConverter enumConverter = new EnumConverter();

    public Converter lookup(Class clazz) {
        final Converter converter = super.lookup(clazz);
        // no specific converter for this class, so it's neither a String, (which has a default converter),
        // nor any known object that has a custom converter for it. It might be an enum !
        if (converter == null &amp;&amp; clazz.isEnum()) {
            return enumConverter;
        } else {
            return converter;
        }
    }

    private class EnumConverter implements Converter {
        public Object convert(Class type, Object value) {
            return Enum.valueOf(type, (String) value);
        }
    }
}
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: greg</title>
		<link>http://www.bitsandpix.com/entry/java-beanutils-enum-support-generic-enum-converter/#comment-1433</link>
		<dc:creator>greg</dc:creator>
		<pubDate>Fri, 17 Jul 2009 19:52:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.bitsandpix.com/?p=632#comment-1433</guid>
		<description>Thanks for this ! So far, I was using the static BeanUtils.convert() method, and all I could do was ConvertUtil.register(..., String.class) - overriding the default String converter with a custom converter that would essentially do the same as above if the target was an enum. I hated the all-static nature of this (if I register the converter at some point during the lifetime of my application, the behaviour basically changes globally and randomly) and the ugliness of it (replacing the default String2String converter!?)

An intermediate approach might be to use the non-static methods like here, the default ConvertUtilsBean implementation, but register the default String converter on a "controlled" instance of it.</description>
		<content:encoded><![CDATA[<p>Thanks for this ! So far, I was using the static BeanUtils.convert() method, and all I could do was ConvertUtil.register(&#8230;, String.class) - overriding the default String converter with a custom converter that would essentially do the same as above if the target was an enum. I hated the all-static nature of this (if I register the converter at some point during the lifetime of my application, the behaviour basically changes globally and randomly) and the ugliness of it (replacing the default String2String converter!?)</p>
<p>An intermediate approach might be to use the non-static methods like here, the default ConvertUtilsBean implementation, but register the default String converter on a &#8220;controlled&#8221; instance of it.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

