我正在使用 JAXB 的 xjc 工具从 AUTOSAR XSD 文件生成 Java 类,并遇到了 IllegalArgumentException,指示非法的类继承循环。这是我收到的错误消息:
Exception in thread "main" java.lang.IllegalArgumentException: Illegal class inheritance loop. Outer class TargetIPduRefElement may not subclass from inner class: TargetIPduRefElement
at [email protected]/com.sun.codemodel.JDefinedClass._extends(JDefinedClass.java:247)
at [email protected]/com.sun.tools.xjc.generator.bean.ImplStructureStrategy$1._extends(ImplStructureStrategy.java:96)
at [email protected]/com.sun.tools.xjc.generator.bean.BeanGenerator.<init>(BeanGenerator.java:187)
at [email protected]/com.sun.tools.xjc.generator.bean.BeanGenerator.generate(BeanGenerator.java:141)
at [email protected]/com.sun.tools.xjc.model.Model.generateCode(Model.java:259)
at [email protected]/com.sun.tools.xjc.Driver.run(Driver.java:365)
at [email protected]/com.sun.tools.xjc.Driver.run(Driver.java:198)
at [email protected]/com.sun.tools.xjc.Driver._main(Driver.java:117)
at [email protected]/com.sun.tools.xjc.Driver$1.run(Driver.java:76)
有人遇到过类似的问题并找到解决方法吗?对于如何调试此问题的任何见解或建议,我将不胜感激。
为了使 xjc 编译您的 XSD(来自您的链接),我必须添加以下绑定文件
<jaxb:bindings version="3.0"
xmlns:jaxb="https://jakarta.ee/xml/ns/jaxb"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<jaxb:bindings schemaLocation="AUTOSAR_00046.xsd" node="/xs:schema">
<!--
Fixes :
Simple type "{0}" was not mapped to Enum due to EnumMemberSizeCap limit. Facets count: {1}, current limit: {2}. You can use customization attribute "typesafeEnumMaxMembers" to extend the limit.
-->
<jaxb:globalBindings typesafeEnumMaxMembers="1024" />
<!--
Fixes Property "{0}" is already defined. Use <jaxb:property> to resolve this conflict.
-->
<jaxb:bindings node="//xs:attributeGroup[@name='ENTRY']//xs:attribute[@name='BGCOLOR']">
<jaxb:property name="BGCOLORAttr"></jaxb:property>
</jaxb:bindings>
<!--
Fixes Complex type and its child element share the same name "{0}". Use a class customization to resolve this conflict.
-->
<jaxb:bindings node="//xs:group[@name='COMPU-SCALES']/xs:sequence/xs:element[@name='COMPU-SCALES']/xs:complexType">
<jaxb:class name="CompuScalesElement"/>
</jaxb:bindings>
<jaxb:bindings node="//xs:group[@name='TARGET-I-PDU-REF']/xs:sequence/xs:element[@name='TARGET-I-PDU-REF']/xs:complexType">
<jaxb:class name="TargetIPduRefElement"/>
</jaxb:bindings>
</jaxb:bindings>
</jaxb:bindings>
这将解决在没有任何绑定文件的情况下遇到的问题:
typesafeEnumMaxMembers="1024"
将解决枚举未生成的问题(根据最大成员数调整)BGCOLOR
属性TARGETIPDUREF
和 COMPUSCALES
如果不使用 jaxb3/4(基于 jakarta),您可以使用以下内容更改文件头以使其与 jaxb2 兼容
<jaxb:bindings version="2.1"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xs="http://www.w3.org/2001/XMLSchema">