重命名JAXB生成的类名方法名

JAXB把schema转换成java class的时候,会自动生成类名,方法名。在有些时候,这些名字会出现问题,比如,schema里面允许有同名的元素,这会导致生成的java类里面有两个同名的成员,显然是不对的。除此之外,JAXB自动生成的方法名,有时候会很长,很不友好。怎么解决这类问题?幸好jaxb提供了定制机制。

 

JAXB提供了用annotation进行扩展的机制。下面的这个例子会生成

 

如果没有注释部分的annotation,生成的java class会包含一个名字很长的方法:

  

把注释部分打开,生成的方法名就体面多了:

 

下面的摘自http://jaxb.java.net/tutorial/section_5_3-Overriding-Names.html,介绍了更多类似的例子。

5.3 Overriding Names

Overriding the name of a class or of an element's child is something you may have to do to avoid name clashes. Usually it is more convenient to fix the XML schema, but you may not always be at liberty to do so.

Let's assume that you have a schema where a complex type has been given the uninspired name List:

<xsd:complexType name="List">
  <xsd:sequence>
    <xsd:element name="items" type="ItemType"
                 minOccurs="0" maxOccurs="unbounded"/>
  </xsd:sequence>
</xsd:complexType>

JAXB's schema compiler circumnavigates the threatening name clash between this class and java.util.List, required within the generated class code for declaring the field items. But wherever this class is used it is potentially in conflict with its namesake from java.util. To avoid having to use the full class name for one of these two classes, you might override the class name for the generated class:

<xsd:complexType name="List">
  <xsd:annotation>
    <xsd:appinfo>
      <jxb:class name="MyListType"/>
    </xsd:appinfo>
  </xsd:annotation>
  <xsd:sequence>
    <xsd:element name="items" type="ItemType"
                 minOccurs="0" maxOccurs="unbounded"/>
  </xsd:sequence>
</xsd:complexType>

Another unlucky choice for the name of an element or attribute would be Class. For this one, JAXB's workaround is the standard programmer's choice, i.e., it is replaced by Clazz. If you don't fancy this, you can request your own substitute, using an jxb:property element, like this:

<xsd:complexType name="School">
  <xsd:sequence>
    <xsd:element name="class" type="ClassType">
      <xsd:annotation>
        <xsd:appinfo>
          <jxb:property name="klass"/>
        </xsd:appinfo>
      </xsd:annotation>
    </xsd:element>
  </xsd:sequence>
</xsd:complexType>

Other Java keywords won't constitute a problem. The instance variable will be given a name consisting of an underscore followed by the letters of the keyword.

Yet another reason for changing a name arises from the use of the same name for a sub-element and an attribute within the same complex type definition. (Arguably this isn't good XML design. But, believe me, it does happen.)

<xsd:complexType name="ClassType">
  <xsd:sequence>
    <xsd:element name="grade" type="xsd:string" minOccurs="0"/>
  </xsd:sequence>
  <xsd:attribute name="grade" type="xsd:string" use="optional"/>
</xsd:complexType>

We'll resolve this conflict by renaming the attribute to gradeAttr, this time by specifying the property name in the bindings file.

<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
               xmlns:xsd="http://www.w3.org/2001/XMLSchema"
               jxb:version="2.0">
  <jxb:bindings schemaLocation="School.xsd" node="/xsd:schema">
    <jxb:bindings node="//xsd:attribute[@name='grade']">
      <jxb:property name="gradeAttr"/>
    </jxb:bindings>
  </jxb:bindings>
</jxb:bindings>

XML Schema even lets you define a sequence where two individual elements have the same tag

<xsd:complexType name="StrangeType">
  <xsd:sequence>
    <xsd:element name="taga" type="xsd:string"/>
    <xsd:element name="tagb" type="xsd:string"/>
    <xsd:element name="taga" type="xsd:string"/>
  </xsd:sequence>
</xsd:complexType>

There is no such thing as a Java class that has two distinct fields with the same name. You must help the schema compiler to resolve this conflict, by renaming either element:

    ...
    <xsd:element name="taga" type="xsd:string">
      <xsd:annotation>
        <xsd:appinfo>
          <jxb:property name="taga2"/>
        </xsd:appinfo>
      </xsd:annotation>
    </xsd:element>
    ...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值