【问题标题】:Ignoring single finding of FindBugs忽略 FindBugs 的单一发现
【发布时间】:2013-05-18 18:13:15
【问题描述】:

我正在运行带有 Findbugs 插件的 Eclipse Java EE juno SR2 win32。

以下代码示例让 FindBugs 显示发现:

catch (OutOfMemoryError e) {
     tryAgain = true;
     log.error("try to recover", e);
     System.gc(); // not so happy about this
}

这是说明

Bug: [..] forces garbage collection; extremely dubious except in benchmarking code

Code explicitly invokes garbage collection. Except for specific use in benchmarking, this is very dubious.

In the past, situations where people have explicitly invoked the garbage collector in routines such as close or 
finalize methods has led to huge performance black holes. Garbage collection can be expensive. Any situation that forces 
hundreds or thousands of garbage collections will bring the machine to a crawl.

Confidence: High, Rank: Of Concern (16)
Pattern: DM_GC 
Type: Dm, Category: PERFORMANCE (Performance)

那么我怎样才能将此行注释为不被检查?

我试过了 (source)

@edu.umd.cs.findbugs.annotations.SuppressWarnings("DM_GC")
System.gc();

这不起作用,所以我将其移至方法

@edu.umd.cs.findbugs.annotations.SuppressWarnings("DM_GC")
private bla(){

这就是说:“edu 无法解析为一种类型” 所以我尝试了:

@SuppressWarnings("DM_GC")
private bla(){

我得到一个“不支持的@SuppressWarnings("DM_GC")”

这不是关于解决 findbugs 错误的请求,而是关于如何禁用特定错误的请求。

谢谢!

仅供参考:我知道调用 gc 并不是一个好主意。

【问题讨论】:

    标签: eclipse findbugs


    【解决方案1】:

    “edu 无法解析为类型”

    这是因为您尚未将 FindBugs jar 添加到您的项目中。如果您使用的是 Maven,请将此依赖项添加到 pom.xml:

    <dependency>
        <groupId>com.google.code.findbugs</groupId>
        <artifactId>annotations</artifactId>
        <version>2.0.0</version>
    </dependency>
    

    如果您不使用 Maven,请将 jar manually 添加到您的类路径中。然后会找到 FindBugs 注解SuppressWarnings,你应该不会再看到问题了。

    【讨论】:

    • 而你使用SuppressFBWarnings代替,你可以在顶部导入它。
    • 这解决了问题,现在 "@edu.umd.cs.findbugs.annotations.SuppressWarnings("DM_GC")" 工作。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-22
    • 2014-10-16
    • 1970-01-01
    • 1970-01-01
    • 2011-08-19
    • 2010-10-19
    相关资源
    最近更新 更多