4
  • Using Jaxb 2.1 to generate java code from .xsd
  • jaxb2-basics plug-in is used
  • Wants to have generated Class Fragment to implement Comparable<Fragment>
public class Fragment implements Serializable, Comparable<Fragment> {
  ...
  public int compareTo(Fragment other) {
    .....
    return 0;
  }
}

With the below jaxb bindings file

<?xml version="1.0" encoding="UTF-8"?>
<jxb:bindings xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
    xmlns:ci="http://jaxb.dev.java.net/plugin/code-injector"
    xmlns:inheritance="http://jaxb2-commons.dev.java.net/basic/inheritance"
    jxb:extensionBindingPrefixes="xjc" 
    jxb:version="2.1">
    <jxb:bindings>
        <jxb:globalBindings>
            <xjc:serializable uid="12343" />
        </jxb:globalBindings>
    </jxb:bindings>
    <jxb:bindings schemaLocation="../schemas/Fragment.xsd"
        version="1.0" node="/xs:schema">
        <jxb:bindings node="//xs:element[@name='Fragment']/xs:complexType">         
            <inheritance:implements>java.lang.Comparable</inheritance:implements>
            <ci:code>           
public int compareTo(Fragment other) {

    return fragmentVersion.compareTo(other.fragmentVersion);
}
            </ci:code>
        </jxb:bindings>
    </jxb:bindings>
</jxb:bindings>

Able to generate class as show below:

public class Fragment implements Serializable, Comparable {
  ...
  public int compareTo(Fragment other) {
    return fragmentVersion.compareTo(other.fragmentVersion);
  }
}

Issue: As you see, class is generated as implements Comparable instead of implements Comparable<Fragment>.

Am sure, missing something. Any help to resolve is appreciated.

Rao
  • 20,781
  • 11
  • 57
  • 77
  • 1
    Have you actually tried `java.lang.Comparable<Fragment>`? – lexicore Aug 12 '16 at 17:55
  • @lexicore, I tried `java.lang.Comparable` which was showing error saying need closing tag. Just tried your suggestion and that works. That should be an answer, and happy to accept it. Thank you so much. – Rao Aug 13 '16 at 00:46

2 Answers2

4

While testing the solution provided by lexicore, noticed another way to resolve the issue:

<inheritance:implements><![CDATA[java.lang.Comparable<Fragment>]]></inheritance:implements>
Rao
  • 20,781
  • 11
  • 57
  • 77
3

JAXB2-Basics support generics.

Just use:

<inheritance:implements>java.lang.Comparable&lt;Fragment&gt;</inheritance:imple‌​ments>
lexicore
  • 42,748
  • 17
  • 132
  • 221