0

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.

lexicore
  • 42,748
  • 17
  • 132
  • 221

1 Answers1

2

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.

lexicore
  • 42,748
  • 17
  • 132
  • 221
  • Thank you for the quick reply. It's for a simulator code, so I'd just go for a small hack instead of investing much time on it. Aw thanks for your great work! keep it up :) – Kalhari Liyanagama Feb 20 '15 at 11:08
  • @KalhariLiyanagama But please do file an issue. Maybe I'll get to it somewhen. And thanks for the feedback. :) – lexicore Feb 20 '15 at 11:23
  • The `annotateEnumValueMethod` has been implemented by now (thank you, @lexicore !), but I can't get it to work for the use case, described in this question. I have tried: `@JsonValue @org.codehaus.jackson.annotate.JsonValue @com.fasterxml.jackson.core.annotate.JsonValue` but nothing works. @lexicore, can you please give me an advice? Thank you! – Pavel A May 15 '18 at 17:39
  • @PavelA Please send me a PR with the reproducing project here: https://github.com/highsource/jaxb2-annotate-plugin-support as `e/enumValue`. I'll take a look. – lexicore May 15 '18 at 17:55
  • 1
    Thank you for the quick reply. It's a closed-source project, so I'll try to create a simple one to illustrate the issue and then send a PR. – Pavel A May 15 '18 at 18:12
  • @lexicore, the PR has been created. Thanks. – Pavel A May 16 '18 at 13:18
  • @PavelA Thank you for the PR. I've fixed [your project](https://github.com/highsource/jaxb2-annotate-plugin-support/tree/master/e/enumValue). Updated a couple of versions and added `jackson-annotations` to the classpath of the [tag:maven-jaxb2-plugin]. Worked for me, I get `@JsonValue public String value() { return value; }` generated. – lexicore May 17 '18 at 18:55
  • Thank you very much, @lexicore! I'm complete newbie in maven, so guessed the issue is in pom.xml, but couldn't get it exactly right. – Pavel A May 18 '18 at 14:40