有没有办法让空变量在我的代码库中自动快速失败,除非它们被标记为“@Nullable”?

Is there a way I can make null variables automatically fail fast in my code base unless they're marked with `@Nullable`?

偶然看到这篇文章:https://github.com/google/guice/wiki/UseNullable

To eliminate NullPointerExceptions in your codebase, you must be disciplined about null references. We've been successful at this by following and enforcing a simple rule: Every parameter is non-null unless explicitly specified.

这是通过剪切规则完成的,还是以某种方式自动完成的?可惜,好像是前者?有没有一种方法可以使空变量 自动 在我的代码库中快速失败,除非它们被标记为 @Nullable?

它是自动完成的,并非严格遵守纪律。

正如 Louis Wasserman 所指出的,Google 的测试代码对标记为 @Nullable.

的参数进行了特殊处理

在测试期间或 运行 时快速失败可能很好,但在编译时了解问题或获得编译时保证在 [= 时不会发生错误会更好32=]次.

Google 以前使用 FindBugs 来帮助在编译时查找空指针异常,但是 Google 现在基本上放弃了 FindBugs,因为它很难与 [=25] 集成=]构建系统。

Google 的数十个项目使用 Google 集合库中的 Checker Framework's Nullness Checker to find null pointer errors statically at compile time. The Nullness Checker has found errors,它是 Guava 的一部分。

我获取此信息的来源是在 Google 从事编程工具工作的朋友。