java javax.annotation.Nonnull 与断言
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14810030/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
javax.annotation.Nonnull vs assert
提问by Gualtiero Testa
I'm using Findbugs and javax.annotation.Nonnull on method parameters.
我在方法参数上使用 Findbugs 和 javax.annotation.Nonnull。
On private methods I usually add an assert line to check for nullness like
在私有方法上,我通常添加一个断言行来检查空值,例如
private void myMethod(@Nonnull String str) {
assert str != null
....
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.
最新的 Netbeans 版本 (7.3rc2) 报告说不需要断言检查(因为 Nonnull 注释)。我不完全确定这是否是 Netbeans 错误。
Can the assert line be removed because I specified the @Nonnull annotation ?
可以删除断言行吗,因为我指定了 @Nonnull 注释?
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.
据我了解,注释仅在静态分析期间使用,而断言在启用时在执行期间处于活动状态,因此两者不可替代。
回答by Christophe Roussy
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.
断言在运行时进行评估,注释帮助 FindBugs 在运行前分析期间捕获问题。由于这两项检查并没有真正发生冲突,因此您可以同时保留它们。如果我的 IDE 告诉我删除断言,我会觉得很烦人。
回答by David Lavender
Netbeans is right. If you think it can be null: remove the annotation. If you know it can't: remove the assert.
Netbeans 是对的。如果您认为它可以为空:删除注释。如果你知道它不能:删除断言。
If there's ANY chance that your method could be called with a null value, then @Nonnull
annotation shouldn't be there.
如果您的方法有可能被空值调用,那么@Nonnull
注释不应该在那里。
Like you said, that annotation doesn't actually do anything at runtime: it is only used by IDEs and static code analysis tools. It doesn't ensure that things aren't null.
就像您说的那样,该注释在运行时实际上并没有做任何事情:它仅由 IDE 和静态代码分析工具使用。它不能确保事物不为空。
回答by Kengo TODA
Since this is private method, we can ensure that annotated parameter cannot be null. I think you can remove this assertion.
由于这是私有方法,我们可以确保带注释的参数不能为空。我认为你可以删除这个断言。
If NetBeans warns to public method, I think it has problem. I recommend you to put assertion.
如果 NetBeans 警告公共方法,我认为它有问题。我建议你把断言。
If you still feel that assertion in private method is necessary, I think you can use bytecode injection. For instance, here is a maven plugin to inject null check. Sorry this is my personal project, but it works to me. I guess it can suit your need. https://github.com/KengoTODA/jsr305-maven-plugin
如果您仍然觉得需要在私有方法中进行断言,我认为您可以使用字节码注入。例如,这是一个注入空检查的 Maven 插件。抱歉,这是我的个人项目,但它对我有用。我想它可以满足您的需求。 https://github.com/KengoTODA/jsr305-maven-plugin
回答by John Foley
I found a different solution, as I was thinking about my IDE warnings.
我找到了一个不同的解决方案,因为我正在考虑我的 IDE 警告。
Initially, I felt that the IDE was wrong. I'm a paranoid programmer, and want to have the label for documentation & static analysis ANDa runtime check in case I ever use it from reflection, or another JVM language or something that isn't statically analyzable, so I thought it was wrong to give me a warning and tell me the assert(x != null)
statement wasn't needed.
一开始,我觉得IDE不对。我是一个偏执的程序员,并希望有文档和静态分析标签和的情况下,运行时检查我曾经使用过的反射,或其他JVM语言或东西是不是静态分析的,所以我认为这是错误的给我一个警告并告诉我assert(x != null)
不需要该声明。
But then I thought about how asserts can be removed depending on the status of the -ea
flag passed to Java at Runtime, and that in some ways assert
and @Nonnull
are really both development-only checks.
但是后来我想到了如何根据-ea
在运行时传递给 Java的标志的状态来删除断言,并且在某些方面assert
,@Nonnull
这实际上都是仅用于开发的检查。
Turns out, there's an actual runtime check that can be inserted (Java 7+) Objects.requireNonNull
which will throw a NullPointerException
and cannot be removed with an -ea
assertion. I think I'm going to prefer this to my assert(x != null); use(x);
pattern.
事实证明,有一个可以插入的实际运行时检查(Java 7+)Objects.requireNonNull
,它会抛出 aNullPointerException
并且不能用-ea
断言删除。我想我会更喜欢这个而不是我的assert(x != null); use(x);
模式。
public ConstructorForClass(@Nonnull Type x) {
this.x = Objects.requireNonNull(x);
//...
}