java 如何抑制同一行代码的多个 FindBugs 警告
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10896425/
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
How to suppress multiple FindBugs warnings for the same line of code
提问by IAmYourFaja
I recently discovered FindBugs' @edu.umd.cs.findbugs.annotations.SuppressWarnings
annotation which is pretty cool and allows you to basically tell FindBugs to ignore certain warnings.
我最近发现 FindBugs 的@edu.umd.cs.findbugs.annotations.SuppressWarnings
注释非常酷,它允许您基本上告诉 FindBugs 忽略某些警告。
I've successfully implemented my own SLF4J binding by following their recommendations to take slf4j-simple
and modify it with your own logger and logger factory bindings, and I'm pleased to say it works like a charm.
我已经按照他们的建议成功地实现了我自己的 SLF4J 绑定,slf4j-simple
使用你自己的记录器和记录器工厂绑定来获取和修改它,我很高兴地说它就像一个魅力。
I just ran find bugs on the package that contains this SLF4J binding and it is complaining about a certain line of code written by the original StaticLoggerBinder
author (Ceki Gulku):
我刚刚在包含此 SLF4J 绑定的包上运行 find 错误,它抱怨StaticLoggerBinder
原作者(Ceki Gulku)编写的某行代码:
// to avoid constant folding by the compiler, this field must *not* be final.
publicstatic String REQUESTED_API_VERSION = "1.6"; // !final
FindBugs complains that this field "isn't final but it should be". However, the (very) smart people over at SLF4J already thought of that, and placed the surrounding comments provided above.
FindBugs 抱怨这个字段“不是最终的,但它应该是”。然而,SLF4J 的(非常)聪明的人已经想到了这一点,并在上面提供了周围的评论。
So, just to get FindBugs to shut up, I've modified the code per my usual way of suppressing FB warnings:
所以,为了让 FindBugs 关闭,我按照我通常的抑制 FB 警告的方式修改了代码:
@edu.umd.cs.findbugs.annotations.SuppressWarnings("MS_SHOULD_BE_FINAL")
public static String REQUESTED_API_VERSION = "1.6";
When I clean my project and re-run FindBugs, I get a secondwarning on the same line of code, this time complaining:
当我清理我的项目并重新运行 FindBugs 时,我在同一行代码中收到第二个警告,这次是抱怨:
This field is never read. The field is public or protected, so perhaps it is intended to be used with classes not seen as part of the analysis. If not, consider removing it from the class.
永远不会读取此字段。该字段是公共的或受保护的,因此它可能旨在与不被视为分析一部分的类一起使用。如果没有,请考虑将其从课程中删除。
When I add this second warning suppression:
当我添加第二个警告抑制时:
@edu.umd.cs.findbugs.annotations.SuppressWarnings("MS_SHOULD_BE_FINAL")
@edu.umd.cs.findbugs.annotations.SuppressWarnings("URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
public static String REQUESTED_API_VERSION = "1.6";
I get a compiler/syntax error from Eclipse:
我从 Eclipse 收到编译器/语法错误:
Duplicate annotation @SuppressWarnings.
重复注释@SuppressWarnings。
How can I suppress multiple FindBugs warnings on the same line of code?
如何在同一行代码中抑制多个 FindBugs 警告?
回答by Péter T?r?k
Just list all the warning identifiers in an array, within a single annotation:
只需在单个注释中列出数组中的所有警告标识符:
@edu.umd.cs.findbugs.annotations.SuppressWarnings({
"MS_SHOULD_BE_FINAL",
"URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD"})
public static String REQUESTED_API_VERSION = "1.6";
Just as for the standard java.lang.SuppressWarnings
, the FindBugs version also has a parameter of type String[]
. For a single value, the curly braces can be omitted though to make life easier.
正如标准一样java.lang.SuppressWarnings
,FindBugs 版本也有一个 type 参数String[]
。对于单个值,可以省略大括号以使生活更轻松。
回答by akostadinov
FindBugs 3+ style:
FindBugs 3+ 风格:
@SuppressFBWarnings(
value={"NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE","STCAL_INVOKE_ON_STATIC_DATE_FORMAT_INSTANCE"},
justification="let me just make the build pass")
回答by Eduardo Sanchez-Ros
Try this:
试试这个:
@edu.umd.cs.findbugs.annotations.SuppressWarnings({"MS_SHOULD_BE_FINAL" , "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD"})
public static String REQUESTED_API_VERSION = "1.6";