[英]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: 从我在旅行中看到的情况来看,人们会做以下事情之一:
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. 我想从这个问题中解决以下几点。
- 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: 我只需要添加一些注释:
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. 它太大了,应该是一个独立的东西。 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 javaparser which is used to parse annotations from Java syntax. jaxb2-annotate-plugin
确实有许多编译时依赖,比如javaparser ,它用于解析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 codemodel 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? 现在,正确的修复如何?
1.6
has to be supported, the diamond fix can't be applied. Iaroslav正确地指出,只要必须支持1.6
,就不能应用钻石修复。 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.