Search code examples
xmlxsdautosar

JAXB xjc: Resolving Illegal Class Inheritance Loop When Generating Classes from AUTOSAR XSD


I'm working with JAXB's xjc tool to generate Java classes from an AUTOSAR XSD file and encountered an IllegalArgumentException indicating an illegal class inheritance loop. Here's the error message I received:

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)

Has anyone encountered a similar issue and found a way to resolve it? I'd appreciate any insights or suggestions on how to debug this problem.


Solution

  • In order to make xjc compile your XSD (from your link), I had to add the following binding file

    <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 &lt;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>
    

    This will resolve the problem encounter without any binding file :

    • typesafeEnumMaxMembers="1024" will solve the enum not being generated (adjust according max members)
    • "Property XXX is already defined. Use <jaxb:property> to resolve this conflict." for BGCOLOR attribute
    • "Complex type and its child element share the same name XXX" for both TARGETIPDUREF and COMPUSCALES

    If not using jaxb3/4 (jakarta-based), you can change the header of the file to make it compatible to jaxb2 with the following

    <jaxb:bindings version="2.1"
                   xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
                   xmlns:xs="http://www.w3.org/2001/XMLSchema">