eclipse 使用哪个@NonNull Java 注释
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35892063/
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
Which @NonNull Java annotation to use
提问by Lonzak
What is the best'NonNull' annotation?
什么是最好的“NonNull”注释?
"Best"in the sense of
意义上的“最好”
- Standard way e.g. future proofness (e.g. support by standard jdk etc.)
- Support of IDE's (shows up in java doc to indicate the usage for developers)
- Support by static analysis tools like findbugs
- Support for runtime analysis
- 标准方式,例如面向未来(例如,标准 jdk 等的支持)
- IDE 的支持(显示在 java 文档中以指示开发人员的用法)
- 静态分析工具如 findbugs 的支持
- 支持运行时分析
Here's how the world currently look like - any further insight is appreciated:
以下是世界目前的样子 - 任何进一步的见解都值得赞赏:
javax.validation.constraints.NotNull
(Docs)
+ javax package thus seems futureproof
- Part of JEE notJSE. In JSE need to import additional libs.
- Not supported by static analysis tools (runtime validation only)(docs)edu.umd.cs.findbugs.annotations.NonNull
- external library and not anjavax
package
- deprecatedsince findbugs version 3.X
+ used for static analysis (by findbugs and therefore Sonar)(docs)javax.annotation.Nonnull
+ used for static analysis (in findbugs)
- JSR-305is dormant/dead/unknown as on the fb mailing listindicated. The author Bill Pugh, even if directly asked, hasn't commented the state in years...(docs, interesting presentation)org.eclipse.jdt.annotation_2.0.0
+ used for static analysis (in eclipse not in findbugs though)
- proprietary to eclipse (didn't try to use them standalone)(docs)org.jetbrains.annotations.NotNull
+ used for static analysis (in intelliJ not in findbugs though)
- proprietary to IntelliJ (but also publicly available as a jar)lombok.NonNull
(docs)
+ used to control code generation
- proprietary annotationandroid.support.annotation.NonNull
(docs)
+ static analysis in android studio
- android specific proprietary annotationorg.checkerframework.checker.nullness.qual.NonNull
(docs)
+ JSR308implementation which is part of Java8 (which did introduce the ability to write annotations in different parts of your code, but did not introduce new annotations)
+ used for static code (not findbugs though) andruntime analysis
- external lib however seems to be endorsedby the java folks
javax.validation.constraints.NotNull
( Docs)
+ javax 包因此似乎是面向未来的
- JEE 的一部分而不是JSE。在 JSE 中需要导入额外的库。
- 不受静态分析工具支持(仅限运行时验证)( docs)edu.umd.cs.findbugs.annotations.NonNull
- 外部库而不是javax
包
-自 findbugs 版本 3.X + 用于静态分析以来已弃用
(由 findbugs 和 Sonar)(文档)javax.annotation.Nonnull
+ 用于静态分析(在 findbugs 中)
- JSR-305处于休眠/死亡/未知状态,如fb 邮件列表所示。作者比尔·普格,即使直接问,多年来也没有评论过这个国家......(文档,有趣的演示)org.eclipse.jdt.annotation_2.0.0
+ 用于静态分析(虽然在 eclipse 中而不是在 findbugs 中)
- eclipse 专有(没有尝试独立使用它们)(文档)org.jetbrains.annotations.NotNull
+ 用于静态分析(尽管在 intelliJ 中而不是在 findbugs 中)
- IntelliJ 专有(但也可作为 jar 公开获得)lombok.NonNull
( docs)
+ 用于控制代码生成
- 专有注释android.support.annotation.NonNull
(文档)
+ android studio 中的静态分析
- android 特定的专有注释org.checkerframework.checker.nullness.qual.NonNull
(文档)
+ JSR308实现,它是 Java8 的一部分(它确实引入了在代码的不同部分编写注释的能力,但没有引入新的注释)
+ 用于静态代码(虽然不是 findbugs)和运行时分析
- 外部库然而似乎得到了java人的认可
Currently I would tend to the Checker Frameworkbut I am looking forward for other views...
目前我倾向于使用Checker 框架,但我期待其他观点......
[disclaimer]I know the question has been asked here however was not answered (or the answer was wrong/incomplete/outdated) [/disclaimer]
[免责声明]我知道这里有人问过这个问题,但没有回答(或答案错误/不完整/过时) [/免责声明]
回答by mernst
There is no standard @NonNull
annotation. Creating such an annotation was the goal of JSR 305, which has been abandoned for a long time. There will not be a standard @NonNull
annotation until JSR 305 is reconstituted. Oracle has no current plans to do so. (JEE annotations are outside the scope of JSR 305.)
没有标准@NonNull
注释。创建这样的注解是 JSR 305 的目标,它已经被废弃了很长时间。在@NonNull
重新构建 JSR 305 之前,不会有标准注释。Oracle 目前没有这样做的计划。(JEE 注释超出了 JSR 305 的范围。)
For futureproofing, the most important factor to consider is whether the annotation is a type annotation or a declaration annotation. Because @NonNull
states a property of the variable's value rather than of the variable itself, it should be a type annotation. Being a type annotation also lets the annotation be written on more locations, as in List<@NonNull String>
.
对于未来证明,要考虑的最重要因素是注解是类型注解还是声明注解。因为@NonNull
声明了变量值的属性而不是变量本身的属性,所以它应该是一个类型注释。作为类型注释还可以让注释写在更多位置,如List<@NonNull String>
.
You can determine whether an annotation is a type annotation by looking at the @Target
meta-annotation on the annotation's definition. As of this writing, it seems that only the Checker Framework and Eclipse versions are type annotations, so I would choose them over the ones that are declaration annotations. Note that the developers of any of the other annotations could update them to be type annotations as well; I don't know of their plans.
您可以通过查看@Target
注释定义上的元注释来确定注释是否是类型注释。在撰写本文时,似乎只有 Checker Framework 和 Eclipse 版本是类型注释,所以我会选择它们而不是声明注释。请注意,任何其他注释的开发人员也可以将它们更新为类型注释;我不知道他们的计划。
The only downside is that using a type annotation requires use of a Java 8 compiler. The Checker Frameworkhas mechanisms for letting code containing its annotations be compiled by a Java 7 compiler.
唯一的缺点是使用类型注释需要使用 Java 8 编译器。该检查框架有让含有其标注的代码由Java 7编译器编译机制。