简体   繁体   English

抑制JAXB生成的类的编译器警告

[英]Suppress compiler warnings on JAXB generated classes

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. 这可能是这个问题的重复( 避免编译器对xjc生成的代码的警告 ),但由于我不太熟悉XJC / JAXB自定义绑定的特性,我可能会认为我可能误解了上述问题。

My question seems simple enough - how can I add @SuppressWarnings("all") annotation to generated JAXB class? 我的问题似乎很简单 - 如何将@SuppressWarnings("all")注释添加到生成的JAXB类中? We have 0 warning policy on our project and after JAXB generation step, we end up with 350+ warnings which are just terrible noise. 我们的项目有0个警告政策,在JAXB生成步骤之后,我们最终会发出350多个警告,这些警告只是可怕的噪音。

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. 我希望XJC上有一个简单的标志,或者至少提供这样的信息的简单方法,但我找不到任何。

From what I've seen in my travels, people do one of these things: 从我在旅行中看到的情况来看,人们会做以下事情之一:

  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? @ lexicore的JAXB2 Annotate插件 ,根据示例,似乎可以完成这项工作,但是添加一个完整的框架和数百KB的代码只是为了向一堆类添加简单的注释? 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), 自定义JAXB绑定(我可能会弄错,但这似乎非常晦涩,我不完全确定它是否可能以这种方式执行),
  3. Custom ANT target which will do a search-and-replace post-JAXB generation, 自定义ANT目标,它将在JAXB后生成搜索和替换,
  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), Eclipse 4+似乎可以选择过滤掉所选项目的警告,但出于各种历史/遗留原因,我们仍然坚持使用Eclipse 3.7.2(Indigo),
  5. Custom XJC plugin which would do the annotation insertion (as suggested by one of the commenters, see here ), 可以进行注释插入的自定义XJC插件(如其中一位评论者的建议,请参见此处 ),
  6. Placing all of the generated classes in a JAR, making the JAR a dependency of the project and then removing generated code altogether. 将所有生成的类放在JAR中,使JAR成为项目的依赖项,然后完全删除生成的代码。

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. 以前,我们通过在单独的Eclipse项目中推送与XML相关的代码然后禁用项目上的警告/错误来解决这个问题。 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, our project uses Ant as build system. FWIW,我们的项目使用Ant作为构建系统。

You have to use this plugin : jaxb2-annotate-plugin 你必须使用这个插件: jaxb2-annotate-plugin

2 solutions : 2解决方案:

1 - Modify your xsd and add this kind of block 1 - 修改你的xsd并添加这种块

<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 - Create a custom binding 2 - 创建自定义绑定

<?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>

Disclaimer : I am the author of the jaxb2-annotate-plugin mentioned in @ToYonos answer. 免责声明 :我是@ToYonos答案中提到的jaxb2-annotate-plugin的作者。

This is not a standalone answer but rather an addition to @ToYonos answer. 这不是一个独立的答案,而是@ToYonos答案的补充。

I would like to address the following point from the question. 我想从这个问题中解决以下几点。

  1. Annox plugin from Atlassian's JAXB2 Basics framework/project (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??), 来自Atlassian的JAXB2 Basics框架/项目的Annox插件(根据示例,似乎可以完成这项工作,但是添加一个完整的框架和数百KB的代码只是为了向一堆类添加简单的注释?真的吗?),

I just need to add a few notes: 我只需要添加一些注释:

  • First of all, this is not "Atlassian's" framework/project. 首先,这不是“Atlassian”的框架/项目。 I am neither employee nor affiliated with Atlassian or Sun or Oracle (although I'm an official Sun/Oracle contributor). 我既不是员工,也不是Atlassian,Sun或Oracle的附属机构(尽管我是Sun / Oracle的正式撰稿人)。 This is an indipendent open-source project not driven by any company. 这是一个独立的开源项目,不是由任何公司驱动的。
  • Next, jaxb2-annotate-plugin was moved to a standalone project , it is not longer part of JAXB2 Basics . 接下来, jaxb2-annotate-plugin被移动到一个独立项目 ,它不再是JAXB2 Basics的一部分。 It just got too large and deserved to be a standalone thing. 它太大了,应该是一个独立的东西。
  • Concerning "a whole framework and hundreds of kB of code". 关于“整个框架和数百KB的代码”。
    • jaxb2-annotate-plugin does not require or add ANY runtime dependencies to your code . jaxb2-annotate-plugin不需要或为代码添加任何运行时依赖项 If you annotations have runtime retention, you'll need the classes of your annotations in runtime, but it should be obvious. 如果注释具有运行时保留,则在运行时需要注释的类,但这应该是显而易见的。
    • jaxb2-annotate-plugin does indeed have a number of compile-time dependencies like which is used to parse annotations from Java syntax. jaxb2-annotate-plugin确实有许多编译时依赖,比如 ,它用于解析Java语法中的注释。 Well, this is due to the fact the plugin has to do a lot of moderately advanced stuff like parse annotations from Java syntax and transform them to to add to the generated code. 嗯,这是因为插件必须执行许多适度高级的操作,例如从Java语法解析注释并将它们为代码以添加到生成的代码中。 I personally don't think that a few hundred kBs in the compile time is a problem at all. 我个人认为编译时没有几百个问题就完全没问题了。

So, from my point of view, @ToYonos offered a reasonable workaround for the moment. 所以,从我的观点来看,@ ToYonos提供了一个合理的解决方法


Now, how would a correct fix look like? 现在,正确的修复如何?

  • The correct fix would be to fix XJC. 正确的解决方法是修复XJC。 Everything else is just a workaround. 其他一切只是一种解决方法。
  • There is the JAXB-1053 issue for this problem. 这个问题有JAXB-1053问题。 Iaroslav correctly points out that as long as 1.6 has to be supported, the diamond fix can't be applied. Iaroslav正确地指出,只要必须支持1.6 ,就不能应用钻石修复。
  • The CodeModel will have to be updated as well to support Java 7+ constructs. CodeModel也必须更新以支持Java 7+构造。
  • You could help by forking and providing a PR to XJC code on GitHub . 您可以通过在GitHub上分叉和提供PR到XJC代码来提供帮助。 However I could not say that PRs really do get applied. 但是我不能说PR确实应用了。 There are three of my PRs in the pipeline , for a month or so already. 有三个我的PR 正在筹备中 ,已经有一个月左右了。
  • It is possible to write an XJC plugin which would override code generation. 可以编写一个XJC插件来覆盖代码生成。 This is not easy but possible. 这不容易但可能。

The correct fix does not look like an easy task, I'd opt to a workaround and wait for the vendor fix at the moment. 正确的修复看起来不是一件容易的事,我选择了一种解决方法,等待供应商修复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM