- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在使用 jaxb
从 xsd
生成 java 类文件。 xsd
包含一个元素的定义,其内容是在相同的 xsd
中定义的常量列表作为枚举。
当使用来自 oracle 的 jdk1.7
的 JAXB 引用实现生成类时( v2.2.4-2
) 可以遍历枚举列表并为它们分配相同类型的变量。
但是,当使用 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 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>
测试 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>
测试读者
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);
}
}
}
}
输出jdk1.7
so.jaxb.enums.generated.GConstNameType
so.jaxb.enums.generated.GConstNameType
FOO
BAR
输出jdk1.8
java.lang.String
java.lang.String
<ClassCastException>
从上面的输出可以清楚地看出 java.lang.String
类型的元素被偷运到名单List<GConstNameType>
或 String
的列表s 被设置而不是 GConstNameType
list 。无论如何 String
xml 文件中的枚举名称未映射到 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
和
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 更改为:
<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>
这适用于 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>
关于java - jaxb:枚举列表上的奇怪类转换异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30440094/
如标题所示,ans_list是一个答案列表,ans_index是一个数字(答案在词汇表中的索引,但与atm无关) 这里生成的 tree.anslist 是什么? (例如,仅针对第一个),忽略迭代。 f
我目前将用户的输入存储在逗号分隔的列表中,如下所示: Userid | Options 1 | 1,2,5 用户在一个数组形式中勾选一组选项,然后用逗号连接起来 1,2,5 然后 MySQ
我目前将用户的输入存储在逗号分隔的列表中,如下所示: Userid | Options 1 | 1,2,5 用户在一个数组形式中勾选一组选项,然后用逗号连接起来 1,2,5 然后 MySQ
我想知道如何完全展平列表和包含它们的东西。除其他外,我想出了一个解决方案,它可以将具有多个元素的东西滑倒并将它们放回原处,或者在滑倒后将具有一个元素的东西拿走。 这与 How do I “flatte
我想知道如何完全展平列表和包含它们的东西。除其他外,我想出了一个解决方案,它可以将具有多个元素的东西滑倒并将它们放回原处,或者在滑倒后将带有一个元素的东西拿走。 这与 How do I “flatte
这个问题已经有答案了: Convert nested list to 2d array (3 个回答) 已关闭 7 年前。 java中有没有快捷方式可以转换 List> 到 String[][] ?
我在排序时遇到问题 List> 。我创建了一个自定义比较器,在其中编写了对数据进行排序的代码。 public class CustomComparator implements Comparator
这个问题已经有答案了: 已关闭10 年前。 Possible Duplicate: Java Generics: Cannot cast List to List? 我只是想知道为什么下面的java代
试图想出一个 LINQy 方法来做到这一点,但我什么也没想到。 我有一个对象列表<>,其中包含一个属性,该属性是逗号分隔的字母代码列表: lst[0].codes = "AA,BB,DD" lst[1
假设我有这些任务: points = [] point = (1, 2) 我怎么会这样做: points += point 它工作得很好,并且给了我点 = [1, 2]。但是,如果我这样做: poin
如何在 scala 中将 List[Task[List[Header]]] 类型转换为 Task[List[Header]]。 我有一个方法返回 Task[List[Header]] 并多次调用 do
如何在 Java 中查找二维列表的元素? 我有一个参数为 List> 的函数我想知道如何找到这个列表的行和列。 最佳答案 如果你喜欢 List> obj 然后你就可以像这样访问 obj.get(cur
分配 List到 List工作正常。 分配 List>到 List>不编译。 代码 public class Main { public static void main(String[] a
我正在用 Java 编写一个方法,该方法必须接收并迭代 Serializable 的 List。 有什么区别: public void myMethod(List list) { } 和 public
我看到很多人想用 mvvm 更新网格/列表/树的一部分,但他们不想刷新整个列表。 对于所有遇到此问题的人,我做了以下示例。 希望这对你有用。 最佳答案 这是一个简单的例子。整个代码中最重要的是: Bi
我正在为现有的 C++ 库编写包装器,该库使用列表,其中 T 是自定义结构。我被建议使用 vector 而不是列表,但我试图避免修改库。 为了更好地理解这个场景,我做了一个简单的应用程序,使用一个列表
List list List list 这两种声明有什么区别吗? 谢谢, 最佳答案 是的。 List可以包含所有派生自 Base 的不同事物的混合物. List包含同质项(从某种意义上说,它们必须全部
有人可以尽可能详细地解释以下类型之间的区别吗? List List List 让我更具体一点。我什么时候想使用 // 1 public void CanYouGiveMeAnAnswer(List l
我有一个元组列表,每个元组都是一对列表。所以我的数据看起来像: mylist = [(['foo', 'bar'], ['bar', 'bar']),(['bar', 'bar'],['bar', '
也许是一个时髦的标题,但我遇到了以下问题: 给定一个类型为 (a * b) list 的列表,我想创建一个类型为 (a * b list) list 的新列表。一个例子: 给定列表 let testL
我是一名优秀的程序员,十分优秀!