views:

2396

answers:

2

Hi there,

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...

+1  A: 

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.

Thanks for thinking along philvarner... Currently I've worked my way around this. I've also posted it on the JAXB forum (http://forums.java.net/jive/message.jspa?messageID=310818), but no answer there either... we'll see. IMHO it's flawed code generation. I'll let you know if the JAXB plugin worked.
Johan Pelgrim
@Johan, what was the workaround?
matt b
A: 

@Johan, what was the workaround?

Richard