在eclipse中使用jsr305注释Findbugs没有发现bug

发布于 2024-08-02 22:09:47 字数 594 浏览 1 评论 0原文

我一直在尝试将 jsr 305 注释与 Findbugs 一起使用,特别是 @CheckForNull 注释,它可以避免我刚刚发现的向客户报告的错误。我已将 jsr305.jar 和annotations.jar 添加到我的构建路径中,但 findbugs 未发现这些错误。我正在使用 Eclipse 和 Eclipse Findbugs 插件。下面是一些示例代码,它显示了相同的错误,但当我对其运行 findbugs 时却没有找到该错误。我已经在 Eclipse Galileo 和 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");
    }
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

被你宠の有点坏 2024-08-09 22:09:47

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

您可以考虑从命令行运行 FindBugs 以消除任何 Eclipse 问题并确保 FindBugs 本身正确运行。了解如何在独立模式下运行 FindBugs 将为您在 IDE 配置不正确时提供后备方案。

我将源代码保存在名为 FindBugsAnnotationsTest.java 的文件中,添加了 ListArrayListCheckForNull 的导入,编译并运行 FindBugs 1.3.9。 FindBugs 生成多个有关空值的警告:

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

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

import java.util.ArrayList;
import java.util.List;
import 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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文