如何正确抑制 "requires transitive directive for an automatic module" 警告?

How to suppress the "requires transitive directive for an automatic module" warning properly?

将 Maven 项目升级到 Java 9 并添加模块描述符后,javac 抱怨自动模块的 传递依赖性:

[WARNING] /.../src/main/java/module-info.java:[3,35] requires transitive directive for an automatic module

重现问题的示例module-info.java

module com.example.mymodule {
    exports com.example.mymodule.myexportedpackage;
    requires transitive com.google.common;
}

这个警告的意思已经很清楚了,这里是一些相关的链接:

问题是 — 如何在不解决实际问题且不禁用所有其他 javac 警告的情况下抑制此警告,

我已经尝试了以下选项,但其中 none 行得通:


不幸的是,我无法解决实际问题(目前),因为 "my" 模块具有来自第三方(自动)模块(例如 Guava)的 return 类型和注释。因此,如果我使用 "requires com.google.common"(不使用 transitive),则会出现不同的警告,例如:

[WARNING] .../MyClass.java:[25,20] class com.google.common.collect.Table in module com.google.common is not indirectly exported using requires transitive

当然我不能为第三方库(现在是自动模块)定义模块描述符。

我正在使用我希望保留的 -Werror,所以警告不仅仅是烦人的...


P.S。我不打算将我的工件发布到任何 public 存储库。

您可以尝试使用

关闭警告选项
-Xlint:-requires-transitive-automatic

JDK-8178011 合并的更改说明:-

There should be two new warnings:

  • when a named module "requires transitive" an automatic module (default on)
  • when a named module "requires" an automatic module (default off)

推断这个 from the changes made here and also from the edit to the JEP 261: Module System 证实了(强调我的):-

In both of the modular modes the compiler will, by default, generate various warnings related to the module system; these may be disabled via the option -Xlint:-module.

More precise control of these warnings is available via the exports, opens, requires-automatic, and requires-transitive-automatic keys for the -Xlint option.

遗憾的是,接受的答案对我没有帮助。
顺便说一句,我正在使用 Java 14 和一堆 JUnit 模块系统 hack。

我不得不添加另一个标志,因此完整列表如下所示:

-Xlint:-exports -Xlint:-requires-transitive-automatic -Xlint:-requires-automatic

我搜索了错误消息并找到了 source code。在那里,可以看到相应的编译器键名为 compiler.warn.leaks.not.accessible.not.required.transitive,命令行参数为 -Xlint:exports.

您也可以像这样使用 @SuppressWarnings

@SuppressWarnings({ "requires-automatic", "requires-transitive-automatic" })
module foo {
 // ...
}

JDK itself uses this technique.