Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

After upgrading a Maven project to Java 9 and adding a module descriptor, javac complains about a transitive dependency for an automatic module:

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

An example module-info.java to reproduce the problem:

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

The meaning of this warning is completely clear, here are some related links:

  • What's the difference between requires and requires transitive statements in Java 9?
  • Why does javac complain about named automatic-modules?
  • Related OpenJDK issue

The question is — how to suppress this warning, without fixing the actual issue, and without disabling all the other javac warnings?

I've tried the following options, but none of them worked:

  • @SuppressWarnings("module") in module-info.java
  • @SuppressWarnings("all") in module-info.java
  • -Xlint:all,-module command line option

Unfortunately, I cannot fix the actual issue (for now) because "my" module has return types and annotations from third-party (automatic) modules (e.g. Guava). Thus, if I'd use "requires com.google.common" (without transitive), then there would be a different warning, e.g.:

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

And of course I cannot define module descriptors for the third-party libraries (which are automatic modules right now).

I'm using -Werror which I'd prefer to keep, so the warning isn't merely annoying...


P.S. I do not intend to publish my artifacts to any public repositories.

like image 576
Alex Shesterov Avatar asked Apr 01 '18 17:04

Alex Shesterov


2 Answers

You can also just use @SuppressWarnings like so:

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

The JDK itself uses this technique.

like image 57
Laird Nelson Avatar answered Sep 22 '22 07:09

Laird Nelson


You could try out the option of switching off the warning using

-Xlint:-requires-transitive-automatic

The changes for which were merged with JDK-8178011 stating:-

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)

Inferring this from the changes made here and also from the edit to the JEP 261: Module System which confirms that(emphasis mine):-

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.

like image 33
Naman Avatar answered Sep 20 '22 07:09

Naman