javax.annotation.Nonnull vs assert [英] javax.annotation.Nonnull vs assert
问题描述
我在方法参数上使用Findbugs和javax.annotation.Nonnull。
I'm using Findbugs and javax.annotation.Nonnull on method parameters.
在私有方法上,我通常添加一个断言行来检查null,如
On private methods I usually add an assert line to check for nullness like
private void myMethod(@Nonnull String str) {
assert str != null
....
最新的Netbeans版本(7.3rc2)报告不需要断言检查(因为Nonnull注释) 。我不完全确定这是一个Netbeans错误。
Latest Netbeans version (7.3rc2) is reporting that the assert check is not necessary (because of the Nonnull annotation). I'm not fully sure this is a Netbeans bug or not.
断言行是否可以删除,因为我指定了@Nonnull注释?
Can the assert line be removed because I specified the @Nonnull annotation ?
据我所知,注释仅在静态分析期间使用,而assert在启用时在执行期间处于活动状态,因此两个不是替代。
As far as I understand, the annotation is used only during static analysis while assert is, when enabled, active during execution so the twos are not alternative.
推荐答案
在运行时评估断言,该注释有助于FindBugs在运行时分析期间捕获问题。由于两个检查都不是真正冲突,你可以保持两者。如果我的IDE告诉我删除断言,我会觉得很烦人。
The assert is evaluated at runtime, the annotation helps FindBugs catch problems during the analysis before runtime. As both checks are not really conflicting you could keep them both. I would find it annoying if my IDE told me to remove the assert.
这篇关于javax.annotation.Nonnull vs assert的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!