抑制JAXB生成的类上的编译器警告 [英] Suppress compiler warnings on JAXB generated classes

查看:144
本文介绍了抑制JAXB生成的类上的编译器警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能与此问题重复(避免编译器警告由xjc生成的代码)但由于我不太熟悉XJC / JAXB自定义绑定的特性,我猜我可能误解了上述问题。

This is possibly a duplicate of this question (Avoiding Compiler warnings on code generated by xjc) but since I am not very well versed in XJC/JAXB custom bindings idiosyncrasies, I'll presume I may have misunderstood the mentioned question.

我的问题看起来很简单 - 如何在生成的JAXB类中添加 @SuppressWarnings(all)注释?我们的项目有0个警告政策,在JAXB生成步骤之后,我们最终会发出350多个警告,这些警告只是可怕的噪音。

My question seems simple enough - how can I add @SuppressWarnings("all") annotation to generated JAXB class? We have 0 warning policy on our project and after JAXB generation step, we end up with 350+ warnings which are just terrible noise.

我希望有一个简单的标志XJC或至少是提供此类信息的简单方法,但我找不到任何信息。

I would expect either a simple flag on the XJC or at least an easy way to provide such info but I cannot find any.

从我在旅行中看到的情况来看,人们会做以下事情之一:

From what I've seen in my travels, people do one of these things:


  1. @ lexicore的 JAXB2 Annotate Plugin ,根据示例,似乎可以完成这项工作,但是添加一个完整的框架和数百KB的代码只是为了向一堆类添加简单的注释?真的??,

  2. 自定义JAXB绑定(我可能会弄错,但这看起来很神秘,我不确定是否有可能以这种方式进行事件),

  3. 将在JAXB生成后进行搜索和替换的自定义ANT目标,

  4. Eclipse 4+似乎可以选择过滤掉所选项目的警告但由于各种历史/遗留原因,我们坚持使用Eclipse 3.7.2(Indigo),

  5. 自定义XJC插件,它会进行注释插入(如其中一位评论者所建议的,请参阅此处),

  6. 将所有生成的类放在JAR中,使JAR成为项目的依赖项,然后完全删除生成的代码。

  1. @lexicore's JAXB2 Annotate Plugin which, according to examples, seems to do the job but to add a whole framework and hundreds of kB of code just to add simple annotation to a bunch of classes? Really??,
  2. Custom JAXB bindings (I may be mistaken but this seems very arcane and I am not fully sure if it were event possible to do it this way),
  3. Custom ANT target which will do a search-and-replace post-JAXB generation,
  4. Eclipse 4+ seems to have an option to filter out warnings on selected projects but for various historical/legacy reasons we're stuck on Eclipse 3.7.2 (Indigo),
  5. Custom XJC plugin which would do the annotation insertion (as suggested by one of the commenters, see here),
  6. Placing all of the generated classes in a JAR, making the JAR a dependency of the project and then removing generated code altogether.

以前,我们通过在单独的Eclipse项目中推送与XML相关的代码然后在p上禁用警告/错误来解决这个问题。 roject。现在,作为代码整合/重构的一部分,我们已经重新调整了一些东西,不再拥有那种奢侈品。

Formerly, we dealt with this by shoving the XML-related code in a separate Eclipse project and then disabling the warnings/errors on the project. Now, as part of code consolidation/refactoring, we've reshuffled things around and no longer have that luxury.

对于这样一个看似微不足道的问题,真的没有优雅的解决方案吗? ?非常感谢任何反馈/想法。

Is there really no elegant solution for such a seemingly trivial problem? Any feedback/thoughts are greatly appreciated.

FWIW,我们的项目使用Ant作为构建系统。

FWIW, our project uses Ant as build system.

推荐答案

你必须使用这个插件: jaxb2-annotate-plugin

You have to use this plugin : jaxb2-annotate-plugin

2解决方案:

1 - 修改你的xsd并添加这种块

1 - Modify your xsd and add this kind of block

<xsd:element name="foobar" type="xsd:string">
    <xsd:annotation>
        <xsd:appinfo>
            <annox:annotate>@java.lang.SuppressWarnings("all")</annox:annotate>
        </xsd:appinfo>
    </xsd:annotation>
</xsd:element>

2 - 创建自定义绑定

2 - Create a custom binding

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<jaxb:bindings
    xmlns:jaxb="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:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:annox="http://annox.dev.java.net"
    xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd"
    jaxb:extensionBindingPrefixes="xjc annox"
    version="2.1">
    <jaxb:bindings schemaLocation="schema.xsd" node="/xs:schema">
        <annox:annotate target="class">@java.lang.SuppressWarnings("all")</annox:annotate>
    </jaxb:bindings>
</jaxb:bindings>

这篇关于抑制JAXB生成的类上的编译器警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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