简体   繁体   English

如何针对同一行代码抑制多个FindBugs警告

[英]How to suppress multiple FindBugs warnings for the same line of code

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绑定的包上运行查找错误,并且它抱怨原始StaticLoggerBinder作者(Ceki StaticLoggerBinder )编写的某行代码:

// 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 ". FindBugs抱怨说这个字段“ 不是最终的,但应该是 ”。 However, the (very) smart people over at SLF4J already thought of that, and placed the surrounding comments provided above. 然而,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 second warning 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警告?

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[] . 与标准java.lang.SuppressWarnings ,FindBugs版本也有一个String[]类型的参数 For a single value, the curly braces can be omitted though to make life easier. 对于单个值,可以省略花括号,以使生活更轻松。

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")

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";

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

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