The JLS 11 "7.7.2. Exported and Opened Packages" says:
It is permitted for
opens
to specify a package which is not declared by a compilation unit associated with the current module.
What would be a scenario for this? Why is this needed?
Thanks to Alan Bateman and Michael Easter for explanations, and I can think of some realistic scenarios.
First, as was explained by Alan and Michael: JLS allows to "open" directories without Java types for situations when we keep resources in them. So, for us these are "resource directories", but JLS names it as package which is not declared by a compilation unit
. And it's not even question of "allows" but rather "must". Without opens
directive for resources directory (com/foo/abc
), another module cannot read resource like that:
InputStream is = ClassLoader.getSystemResourceAsStream("com/foo/abc/config.properties");
Second, I wrote this question mostly because I was puzzled that opens
allows to specify even non-existing "packages" (directories), although it gives warnings, but compiles successfully.
A possible scenario behind that I can think of a build script for modular JAR file:
.java
files (we have module-info.java
define opens com.foo.abc;
and com/foo/abc
directory ("package") does not exist)com/foo/abc
directoryconfig_dev.properties
file, at this step decision made for what environment we make build (PROD/TEST/DEV)In case opens
for non-existing directory was not allowed, step #1 would fail. But it only gives a warning during compile time, which is Ok.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With