实现compareObjects()的正确方法是什么
我的compareObjects方法实现如下
public static int compareObjects(Comparable a, Comparable b){
if (a == null && b == null){
return 0;
} else if (a == null && b != null){
return -1;
} else if (a != null && b == null){
return 1;
} else {
return a.compareTo(b);
}
}
当我通过findBugs运行这个方法时,我在return a.compareTo(b)
行上得到了这个建议:
有一个语句分支,如果执行,将保证取消引用 null 值,这会在执行代码时生成 NullPointerException。当然,问题可能是分支或语句不可行,空指针异常永远无法执行;决定这超出了 FindBugs 的能力。由于该值之前已被测试为空,因此这是肯定的可能性。
此时a
永远不可能为空。为什么 FindBugs 向我显示此建议?我该如何纠正这个问题?实现compareObjects()的正确方法是什么?
I have compareObjects method implemented as below
public static int compareObjects(Comparable a, Comparable b){
if (a == null && b == null){
return 0;
} else if (a == null && b != null){
return -1;
} else if (a != null && b == null){
return 1;
} else {
return a.compareTo(b);
}
}
When I run this through findBugs, I get this suggestion on the line return a.compareTo(b)
:
There is a branch of statement that, if executed, guarantees that a null value will be dereferenced, which would generate a NullPointerException when the code is executed. Of course, the problem might be that the branch or statement is infeasible and that the null pointer exception can't ever be executed; deciding that is beyond the ability of FindBugs. Due to the fact that this value had been previously tested for nullness, this is a definite possibility.
At this point a
can never be null. Why does FindBugs show me this suggestion? How can I correct this; what is the correct way to implement compareObjects()
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为这可能是因为你不需要额外的 &&声明。在第一个 if 语句之后,您已经知道其中一个为空。
I think it might be because you don't need the extra && statements. After the first if statement you already know that one of them is null.
再看一遍,试试这段代码:
Looking at it again , try this code:
这可能是 FindBugs 的一个限制;我同意您已经涵盖了所有基础,但是您的空检查分为两个不同的条件。现在这些条件恰好是互补的,因此如果
a
为 null,则至少其中一个条件会触发,但根据 FindBugs 的复杂程度,它可能无法识别这一点。那么这里有两个选择:
忽略 FindBugs 警告。由于其本质,它会时不时地引发一些误报,因此,如果您认为重写不值得,则不必重写代码才能使其 100% 满意。就其本身的优点而言。
您可以使用
@SuppressWarnings如果您希望报告在末尾显示一个漂亮的大零,则可以使用
注释 将其实际传达给 FindBugs。有关示例,请参阅此问题。< /p>通过嵌套
if
块重构条件,以便对a
进行无效检查更加明确:根据您的品味和风格,无论如何,这可能是更好的重写,因为它更清楚地说“如果
a
为空,则进行这个计算并返回它,否则进行此计算”。当然,如果您愿意,可以将三元条件更改为另一个if-else
块。It may be a limitation in FindBugs; I agree that you've covered all the bases, but your null-check is split across two different conditions. Now these conditions happen to be complementary, so at least one of them will fire if
a
is null, but depending how sophisticated FindBugs is, it may not recognise this.Two options here, then:
Just ignore the FindBugs warning. Due to its nature it will raise some false positives from time to time, so don't feel like you have to rewrite your code to make it 100% happy if you don't think the rewrite is worthwhile on its own merits.
You can use the
@SuppressWarnings
annotation to actually communicate this to FindBugs, if you want the report to show a nice big zero at the end. See this question for an example.Restructure the condition so that the nullity check on
a
is more explicit, by nestingif
blocks:Depending on your tastes and style that might be a better rewrite anyway, in that is more clearly says "if
a
is null, do this calculation and return it, otherwise do this calculation". You can of course change the ternary condition into anotherif-else
block if you prefer that.