Question

When compiling the following simpleType with the XJC compile (from the JAXB package)...

<xs:simpleType name="test">
    <xs:annotation>
        <xs:appinfo>
            <jaxb:typesafeEnumClass/>
        </xs:appinfo>
    </xs:annotation>
    <xs:restriction base="xs:string">
      <xs:enumeration value="4">
        <xs:annotation>
          <xs:appinfo>
            <jaxb:typesafeEnumMember name="FOUR"/>
          </xs:appinfo>
        </xs:annotation>
      </xs:enumeration>      
      <xs:enumeration value="6">
        <xs:annotation>
          <xs:appinfo>
            <jaxb:typesafeEnumMember name="SIX"/>
          </xs:appinfo>
        </xs:annotation>
      </xs:enumeration>
     </xs:restriction>
</xs:simpleType>

I end up with the following enum in Java (import statements and comments removed)

@XmlEnum
public enum Test {

    @XmlEnumValue("4")
    FOUR("4"),
    @XmlEnumValue("6")
    SIX("6");
    private final String value;

    Test(String v) {
        value = v;
    }

    public String value() {
        return value;
    }

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

}

This is exactly what I want... except for the public String value() method. I would expect the method to be called public String getValue() according to Sun's naming conventions. That way I can easily use it in a JSP-page using EL. Now I have to work my way around it.

Does anybody have any experience in further tweaking the XJC compilation to a more useful enumeration with a getValue() method, instead of a value() method? Or can I add a method or something?

P.S. This occurred in v2.0.3 of JAXB. I downloaded the latest version v2.1.8 and it's the same there...

Was it helpful?

Solution

There's nothing in the JAXB spec that seems to allow this change. I think the only way to do this would be to write a JAXB Plugin.

OTHER TIPS

you could create a small variant of the generated class that only differs from the generated one for the name of this method. then at runtime, you have to make sure your variant is loaded instead of the generated one, playing the classloader game.

Of course, this can only work is the original XSD doesn't change often.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top