要使用哪个 @NonNull Java 注释 [英] Which @NonNull Java annotation to use

查看:40
本文介绍了要使用哪个 @NonNull Java 注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是最佳NonNull"注释?

What is the best 'NonNull' annotation?

最佳"

  • 标准方式,例如面向未来(例如标准 jdk 等的支持)
  • 对 IDE 的支持(显示在 Java 文档中以指示开发人员的用法)
  • 由 findbugs 等静态分析工具提供支持
  • 支持运行时分析

以下是当前世界的样子 - 感谢您提供进一步的见解:

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)

edu.umd.cs.findbugs.annotations.NonNull (docs)
- 外部库而不是 javax
- 已弃用,因为 findbugs 版本 3.X
+ 用于静态分析(通过 findbugs 和 Sonar)

edu.umd.cs.findbugs.annotations.NonNull (docs)
- external library and not an javax package
- deprecated since findbugs version 3.X
+ used for static analysis (by findbugs and therefore Sonar)

javax.annotation.Nonnull (文档)
+ 用于静态分析(在 findbugs 中)
- JSR-305fb 邮件列表.作者Bill Pugh,就算直接问了,也好多年没有评论状态了……

javax.annotation.Nonnull (docs)
+ 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...

org.eclipse.jdt.annotation_2.0.0(docs,有趣的演示)
+ 用于静态分析(虽然在 Eclipse 中而不是在 findbugs 中)
- Eclipse 专有(没有尝试独立使用它们)

org.eclipse.jdt.annotation_2.0.0 (docs, interesting presentation)
+ used for static analysis (in eclipse not in findbugs though)
- proprietary to eclipse (didn't try to use them standalone)

org.jetbrains.annotations.NotNull (文档)
+ 用于静态分析(在 intelliJ 中而不是在 findbugs 中)
- IntelliJ 专有(但也可作为 jar 公开使用)

org.jetbrains.annotations.NotNull (docs)
+ 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 (文档)
+ android studio 中的静态分析
- android特定的专有注释

android.support.annotation.NonNull (docs)
+ static analysis in android studio
- android specific proprietary annotation

org.checkerframework.checker.nullness.qual.NonNull (文档)
+ JSR308 实现,它是 Java8 的一部分(它确实引入了在代码的不同部分编写注释的能力,但没有引入新的注解)
+ 用于静态代码(虽然不是 findbugs)运行时分析
- 然而,外部库似乎被认可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

目前我倾向于使用 Checker 框架,但我期待其他观点......

Currently I would tend to the Checker Framework but I am looking forward for other views...

[disclaimer] 我知道这里有人问过这个问题但是没有回答(或者答案错误/不完整/过时)[/disclaimer]

[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 之前,不会有标准的 @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, 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 Framework 和 Eclipse 版本是类型注释,所以我会选择它们而不是声明注释.请注意,任何其他注释的开发人员也可以将它们更新为类型注释;我不知道他们的计划.

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.

唯一的缺点是使用类型注释需要使用 Java 8 编译器.Checker Framework 具有让 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆