如何将 @CheckForNull 等与 Findbugs 一起使用?
当我通过 Findbugs 运行此命令时,我收到警告:
static @NonNull Object foo(@CheckForNull Object arg) {
if (arg == null) { // warning on this line
throw new NullPointerException();
}
return "something";
}
警告的详细信息如下:
错误:
arg
必须为非空,但被标记为可为空
模式 ID:NP_PARAMETER_MUST_BE_NONNULL_BUT_MARKED_AS_NULLABLE
,类型:NP
,类别:STYLE
此参数的使用方式始终要求其为非空,但该参数被显式注释为
可为空
。要么是参数的使用错误,要么是注释错误。
有人可以解释一下 Findbugs 在这里抱怨什么吗?
请注意,我使用的是 edu.umd.cs.findbugs.annotations.*
成员,而不是 javax.annotations.*
。 (有区别吗?)
设置的是 FindBugs 插件 1.3.9.2009- for Eclipse 3.6.1。
Matthew Flaschen 建议我使用 @NonNull
代替,但现在我遇到了这个问题:
static void blah(@NonNull Object arg) {
if (arg == null) {
throw new NullPointerException();
}
System.out.println(arg);
}
static @CheckForNull Object bleh() {
return null;
}
//...
blah(bleh()); // warning here!
警告的详细信息是:
错误:由于被调用方法的返回值可能导致空指针取消引用
模式 ID:NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE
,类型:NP
,类别:STYLE
方法的返回值在没有
null
检查的情况下被取消引用,并且该方法的返回值通常应该检查null
。执行代码时,这可能会导致NullPointerException
。
我基本上希望 blah
满足 @CheckForNull
要求,但如果我将其 arg 设为 @NonNull
,我就无法做到这一点。我该如何让它发挥作用?
When I run this through Findbugs, I get a warning:
static @NonNull Object foo(@CheckForNull Object arg) {
if (arg == null) { // warning on this line
throw new NullPointerException();
}
return "something";
}
The details of the warning is the following:
Bug:
arg
must be nonnull but is marked as nullable
Pattern id:NP_PARAMETER_MUST_BE_NONNULL_BUT_MARKED_AS_NULLABLE
, type:NP
, category:STYLE
This parameter is always used in a way that requires it to be nonnull, but the parameter is explicitly annotated as being
Nullable
. Either the use of the parameter or the annotation is wrong.
Can someone explain what Findbugs is complaining about here?
Note that I'm using the edu.umd.cs.findbugs.annotations.*
members, not the javax.annotations.*
. (Is there a difference?)
Set up is FindBugs plug-in 1.3.9.2009- for Eclipse 3.6.1.
Matthew Flaschen suggested that I use @NonNull
instead, but now I ran into this problem:
static void blah(@NonNull Object arg) {
if (arg == null) {
throw new NullPointerException();
}
System.out.println(arg);
}
static @CheckForNull Object bleh() {
return null;
}
//...
blah(bleh()); // warning here!
The details of the warning is:
Bug: Possible null pointer dereference due to return value of called method
Pattern id:NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE
, type:NP
, category:STYLE
The return value from a method is dereferenced without a
null
check, and the return value of that method is one that should generally be checked fornull
. This may lead to aNullPointerException
when the code is executed.
I basically want blah
to satisfy the @CheckForNull
requirement, but I can't do it if I make its arg be @NonNull
. How do I get this to work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你这是自相矛盾。
CheckForNull
表示,“带注释的元素可能为空”,但如果是,则立即抛出。如果调用者永远不能接受传递 null,我相信你应该对其进行注释:
You're contradicting yourself.
CheckForNull
means, "The annotated element might be null", but if it is you immediately throw.If it's never acceptable for a caller to pass null, I believe you should instead annotate it: