使用注释插件 + JAXB 在 java 'field' 中插入自定义注释(在 xsd -> java 上) [英] Insert custom annotation in java 'field' using annotate plugin + JAXB (upon xsd -> java)

查看:15
本文介绍了使用注释插件 + JAXB 在 java 'field' 中插入自定义注释(在 xsd -> java 上)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用例:

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

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

问题:

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

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

我的测试 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>

我的测试绑定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>    

我生成的 java 片段:

My generated java snippet:

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

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

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

推荐答案

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

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

  • 二传手
  • 设置器参数
  • 吸气剂
  • 字段

请参阅文档.

这篇关于使用注释插件 + JAXB 在 java 'field' 中插入自定义注释(在 xsd -> java 上)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆