jaxb:枚举列表中的奇怪类转换异常

A4L*_*A4L 12 java xml jaxb oxm java-8

jaxb用来从xsd文件生成java类.它xsd包含一个元素的定义,其内容是与xsd枚举相同定义的常量列表.

当使用oracle jdk1.7(v2.2.4-2)的JAXB引用实现生成类时,可以迭代枚举列表并为它们分配相同类型的变量.

但是,当使用oracle jdk1.8(生成1.8.0_45-b15- 最新发布日期)JAXB引用实现(v2.2.8-b130911.1802)生成类时,不再可能将列表的元素分配给枚举类型的变量.

任何使用增强型for循环分配或迭代的尝试都以a结束 ClassCastException

java.lang.ClassCastException: java.lang.String cannot be cast to so.jaxb.enums.generated.GConstNameType
    at so.jaxb.enums.domain.TestReader.readTest(TestReader.java:36)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Run Code Online (Sandbox Code Playgroud)

列表本身在两种情况下都使用正确的枚举类型进行参数化.

这是一个重现上述问题的代码:

XSD文件

<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns="http://www.w3.org/2001/XMLSchema" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:tns="http://www.foo.com/xmlns/test"
    targetNamespace="http://www.foo.com/xmlns/test"
    attributeFormDefault="unqualified"
    elementFormDefault="qualified">

    <xs:simpleType name="GConstType">
        <xs:list itemType="tns:GConstNameType" />
    </xs:simpleType>
    <xs:simpleType name="GConstNameType">
        <xs:restriction base="xs:string">
            <xs:enumeration value="FOO" />
            <xs:enumeration value="BAR" />
            <xs:enumeration value="BAZ" />
        </xs:restriction>
    </xs:simpleType>

    <xs:complexType name="TestType">
        <xs:all>
            <xs:element name="const-name-list" 
                type="tns:GConstType" minOccurs="0" maxOccurs="1" />
        </xs:all>
    </xs:complexType>

    <xs:element name="test" type="tns:TestType" />

</xs:schema>
Run Code Online (Sandbox Code Playgroud)

测试XML文件

<?xml version="1.0" encoding="UTF-8"?>
<t:test xmlns="http://www.w3.org/2001/XMLSchema"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:t="http://www.foo.com/xmlns/test"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <t:const-name-list>FOO BAR</t:const-name-list>

</t:test>
Run Code Online (Sandbox Code Playgroud)

测试读者

public class TestReader {

    @Test
    public void readTest() throws IOException {
        try (InputStream xml = TestReader.class
                .getResourceAsStream("/so/jaxb/enums/resources/test.xml");
            InputStream xsd = TestReader.class
                .getResourceAsStream("/so/jaxb/enums/resources/test.xsd")) {
            TestType testType = fromXML(TestType.class, xml, xsd);
            List<GConstNameType> constNameList = testType.getConstNameList();
            for(Object constName : constNameList) {
                System.out.println(constName.getClass().getName());
            }
            for(GConstNameType constName : constNameList) {
                System.out.println(constName);
            }
        }
    }

    public static <T> T fromXML(Class<T> _class, InputStream xml, InputStream xsd) {
        XMLStreamReader xsr = null;
        try {
            Source xmlSource = new StreamSource(xml);
            Source xsdSource = new StreamSource(xsd);
            JAXBContext jaxbContext = JAXBContext.newInstance(_class);
            Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
            Schema schema = SchemaFactory
                .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(xsdSource);
            unmarshaller.setSchema(schema);
            xsr = XMLInputFactory.newInstance().createXMLStreamReader(xmlSource);
            JAXBElement<T> jaxbElement = unmarshaller.unmarshal(xsr, _class);
            return jaxbElement.getValue();
        } catch (JAXBException | SAXException | XMLStreamException | FactoryConfigurationError e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if(xsr != null) {
                    xsr.close();   
                }
            } catch(XMLStreamException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

输出jdk1.7

so.jaxb.enums.generated.GConstNameType
so.jaxb.enums.generated.GConstNameType
FOO
BAR
Run Code Online (Sandbox Code Playgroud)

输出jdk1.8

java.lang.String
java.lang.String
<ClassCastException>
Run Code Online (Sandbox Code Playgroud)

从上面的输出可以清楚地看出,该类型的元素java.lang.String被走私到列表中,List<GConstNameType>或者String设置了一个GConstNameType列表而不是一个列表.无论如何,Stringxml文件中的枚举名称都没有映射到java枚举常量.

java运行时在两种情况下都是相同的,它jre来自于jdk1.8.

用于生成的命令:

C:\Oracle\Java\jdk1.7\bin\xjc.exe D:\dev\java\Tests\src\so\jaxb\enums\resources\test.xsd -b D:\dev\java\Tests\src\so\jaxb\enums\resources -d D:\dev\java\Tests/src -p so.jaxb.enums.generated -extension 
Run Code Online (Sandbox Code Playgroud)

C:\Oracle\Java\jdk1.8\bin\xjc.exe D:\dev\java\Tests\src\so\jaxb\enums\resources\test.xsd -b D:\dev\java\Tests\src\so\jaxb\enums\resources -d D:\dev\java\Tests/src -p so.jaxb.enums.generated -extension 
Run Code Online (Sandbox Code Playgroud)
  • 这有什么可能/这里发生了什么?
  • 上面显示的模式定义是否不正确定义枚举?
  • 如何解决这个问题,而不必使用XmlAdapter(一种适用于每个jdk版本的方式)?

编辑

两个生成的包之间唯一的代码差异

两个生成的包之间唯一的代码差异

删除注释

@XmlSchemaType(name = "anySimpleType")
Run Code Online (Sandbox Code Playgroud)

使jdk1.8生成的代码完全正常运行.

  • 为什么较新的JAXB实现将枚举映射到anySimpleType

igr*_*eld 2

您可以将您的 xsd 更改为:

 <xs:complexType name="TestType">
    <xs:sequence>
        <xs:element name="const-name-list">
            <xs:simpleType>
                <xs:list itemType="tns:GConstNameType"/>
            </xs:simpleType>
        </xs:element>
    </xs:sequence>
</xs:complexType>

<xs:simpleType name="GConstNameType">
    <xs:restriction base="xs:string">
        <xs:enumeration value="FOO"/>
        <xs:enumeration value="BAR"/>
        <xs:enumeration value="BAZ"/>
    </xs:restriction>
</xs:simpleType>
Run Code Online (Sandbox Code Playgroud)

这是在 java 8 上运行的。

新的解析器有一些新的限制。

更新:对于您的评论,您可以使用以下内容:

<xs:complexType name="TestType">
    <xs:complexContent>
        <xs:extension base="tns:ListType">
            <xs:sequence/>
        </xs:extension>
    </xs:complexContent>
</xs:complexType>

<xs:complexType name="ListType">
    <xs:sequence>
        <xs:element name="const-name-list">
            <xs:simpleType>
                <xs:list itemType="tns:GConstNameType"/>
            </xs:simpleType>
        </xs:element>
    </xs:sequence>
</xs:complexType>

<xs:complexType name="SecondTestType">
    <xs:complexContent>
        <xs:extension base="tns:ListType">
            <xs:sequence/>
        </xs:extension>
    </xs:complexContent>
</xs:complexType>

<xs:simpleType name="GConstNameType">
    <xs:restriction base="xs:string">
        <xs:enumeration value="FOO"/>
        <xs:enumeration value="BAR"/>
        <xs:enumeration value="BAZ"/>
    </xs:restriction>
</xs:simpleType>
Run Code Online (Sandbox Code Playgroud)