使用注释插件 + JAXB(基于 xsd -> java)在 java 'field' 中插入自定义注释

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9799837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 07:47:19  来源:igfitidea点击:

Insert custom annotation in java 'field' using annotate plugin + JAXB (upon xsd -> java)

javaxmljaxbannotatejaxb2-basics

提问by Hari

Use case:

用例:

Wanna insert custom annotation to fields in java class generated by JAXB

想在 JAXB 生成的 java 类中的字段中插入自定义注释

Problem:

问题:

Using Annotate plugin + JAXB [1], am able to successfully insert custom annotations but they are getting inserted at getter method rather than field. Morphia (mongo DB) annotations (that i actually want to insert) however can annotate only java fields [2].

使用 Annotate 插件 + JAXB [1],能够成功插入自定义注释,但它们是在 getter 方法而不是字段中插入的。Morphia (mongo DB) 注释(我实际上想要插入)但是只能注释 java 字段 [2]。

My test xsd:

我的测试 xsd:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" jaxb:version="2.1"
xmlns:annox="http://annox.dev.java.net" jaxb:extensionBindingPrefixes="annox">

<xsd:element name="hoo" type="External" />
<xsd:complexType name="External">
    <xsd:sequence>
        <xsd:element name="bar" type="xsd:string" />
        <xsd:element name="hoobar" type="xsd:string" />
    </xsd:sequence>
</xsd:complexType>
</xsd:schema>

My test binding xjb:

我的测试绑定 xjb:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<jaxb:bindings
 version="2.1"
 xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"

  xmlns:annox="http://annox.dev.java.net" jaxb:extensionBindingPrefixes="annox">
  <jaxb:bindings schemaLocation="external.xsd" node="/xs:schema">

<jaxb:bindings node="xs:complexType[@name='External']/xs:sequence/xs:element[@name='bar']">
  <annox:annotate>
    <annox:annotate
      annox:class="java.lang.SuppressWarnings"
      impl="com.acme.foo.MyFieldBridge">
    </annox:annotate>
  </annox:annotate>
</jaxb:bindings>    

My generated java snippet:

我生成的java片段:

 @XmlElement(required = true)
protected String bar;
@XmlElement(required = true)
protected String hoobar;

/**
 * Gets the value of the bar property.
 * 
 * @return
 *     possible object is
 *     {@link String }
 *     
 */
@SuppressWarnings({

})
public String getBar() {
    return bar;
}

As you can see, i want to annotate "bar" field. Please advise. Ask for more if needed.

如您所见,我想注释“bar”字段。请指教。如果需要,请询问更多。

[1] Generate @Indexed annotation using Jaxb or HyperJaxb
[2] For sample see @Id annotation of Morphia

[1]使用 Jaxb 或 HyperJaxb 生成 @Indexed 注释
[2] 示例参见 Morphia 的 @Id 注释

采纳答案by lexicore

Ok, you figured it out yourself. Use <annox:annotate target="field">to annotate a field. Other options are:

好吧,你自己想出来了。使用<annox:annotate target="field">注释字段。其他选项是:

  • setter
  • setter-parameter
  • getter
  • field
  • class
  • 二传手
  • 设置参数
  • 吸气剂
  • 场地
  • 班级

See the documentation.

请参阅文档

回答by kavai77

Just one more thing: you need to put the fieldattribute to the outer <annox:annotate>tag:

还有一件事:您需要将field属性放在外部<annox:annotate>标签中:

<annox:annotate target="field">
    <annox:annotate annox:class="java.lang.SuppressWarnings"/>
</annox:annotate>

Putting it to the same tag as the annox:classattribute resides might not work. That happend to me.

将它放在与annox:class属性所在的标签相同的标签中可能不起作用。那发生在我身上。