重命名JAXB生成的类名方法名
来源:互联网 发布:淘宝直播秒杀技巧 编辑:程序博客网 时间:2024/05/29 14:32
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> ...
- 重命名JAXB生成的类名方法名
- vc重命名类名
- eclipse里面的java类名重命名,修改包名
- Mysql拆库两种重命名schema名的方法及其性能
- thinkphp 获取当前的方法名,类名,函数名
- 使Eclipse或AndroidStudio的代码模板自动生成类名和方法名
- eclipse里面的java类名重命名F2,Rename
- Linux自动生成设备名的方法
- 原来Java的类名,方法名,变量名,参数名可以用中文啊
- 重命名计算机名.vbs
- myeclipse 重命名项目名
- Project---重命名工程名
- JQuery重命名库名
- Android重命名包名
- AndroidStudio重命名包名
- phonegap创建工程 生成指定的包名 类名 应用名
- 利用Eclipse或AndroidStudio的代码模板,自动生成 Log.e(TAG,"类名.方法名. msg"); 格式的代码
- java 获取调用类的类名和方法名
- android GPS DEMO
- .net 自带Ajax简析--UpdatePanel
- 编写IOCP时的几个注意事项(Mirosoft)
- Eclipse ,MyEclipse 代码提示功能设置与优化(转) 缓解 卡 问题
- 【转】Ubuntu 中软件的安装、卸载以及查看的方法总结
- 重命名JAXB生成的类名方法名
- MTK文件操作常用函数
- 多进程服务器中,epoll的创建应该在创建子进程之后
- 通过SetWorkingSet减少内存使用(翻译)
- 分享Lucene中文分词组件"IK Analyzer V3.2.8"
- 【转】ubuntu下安装bochs 2.4.2
- ruby ROR中异常处理rescue与raise介绍
- 测试工具不同机型设置
- linux配置Java环境