Search code examples
javaaccess-modifiersjava-9java-platform-module-system

"Module local" access behaviour in Java 9


As a core of Jigsaw project is the Java Module System, it would be nice to have an ability to restrict access to particular program elements (classes, methods and fields) within particular module only.

It can be helpful when there are some elements in module which are essentially public for this module, but shouldn't be accessible outside this module.

So I'm talking about the next level of access after "package-local", which could be named "module-local".

However a brief look on Jigsaw rules and early specs didn't help me to find out such kind of functionality. More specifically this Modifier specification doesn't contain any new elements.

So is there any other possibility to do it in future Java 9?


Solution

  • A public element (i.e., a class, interface, method, or field) in a non-exported package is, in effect, “module local.” It will be accessible to all other code in the module, but not from outside the module.

    There is no way to declare a module-local element in an exported package. A public element of an exported package is accessible from outside the module, a package-private element is still package-private, and there’s no element-level access mode between these two modes. We could define a new such mode but we’ve seen few compelling use cases for it and, moreover, implementing modular access control in the JVM at a granularity finer than that of exported packages would impose significant performance costs.