5

Why does the Java 9 compiler warn "requires directive for an automatic module" if module-info.java references automatic modules with "Automatic-Module-Name" set? What's the risk of such modules?

This question isn't an exact duplicate of What is an automatic module? because the latter does not address the specific reasons behind the compiler warning I have referenced (the context of the question matters). That said, it is a useful link for follow-up reading.

Gili
  • 86,244
  • 97
  • 390
  • 689
  • 2
    Not an exact duplicate though, but IMHO the answer to this is covered in [What is an automatic module?](https://stackoverflow.com/questions/46741907/what-is-an-automatic-module) ..Just that it does not specifically include Remi's prospect. – Naman Oct 15 '17 at 01:23
  • 4
    Possible duplicate of [What is an automatic module?](https://stackoverflow.com/questions/46741907/what-is-an-automatic-module) – ZhekaKozlov Oct 15 '17 at 02:02
  • 2
    Btw, you can disable this warning by using javac's extra option "-Xlint", for example "-Xlint:all,-requires-automatic" which means "enable all warnings but the warn about use of automatic modules in the requires clauses". – Valentin Kovalenko Oct 15 '17 at 03:57
  • 4
    I think the question is a bit mis-leading. javac will only emit a warning if you `requires transitive` an automatic module or you compile with `-Xlint:requires-automatic`. – Alan Bateman Oct 15 '17 at 07:32

2 Answers2

4

Quoting Remi Forax:

The main issue is that an automatic module can see classes from the classpath, but it also exports all its package so there is no encapsulation, and once you require one automatic module all automatic modules from the module path are visible.

So an automatic module is a great tool when you transitioned to the module world, but in fine, you do not want any automatic modules in you dependency graph.

Gili
  • 86,244
  • 97
  • 390
  • 689
  • Don't get me wrong, but every time I read this answer I feel this was already covered in the duplicated linked question. What's the difference here, could you put that in the answer as well? – Naman Oct 15 '17 at 03:41
  • @nullpointer the accepted answer of the linked question discusses automatic modules at length. If we were to close this question as a duplicate, it wouldn't be clear which subset of the 2 page answer addresses the compiler warning in question. It's like asking "why am I getting this Java compiler error?" and someone posting the Java Language Specification as the answer. Sure, the answer is there somewhere, but good luck finding it :) – Gili Oct 15 '17 at 03:48
  • And if you know, its there and a good thing to be pointed out, you can suggest(comment) as well as edit the answer to bring out the information. Asking another question to put a specific column from it isn't justified either. – Naman Oct 15 '17 at 03:50
3

Well, the problem is that automatic modules require transitively all other modules. So, by requiring it, you pollute the dependency graph with a lot of stuff and later you might get very surprising behavior.

Imagine that: You have an automatic module com.logging which is required by your (explicit) module my.app. However you also have another automatic module (not required by my.app, just residing in the module-path) called com.dbconnectors. It will get loaded because when one automatic module is required, all other will be resolved as well. It will also be accessible to my.app because com.logging requires it transitively.

Then, when your module my.app requires com.logging your code using stuff from com.dbconnectors "magically" works, while it doesn't when you remove the requires com.logging from the module descriptor.

In general transitive dependencies should be used judiciously.

nme
  • 647
  • 1
  • 7
  • 22