1

使用 XJC 编译器(来自 JAXB 包)编译以下 simpleType 时...

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

我最终在 Java 中得到了以下枚举(删除了导入语句和注释)

@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());
    }

}

这正是我想要的......除了public String value()方法。我希望public String getValue()根据 Sun 的命名约定调用该方法。这样我就可以轻松地在使用 EL 的 JSP 页面中使用它。现在我必须解决它。

getValue()有没有人有任何经验将 XJC 编译进一步调整为使用方法而不是方法的更有用的枚举value()?或者我可以添加一个方法或什么?

PS 这发生在 JAXB 的 v2.0.3 中。我下载了最新版本v2.1.8 和那里一样...

4

2 回答 2

1

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.

于 2008-10-28T16:44:59.610 回答
0

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.

于 2012-04-06T21:51:30.587 回答