Findbugs using jsr305 annotations in eclipse is not finding bugs

谁都会走 提交于 2019-12-12 15:08:58

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/1321873/findbugs-using-jsr305-annotations-in-eclipse-is-not-finding-bugs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!