> 1.) When I write a simple main class, that invokes the enum classes
> fromValue() method, I receive the following exception:
>
> Exception in thread "main" java.lang.ExceptionInInitializerError
<snip />
> Do I need some initialization when using the typesafe enum classes?
No, you shouldn't be. So this must be a bug. And the workaround
suggested by Philip should work.
> 2.) I was expecting that an anonymous simple types enum classes would be
> generated as inner classes
> of the corresponding complex types interface. However, this doesn't seem
> to be the case. To quote
> the spec: "By default, if the XML component containing a typesafe enum
> class to be generated is
> scoped within a complex type as opposed to a global scope, the typesafe
> enum class should occur
> as a nested class within the Java content interface representing the
> complex type scope." [4.3]
See spec 5.2.3. In particular, it says "An anonymous simple type
definition is never bound to a typesafe enum class by default, but it
can be customized as described in Section 6.10, <typesafeEnum>
Declaration to bind to a typesafe enum class."
> Perhaps I misunderstand the meaning of "the XML component containing a
> typesafe enum class". If
> so, please verify the "LocalSimpleTypesElement" in the attached class
> and fix my error.
Quoting an example from 6.10,
<xs:attribute name="usState">
<xs:simpleType>
<xs:annotation><xs:appinfo>
<jaxb:typesafeEnumClass name="USStateAbbr"/>
</xs:appinfo></xs:annotation>
<xs:restrictionbase="xs:string">
<xs:enumeration value="AK">
<xs:annotation><xs:appinfo>
<jaxb:typesafeEnumMember name="State_AK" />
</xs:appinfo></xs:annotation>
</xs:enumeration>
<xs:enumeration value="AL">
<annotation><appinfo>
<typesafeEnumMember name="State_AL" />
</xs:appinfo></xs:annotation>
</xs:enumeration>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
> 3.) I wondered that the typesafe enum classes to not implement the
> java.io.Serializable interface.
With the <xjc:serializable/> customization, it will. By default, it
doesn't.
> 4.) Perhaps related to 3.), I wondered about the hashCode() and equals()
> methods. I was expecting
> something similar to
>
> public int hashCode() { return value; }
> public boolean equals(Object o) { return o != null && o instanceof
> MyClass && ((MyClass) o).value == value; }
>
> IMO the generated equals method (which is simply invoking
> Object.equals(Object)) fails on
> objects that I receive via serialization. I wasn't able to demonstrate
> that, though, due to
> the problems in 1.)
Again, I thought this should work in the presence of
<xjc:serializable /> Assuming that it doesn't, overriding
equals/hashCode as you suggested is a wrong fix to the problem. The idea
of the type-safe enum is the ability to compare objects by ==.
regards,
--
Kohsuke Kawaguchi
Sun Microsystems kohsuke.kawaguchi_at_sun.com
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
For additional commands, e-mail: users-help_at_jaxb.dev.java.net