enumsjaxbmaven-jaxb2-pluginjaxb2-basicsjaxb2-annotate-plugin

How to annotate enum using jaxb2-basics-annotate?


I'm using maven-jaxb2-plugin and jaxb2-basics-annotate plugins to auto generate POJOs from my xsd. I've successfully generated annotations in POJOs. I need to apply an annotation to a method in an enum but cannot figure out how to do it.

xsd has,

    <xsd:simpleType name="DeliveryStatus">
    <xsd:restriction base="xsd:string">
        <xsd:enumeration value="DeliveredToTerminal" />
        <xsd:enumeration value="DeliveryUncertain" />
        <xsd:enumeration value="DeliveryImpossible" />
        <xsd:enumeration value="DeliveredToNetwork" />
        <xsd:enumeration value="MessageWaiting" />
        <xsd:enumeration value="DeliveryNotificationNotSupported" />
    </xsd:restriction>
    </xsd:simpleType>

generated file

@XmlType(name = "DeliveryStatus")
@XmlEnum
public enum DeliveryStatus {

@XmlEnumValue("DeliveredToTerminal")
DELIVERED_TO_TERMINAL("DeliveredToTerminal"),
@XmlEnumValue("DeliveryUncertain")
DELIVERY_UNCERTAIN("DeliveryUncertain"),
@XmlEnumValue("DeliveryImpossible")
DELIVERY_IMPOSSIBLE("DeliveryImpossible"),
@XmlEnumValue("MessageWaiting")
MESSAGE_WAITING("MessageWaiting"),
@XmlEnumValue("DeliveredToNetwork")
DELIVERED_TO_NETWORK("DeliveredToNetwork"),
@XmlEnumValue("DeliveryNotificationNotSupported")
DELIVERY_NOTIFICATION_NOT_SUPPORTED("DeliveryNotificationNotSupported");
private final String value;

DeliveryStatus(String v) {
    value = v;
}

public String value() {
    return value;
}

public static DeliveryStatus fromValue(String v) {
    for (DeliveryStatus c: DeliveryStatus.values()) {
        if (c.value.equals(v)) {
            return c;
        }
    }
    throw new IllegalArgumentException(v);
}
}

What I need is to add JsonValue annotation to value method above.

I tried following and some other tweaksbut in binding.xjb nothing works.

<jaxb:bindings node="xs:simpleType[@name='DeliveryStatus']">
    <annox:annotate target="field">
     <annox:annotateEnum annox:class="org.codehaus.jackson.annotate.JsonValue"/>
    </annox:annotate>
</jaxb:bindings> 

Is there something called annotateEnum? Can it work, if so how?

Please help.


Solution

  • Disclaimer: I'm the author of the jaxb2-annotate-plugin.

    Yes, there is an annotateEnum customization element (see the docs). But it only applies the annotation to the enum class itself, i.e. to public enum DeliveryStatus {...}. So this does not solve your problem with value(), this can't be annotated at the moment.

    Please file an issue here:

    Would be nice to have a test schema here (please send me a PR):

    Unfortunately, I can't promise any due date. I think the fastet way you'de gert results is to try to implement it yourself. See this part of the code:

    You'd basically need to add annotateEnumValueMethod handler similar to how the annotateEnum is done. The only tricky part is that you'll need to annotate not the class but the value() method, but it's not difficult. I'll be open to PRs here.

    Hope it helps.