编译检查插件框架以执行无零返回

发布于 2025-01-23 12:55:31 字数 339 浏览 1 评论 0原文

Java是否有任何框架 /编译器插件 /静态分析工具

  • 明确标记为无效(通过注释或其他方式),
  • 强制执行所有返回值,除非在给定的包装列表中

这本质上意味着强制执行kotlin模型,默认情况下,除非明确标记,否则什么都不是无效的。

请注意,仅查找@notnull注释并仅执行这些工具就不够,不允许null返回值是默认值。或者,该工具可以集中于可能为null的删除值,并强制执行除非标记为null,否则它们可能会要求您进行零检查。

显然,您仍然可以提出有关这种解决方案的问题,因为显然是将它们联系在一起的问题。也很高兴找出是否已经被问到,我自己找不到一个匹配的现有问题。

Is there any framework / compiler plugin / static analysis tool for Java that

  • Enforces that all return values are non-null unless they are explicitly marked as Nullable (via annotation or another way)
  • in a given list of packages

In essence that would mean enforcing the Kotlin model, where by default nothing can be null, unless it is specifically marked.

Note that a tool that just looks for @Notnull annotations and enforces only those would not be sufficient, not allowing null return values should be the default. Alternatively the tool can focus on dereferencing values that might be null and enforce that they cannot be null unless marked as such and then they may require you to do a null check.

You can obviously still bring questions that go for that kind of solution up as related questions obviously to link them together. Also happy to find out whether this has already been asked, did not find a matching existing question myself.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

給妳壹絲溫柔 2025-01-30 12:55:31

nullness检查器 rel =“ nofollow noreferrer”>检查器框架这样做。

关于您的第一个要求, nullness检查器文档

无效检查器支持的最重要注释是 @nonnull @nullable @nonnull很少写,因为它是默认值。

关于第二个要求,您可以使用-aonlydefs命令行选项。它的文档说:

设置-aonlydefs命令行选项与匹配类名称(不是文件名)的正则表达式,其定义应被键入。

它与完全合格的类名称匹配,因此您可以指定一个软件包名称。

The Nullness Checker of the Checker Framework does this.

Regarding your first requirement, the Nullness Checker documentation says:

The most important annotations supported by the Nullness Checker are @NonNull and @Nullable. @NonNull is rarely written, because it is the default.

Regarding your second requirement, you can use the -AonlyDefs command-line option. Its documentation says:

set the -AonlyDefs command-line option to a regular expression that matches class names (not file names) whose definitions should be type-checked.

It matches fully-qualified class names, so you can specify a package name.

波浪屿的海角声 2025-01-30 12:55:31

除了Mernst的接受答案外,我还找到了用于错误框架的Nullaway插件。
教程: https://wwwww.baeldung.com/java-nullaway
主页: https://github.com/uber/nullaway

重点,到目前为止,它们似乎都与我需要的东西相匹配。

In addition to the accepted answer from mernst, I have also found the NullAway plugin for the ErrorProne framework.
Tutorial: https://www.baeldung.com/java-nullaway
Main page: https://github.com/uber/NullAway

Will have to try out both at some point, so far they seem both to match what I need.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文