本文介绍了使用JSR305注解在Eclipse FindBugs的是不是查找错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试与JSR 305注解与FindBugs的使用,特别是@CheckForNull注释这本来可以避免我刚刚发现使得它给客户的错误。我已经添加jsr305.jar和annotations.jar到我的构建路径,但错误没有被发现的FindBugs。我使用Eclipse和Eclipse插件FindBugs的。下面是一些示例code这显示了相同的错误,但不会当我运行了它的FindBugs发现的bug。我已经在Eclipse Galileo和木卫三试过这个。

 公共类FindBugsAnnotationsTest {    ArrayList的<串GT; canBeNull;    @CheckForNull
    公开名单<串GT; getCanBeNull(){
        返回canBeNull;
    }    公共无效shouldGetFindbugsWarning(){
    canBeNull.add(字符串);        。getCanBeNull()加(字符串);
    }
}


解决方案

这可能是显而易见的,但我觉得你的问题是与Eclipse(也许是FindBugs的插件,尤其是),不FindBugs的本身。

您可能会考虑在命令行中运行FindBugs的,以消除任何Eclipse的问题,并确保你已经在自己的正确运行FindBugs的。知道如何在单机模式下运行FindBugs的会给你一个后备,当您的IDE配置不正确。

我救了你的源$ C ​​$ C在一个名为 FindBugsAnnotationsTest.java ,增加了列表进口, 的ArrayList CheckForNull ,编译并运行FindBugs的1.3.9。 FindBugs的生成有关空值几个警告:


个月天NP:在FindBugsAnnotationsTest.shouldGetFindbugsWarning可能的空指针引用()到期归还在FindBugsAnnotationsTest.java:[line 18解除引用调用方法的价值]
M C UWF:不成文领域:FindBugsAnnotationsTest.canBeNull在FindBugsAnnotationsTest.java:[line 12]
M C NP:读不成文场canBeNull的FindBugsAnnotationsTest.shouldGetFindbugsWarning()在FindBugsAnnotationsTest.java:[line 16]
生成的警告:3

这是我加入到顶部的进口 FindBugsAnnotationsTest.java

 进口的java.util.ArrayList;
进口的java.util.List;
进口edu.umd.cs.findbugs.annotations.CheckForNull;

命令:

 的javac -d。 -classpath $ {} FINDBUGS_HOME /lib/findbugs.jar FindBugsAnnotationsTest.java
$ {} FINDBUGS_HOME /斌/ FindBugs的FindBugsAnnotationsTest.class

其中, $ {FINDBUGS_HOME} 是在其中安装FindBugs的1.3.9的目录。 的javac 被假定为路径上。

请注意:我使用了 findbugs.jar 而不是 annotations.jar JSR305的.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

这篇关于使用JSR305注解在Eclipse FindBugs的是不是查找错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 15:57