在eclipse中使用jsr305注释Findbugs没有发现bug
我一直在尝试将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这可能是显而易见的,但我认为您的问题在于 Eclipse(尤其是 FindBugs 插件),而不是 FindBugs 本身。
您可以考虑从命令行运行 FindBugs 以消除任何 Eclipse 问题并确保 FindBugs 本身正确运行。了解如何在独立模式下运行 FindBugs 将为您在 IDE 配置不正确时提供后备方案。
我将源代码保存在名为
FindBugsAnnotationsTest.java
的文件中,添加了List
、ArrayList
和CheckForNull
的导入,编译并运行 FindBugs 1.3.9。 FindBugs 生成多个有关空值的警告:这些是我添加到
FindBugsAnnotationsTest.java
顶部的导入:命令:
其中
${FINDBUGS_HOME}
是 Findbugs 1.3 所在的目录.9 已安装。假定javac
位于路径上。注意:我使用了
findbugs.jar
而不是annotations.jar
和jsr305.jar
,但使用此命令得到了相同的结果: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 forList
,ArrayList
, andCheckForNull
, compiled, and ran FindBugs 1.3.9. FindBugs generates several warnings about null values:These are the imports I added to the top of
FindBugsAnnotationsTest.java
:Commands:
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 ofannotations.jar
andjsr305.jar
but I get the same results with this command: