jaxb:枚举列表中的奇怪类强制转换exception
我正在使用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循环分配或迭代的尝试都以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)
列表本身在两种情况下都使用正确的枚举类型进行参数化。
这是一个重现上述问题的代码:
XSD文件
测试XML文件
FOO BAR
测试读者
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 constNameList = testType.getConstNameList(); for(Object constName : constNameList) { System.out.println(constName.getClass().getName()); } for(GConstNameType constName : constNameList) { System.out.println(constName); } } } public static T fromXML(Class _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 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); } } } }
输出jdk1.7
so.jaxb.enums.generated.GConstNameType so.jaxb.enums.generated.GConstNameType FOO BAR
输出jdk1.8
java.lang.String java.lang.String
从上面的输出中可以清楚地看到java.lang.String
类型的元素被走私到列表List
或者设置了String
的List而不是GConstNameType
的List。 无论如何,xml文件中的String
枚举名称都没有映射到java enum常量。
java运行时在两种情况下都是相同的,它是来自jdk1.8
的jre
。
用于生成的命令:
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
和
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
- 这有什么可能/这里发生了什么?
- 上面显示的模式定义是否不正确定义枚举?
- 如何解决这个问题,而不必使用
XmlAdapter
(一种可以在每个jdk版本上工作的方式)?
编辑
两个生成的包之间唯一的代码差异
删除注释
@XmlSchemaType(name = "anySimpleType")
使jdk1.8
生成的代码完全正常运行。
- 为什么较新的
JAXB
实现将枚举映射到anySimpleType
?
你可以将你的xsd改为:
这是在java 8上工作。
新的解析器有一些新的限制。
更新 :您的评论可以使用: