Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAXB 2.1 implementing Comparable<T> for the generated Class

  • 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.

like image 784
Rao Avatar asked Mar 11 '23 10:03

Rao


2 Answers

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

<inheritance:implements><![CDATA[java.lang.Comparable<Fragment>]]></inheritance:implements>
like image 102
Rao Avatar answered Mar 18 '23 01:03

Rao


JAXB2-Basics support generics.

Just use:

<inheritance:implements>java.lang.Comparable&lt;Fragment&gt;</inheritance:imple‌​ments>
like image 21
lexicore Avatar answered Mar 18 '23 02:03

lexicore