哪个@NonNull要使用Java注释 [英] Which @NonNull Java annotation to use
问题描述
什么是最佳NonNull注释?
What is the best 'NonNull' annotation?
最佳
- 标准方式例如未来的证明(例如标准jdk等的支持)
- 支持IDE(在java文档中显示用于开发人员的用法)
- 支持通过静态分析工具,如findbugs
- 支持运行时分析
- 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
喜欢 - 任何进一步的洞察力被赞赏:
Here's how the world currently look like - any further insight is appreciated:
-
javax.validation.constraints.NotNull
(文档)
+ javax包因此似乎是未来的
- JEE的一部分不 JSE。在JSE需要导入其他库。
- 静态分析工具不支持(仅限运行时验证)
javax.validation.constraints.NotNull
(Docs)
+ javax package thus seems futureproof
- Part of JEE not JSE. In JSE need to import additional libs.
- Not supported by static analysis tools (runtime validation only)
( docs ) edu.umd.cs.findbugs.annotations.NonNull
- 外部库而不是 javax
包
- 弃用,因为findbugs版本3.X
+用于静态分析(通过findbugs,因此Sonar)
(docs)edu.umd.cs.findbugs.annotations.NonNull
- external library and not an javax
package
- deprecated since findbugs version 3.X
+ used for static analysis (by findbugs and therefore Sonar)
( docs ) javax.annotation.Nonnull
+用于静态分析(在findbug中)
- JSR-305 休眠/死亡/未知 fb mailing list 表示。作者Bill Pugh,即使直接问,几年来还没有评论该州。
(docs)javax.annotation.Nonnull
+ used for static analysis (in findbugs)
- JSR-305 is dormant/dead/unknown as on the fb mailing list indicated. The author Bill Pugh, even if directly asked, hasn't commented the state in years...
( docs ,有趣的是演示文稿) org.eclipse.jdt.annotation_2.0.0
+用于静态分析(在eclipse中不在findbug中)
- eclipse专有(未尝试独立使用)
(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
+用于静态分析(在intelliJ不在findbugs)
- IntelliJ专有(但也可以作为jar公开)
(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 )
+用于控制代码生成
- 专有注释
lombok.NonNull
(docs)
+ used to control code generation
- proprietary annotation
android.support.annotation.NonNull
( docs )
+ android studio中的静态分析
- android特定专有注释
android.support.annotation.NonNull
(docs)
+ static analysis in android studio
- android specific proprietary annotation
org.checkerframework.checker.nullness.qual.NonNull
( docs )
+ JSR308 实现,它是Java8的一部分(它确实介绍了在代码的不同部分编写注释的能力,但没有引入新的注释)
+用于s tatic代码(不是findbugs)和运行时分析
- 外部lib似乎是由Java人士批准
org.checkerframework.checker.nullness.qual.NonNull
(docs)
+ JSR308 implementation 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) and runtime analysis
- external lib however seems to be endorsed by the java folks
目前,我会倾向于检查框架,但我期待其他视图...
Currently I would tend to the Checker Framework but I am looking forward for other views...
[免责声明] 我知道问题已被问到,但是没有回答(或者答案是错误的/不完整的/过时的)
[/免责声明]
[disclaimer] I know the question has been asked here however was not answered (or the answer was wrong/incomplete/outdated) [/disclaimer]
推荐答案
没有标准的 @NonNull
注释。创建这样的注释是JSR 305的目标,JSR 305已经被遗弃了很长时间。直到重建JSR 305才会有标准的 @NonNull
注释。 Oracle目前没有计划这样做。 (JEE注释不在JSR 305的范围内。)
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
表示变量值的属性,而不是变量本身的属性,它应该是一个类型注释。作为类型注释还可以将注释写入更多位置,如 List< @NonNull String>
。
For futureproofing, I think 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>
.
您可以通过查看注释定义中的 @Target
元标注来确定注释是否是注释类型。似乎只有Checker框架和Eclipse版本是注释类型,所以我将选择它们作为声明注释。请注意,任何其他注释的开发人员也可以将其更新为类型注释;我不知道他们的计划。
You can determine whether an annotation is a type annotation by looking at the @Target
meta-annotation on the annotation's definition. It seems that only the Checker Framework and Eclipse versions are type annotation, 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.
唯一的缺点是使用类型注释需要使用Java 8编译器。 Checker框架具有让包含其注释的代码由Java 7编译器编译的机制。
The only downside is that using a type annotation requires use of a Java 8 compiler. The Checker Framework has mechanisms for letting code containing its annotations be compiled by a Java 7 compiler.
这篇关于哪个@NonNull要使用Java注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!