Findbugs在eclipse中使用jsr305注解找不到bug [英] Findbugs using jsr305 annotations in eclipse is not finding bugs

查看:28
本文介绍了Findbugs在eclipse中使用jsr305注解找不到bug的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试验与 Findbugs 一起使用的 jsr 305 批注,特别是 @CheckForNull 批注,它可以避免我刚刚发现的错误,并将其提供给客户.我已将 jsr305.jar 和 annotations.jar 添加到我的构建路径中,但 findbugs 未找到这些错误.我将 Eclipse 与 Eclipse Findbugs 插件一起使用.下面是一些示例代码,它显示了相同的错误,但当我在它上面运行 findbugs 时没有找到错误.我已经在 Eclipse Galileo 和 Ganymede 中尝试过这个.

公共类 FindBugsAnnotationsTest {ArrayList可以为空;@CheckForNull公共列表<字符串>getCanBeNull() {返回 canBeNull;}public void shouldGetFindbugsWarning() {canBeNull.add("一个字符串");getCanBeNull().add("一个字符串");}}

解决方案

这可能是显而易见的,但我认为您的问题与 Eclipse(尤其是 FindBugs 插件)有关,而不是 FindBugs 本身.

您可以考虑从命令行运行 FindBugs 以消除任何 Eclipse 问题并确保您自己的 FindBugs 正确运行.了解如何在独立模式下运行 FindBugs 将在您的 IDE 配置不正确时提供回退.

我将您的源代码保存在一个名为 FindBugsAnnotationsTest.java 的文件中,为 ListArrayListCheckForNull<添加了导入/code>,编译并运行 FindBugs 1.3.9.FindBugs 生成几个关于空值的警告:

<前>M D NP:在 FindBugsAnnotationsTest.shouldGetFindbugsWarning() 中可能取消引用空指针,原因是在 FindBugsAnnotationsTest.java 处取消引用的调用方法的返回值:[第 18 行]M C UwF:未写字段:FindBugsAnnotationsTest.canBeNull At FindBugsAnnotationsTest.java:[line 12]M C NP:在 FindBugsAnnotationsTest.shouldGetFindbugsWarning() At FindBugsAnnotationsTest.java:[line 16] 中读取未写字段 canBeNull产生的警告:3

这些是我添加到 FidBugsAnnotationsTest.java 顶部的导入:

import java.util.ArrayList;导入 java.util.List;导入 edu.umd.cs.findbugs.annotations.CheckForNull;

命令:

javac -d .-classpath ${FINDBUGS_HOME}/lib/findbugs.jar FindBugsAnnotationsTest.java${FINDBUGS_HOME}/bin/findbugs FindBugsAnnotationsTest.class

其中 ${FINDBUGS_HOME} 是 Findbugs 1.3.9 的安装目录.javac 假定在路径上.

注意:我使用了 findbugs.jar 而不是 annotations.jarjsr305.jar 但我用这个命令得到了相同的结果:

javac -d .-classpath ${FINDBUGS_HOME}/lib/annotations.jar:${FINDBUGS_HOME}/lib/jsr305.jar FindBugsAnnotationsTest.java

I've been experimenting with the jsr 305 annotations for use with Findbugs, specifically the @CheckForNull annotation which would have avoided a bug I just found making it out to customers. I've added jsr305.jar and annotations.jar to my build path but the bugs aren't found by findbugs. I'm using Eclipse with the Eclipse Findbugs plugin. Below is some sample code which shows the same bug but doesn't when I run findbugs over it find the bug. I have tried this in Eclipse Galileo and Ganymede.

public class FindBugsAnnotationsTest {

    ArrayList<String> canBeNull;

    @CheckForNull
    public List<String> getCanBeNull() {
        return canBeNull;
    }

    public void shouldGetFindbugsWarning() {
    canBeNull.add("a string");

        getCanBeNull().add("a string");
    }
}

解决方案

This may be obvious, but I think your issues are with Eclipse (perhaps the FindBugs plugin in particular), not FindBugs itself.

You might consider running FindBugs from the command line to eliminate any Eclipse issues and ensure that you have FindBugs running correctly in its own. Knowing how to run FindBugs in a standalone mode will give you a fallback when your IDE is not configured properly.

I saved your source code in a file named FindBugsAnnotationsTest.java, added imports for List, ArrayList, and CheckForNull, compiled, and ran FindBugs 1.3.9. FindBugs generates several warnings about null values:

M D NP: Possible null pointer dereference in FindBugsAnnotationsTest.shouldGetFindbugsWarning() due to return value of called method  Dereferenced at FindBugsAnnotationsTest.java:[line 18]
M C UwF: Unwritten field: FindBugsAnnotationsTest.canBeNull  At FindBugsAnnotationsTest.java:[line 12]
M C NP: Read of unwritten field canBeNull in FindBugsAnnotationsTest.shouldGetFindbugsWarning()  At FindBugsAnnotationsTest.java:[line 16]
Warnings generated: 3

These are the imports I added to the top of FindBugsAnnotationsTest.java:

import java.util.ArrayList;
import java.util.List;
import edu.umd.cs.findbugs.annotations.CheckForNull;

Commands:

javac -d . -classpath ${FINDBUGS_HOME}/lib/findbugs.jar FindBugsAnnotationsTest.java
${FINDBUGS_HOME}/bin/findbugs FindBugsAnnotationsTest.class

Where ${FINDBUGS_HOME} is the directory in which Findbugs 1.3.9 is installed. javac is assumed to be on the path.

Note: I used the findbugs.jar instead of annotations.jar and jsr305.jar but I get the same results with this command:

javac -d . -classpath ${FINDBUGS_HOME}/lib/annotations.jar:${FINDBUGS_HOME}/lib/jsr305.jar FindBugsAnnotationsTest.java

这篇关于Findbugs在eclipse中使用jsr305注解找不到bug的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆