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

3

15

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:

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.

Shiksa answered 1/4, 2018 at 17:50 Comment(0)
L
12

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.

Linesman answered 1/4, 2018 at 18:17 Comment(2)
I've tried -Xlint:-requires-transitive-automatic and it worked. Thank you so much.Shiksa
I also added -Xlint:-requires-automatic.Choplogic
B
8

You can also just use @SuppressWarnings like so:

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

The JDK itself uses this technique.

Bricebriceno answered 9/6, 2021 at 0:4 Comment(0)
F
2

Sadly, the accepted answer didn't help me.
BTW, I am using Java 14 with a bunch of module system hacks for JUnit.

I had to add another flag, so the complete list looks as following:

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

I have searched for the error message and found the source code. There, one can see that the corresponding compiler key is called compiler.warn.leaks.not.accessible.not.required.transitive, with the command line arg -Xlint:exports.

Farika answered 17/6, 2020 at 13:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.