Friday, September 23, 2011

JAXB and Java 5 Enums

In my previous post about Java 5 Enums, I wrote about how Java 5 provides capabilities to add behavior (via member fields and methods) within the enum class. This can be leveraged to provide the switching-like on the enumeration constants. So instead of the following code fragment: We can define an abstract method in the Enum specification, and override that method in each of the Enum definitions as shown here. However there can be cases where it is not possible to change the Enum class in order to define an abstract method, if the Enum class has been generated, such as via JAXB compiler. In that case, the only way we can provide any custom behavior is by programming a switch construct as above in a class that leverages the enum. Digressing a little bit from the main point, I would also like to point out how JAXB treats different XSD enum definitions. See the examples below from a file, example.xsd which contains the 2 schema definitions, viz. CountryCurrencyEnum and CountryEnum. The above example demonstrates 2 Enum examples. The currency Enum is an example of inline definition of the Enum, while the Country enum is an example of a external Enum definition. When you run the latter explicit enum definition through the XJC compiler, it produces a separate Enum class as below: However the XJC compiler will produce the following file when it sees the inline Enum definition. So if we are interested in generating Java 5 style enums via JAXB, we are better off creating a separate definition in the xsd file.

No comments: