<?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"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Kommentare zu: Subtleties on java.lang.Enum</title>
	<atom:link href="http://www.olivergierke.de/wordpress/2009/01/subtleties-on-javalangenum/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.olivergierke.de/wordpress/2009/01/subtleties-on-javalangenum/</link>
	<description>www.olivergierke.de</description>
	<lastBuildDate>Thu, 25 Feb 2010 19:31:53 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>Von: Erkki Lindpere</title>
		<link>http://www.olivergierke.de/wordpress/2009/01/subtleties-on-javalangenum/comment-page-1/#comment-451</link>
		<dc:creator>Erkki Lindpere</dc:creator>
		<pubDate>Mon, 12 Jan 2009 16:56:28 +0000</pubDate>
		<guid isPermaLink="false">http://www.olivergierke.de/wordpress/?p=209#comment-451</guid>
		<description>Yeah, enums are tricky, especially when used with reflection. For example, here is some code to fill an unknown Enum array given some Strings that correspond to the enum names:

&lt;pre class=&quot;java:nocontrols:nogutter&quot; name=&quot;code&quot;&gt;if (fieldType.isArray() &amp;&amp; 
  fieldType.getComponentType().isEnum()) {
  Enum e = (Enum) fieldType.getComponentType().
    getEnumConstants()[0];
  Vector x = new Vector();
  for (Iterator iter = field.getValues(); iter.hasNext();) {
    String val = iter.next();
    x.add(Enum.valueOf(e.getClass(), val));
  }
  fieldDesc.setValue(bean, x.toArray(Arrays.copyOf(
    fieldType.getComponentType().getEnumConstants(),
      x.size())));
}&lt;/pre&gt;

Maybe there&#039;s a better way to create the array, but i resorted to the trick of: getting the full array of possible values using getEnumConstants(), making a copy of that array with the size of actual values present. Then filling that array with the actual values from a vector.</description>
		<content:encoded><![CDATA[<p>Yeah, enums are tricky, especially when used with reflection. For example, here is some code to fill an unknown Enum array given some Strings that correspond to the enum names:</p>
<pre class="java:nocontrols:nogutter" name="code">if (fieldType.isArray() &#038;&#038;
  fieldType.getComponentType().isEnum()) {
  Enum e = (Enum) fieldType.getComponentType().
    getEnumConstants()[0];
  Vector x = new Vector();
  for (Iterator iter = field.getValues(); iter.hasNext();) {
    String val = iter.next();
    x.add(Enum.valueOf(e.getClass(), val));
  }
  fieldDesc.setValue(bean, x.toArray(Arrays.copyOf(
    fieldType.getComponentType().getEnumConstants(),
      x.size())));
}</pre>
<p>Maybe there&#8217;s a better way to create the array, but i resorted to the trick of: getting the full array of possible values using getEnumConstants(), making a copy of that array with the size of actual values present. Then filling that array with the actual values from a vector.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
