JAXB 2.1 为生成的 Class 实现 Comparable<T>
JAXB 2.1 implementing Comparable<T> for the generated Class
- 使用 Jaxb 2.1 从 .xsd
生成 java 代码
- 使用jaxb2-basics插件
- 想生成Class
Fragment
来实现Comparable<Fragment>
public class Fragment implements Serializable, Comparable<Fragment> {
...
public int compareTo(Fragment other) {
.....
return 0;
}
}
使用下面的 jaxb 绑定文件
<?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>
能够生成class如下所示:
public class Fragment implements Serializable, Comparable {
...
public int compareTo(Fragment other) {
return fragmentVersion.compareTo(other.fragmentVersion);
}
}
问题:如您所见,class 生成为 implements Comparable
而不是 implements Comparable<Fragment>
。
我确定,遗漏了一些东西。感谢任何解决问题的帮助。
JAXB2-Basics 支持泛型。
只需使用:
<inheritance:implements>java.lang.Comparable<Fragment></inheritance:implements>
在测试 lexicore 提供的解决方案时,注意到另一种解决问题的方法:
<inheritance:implements><![CDATA[java.lang.Comparable<Fragment>]]></inheritance:implements>
- 使用 Jaxb 2.1 从 .xsd 生成 java 代码
- 使用jaxb2-basics插件
- 想生成Class
Fragment
来实现Comparable<Fragment>
public class Fragment implements Serializable, Comparable<Fragment> {
...
public int compareTo(Fragment other) {
.....
return 0;
}
}
使用下面的 jaxb 绑定文件
<?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>
能够生成class如下所示:
public class Fragment implements Serializable, Comparable {
...
public int compareTo(Fragment other) {
return fragmentVersion.compareTo(other.fragmentVersion);
}
}
问题:如您所见,class 生成为 implements Comparable
而不是 implements Comparable<Fragment>
。
我确定,遗漏了一些东西。感谢任何解决问题的帮助。
JAXB2-Basics 支持泛型。
只需使用:
<inheritance:implements>java.lang.Comparable<Fragment></inheritance:implements>
在测试 lexicore 提供的解决方案时,注意到另一种解决问题的方法:
<inheritance:implements><![CDATA[java.lang.Comparable<Fragment>]]></inheritance:implements>