Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

EclipseLink/DesignDocs/293925/Phase8

Phase 8 - Enums

Provide support for Java Enums.

Annotations

The following annotations will be targetted in this phase:

Annotation XML Metadata Tag Package Type Field Method
XmlEnum xml-enum   X    
XmlEnumValue xml-enum-value     X
X

Example: XmlEnum and XmlEnumValue Annotations

Java Metadata

The following example will demonstrate how the XmlEnum and XmlEnumValue annotations can be applied:

org.example.Game.java

package org.example;
 
@javax.xml.bind.annotation.XmlRootElement
public class Game { 
    public Card card;
    public Coin coin;
}

org.example.Card.java

package org.example;
 
@javax.xml.bind.annotation.XmlEnum(String.class)
public enum Card { CLUBS, DIAMONDS, HEARTS, SPADES }

org.example.Coin.java

package org.example;
 
@javax.xml.bind.annotation.XmlType(name="coin-enum")
@javax.xml.bind.annotation.XmlEnum(Integer.class)
public enum Coin {
    @javax.xml.bind.annotation.XmlEnumValue("1") PENNY,
    @javax.xml.bind.annotation.XmlEnumValue("5") NICKEL,
    @javax.xml.bind.annotation.XmlEnumValue("10") DIME,
    @javax.xml.bind.annotation.XmlEnumValue("25") QUARTER
}

XML Metadata

xml-enum

If this is present in XML, the corresponding annotation (determined based on java-enum/enum classname match) will be replaced.

xml-enum-value

If this is present in XML, the corresponding annotation (determined based on java-enum-value/enum field name match) will be replaced. Non-matching XML entries will be combined with those from annotations.

org/example/eclipselink-oxm.xml

This XML file represents metadata overrides for the "org.example" package.

<?xml version="1.0" encoding="US-ASCII"?>
<xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm">
    <xml-enums>
        <xml-enum java-enum="org.example.Card" value="java.lang.String">
            <xml-enum-value java-enum-value="CLUBS">CLUBS</xml-enum-value>
            <xml-enum-value java-enum-value="HEARTS">HEARTS</xml-enum-value>
            <xml-enum-value java-enum-value="SPADES">SPADES</xml-enum-value>
            <xml-enum-value java-enum-value="DIAMONDS">DIAMONDS</xml-enum-value>
        </xml-enum>
        <xml-enum java-enum="org.example.Coin" value="java.lang.Integer">
            <xml-enum-value java-enum-value="PENNY">1</xml-enum-value>
            <xml-enum-value java-enum-value="NICKEL">5</xml-enum-value>
            <xml-enum-value java-enum-value="DIME">10</xml-enum-value>
            <xml-enum-value java-enum-value="QUARTER">25</xml-enum-value>
        </xml-enum>
    </xml-enums>
    <java-types>
        <java-type name="org.example.Game">
            <xml-root-element name="game" />
            <java-attributes>
                <xml-element java-attribute="card" />
                <xml-element java-attribute="coin" />
            </java-attributes>
        </java-type>
        <java-type name="org.eclipse.persistence.testing.jaxb.externalizedmetadata.xmlenum.Coin">
            <xml-type name="coin-enum" />
        </java-type>
    </java-types>
</xml-bindings>

Back to the top