Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is exporting the entire module not allowed?

Tags:

In Java 9's module declaration there are 2 constructs:

exports com.foo;

And

opens com.foo;

Where exports grants compile-time access, while opens allows runtime access, as reflection and resources.

opens has one leniency over exports that you can define the whole module as open, resulting the same as explicitly opening every package:

open module com.mod {

But there's no similar construct

exported module com.mod {

My Question: Why is it so; what decisions has been made to allow opening the whole module at once but not with exporting?

like image 903
Mordechai Avatar asked Jul 23 '17 20:07

Mordechai


People also ask

How many exports can a module have?

A module can have one and only one default export.

What can you export with module exports?

By module. exports, we can export functions, objects, and their references from one file and can use them in other files by importing them by require() method.

Can you have more than one module exports?

Answer. We can export more than one object using module.


1 Answers

A module's exports define its API, which should be deliberately designed and kept stable. An "exported module" could easily and inadvertently change its API by adding, removing, or renaming packages, which would go against the stability goal. (This is essentially the same reason why there are no "wildcard exports" like exports foo.bar.*).

Open packages, on the other hand, do not really define a module's API. Sure, code can depend on functionality that is only accessible via reflection, but the Java community generally sees reflection as a "hack" when used to access internals.

Its much wider (and more beneficial) use is to access an artifact to provide a service for it (XML/JSON serialization, persistence, dependency injection, ...). In such cases, the code reflecting over the module does not depend on it and is hence not broken by moving stuff around. There is hence less reason to keep the opened packages stable, which makes a free-for-all approach like open modules feasible.

like image 63
Nicolai Parlog Avatar answered Oct 04 '22 02:10

Nicolai Parlog