5

According to https://www.oracle.com/corporate/features/understanding-java-9-modules.html, the Java Module system introduces the following directives:

  • exports, exports ... to
  • uses
  • provides ... with
  • open, opens, opens ... to

What (if any) impact does each directive have on an external module accessing internal members using reflection?

For example, does exports <package> allow external modules to access all public, protected, private members of the exported package using reflection? What about the other directives?

Gili
  • 86,244
  • 97
  • 390
  • 689

1 Answers1

5

I would simply quote the #JLS7.7 here (formatted and categorized by me):

Distinct from access at compile time and access at runtime, the Java SE Platform provides reflective access via the Core Reflection API (ยง1.4).

More towards your question categorising as Normal module(module foo) and Open module (open module bar):

Normal Module

A normal module grants reflective access to types in only those packages which are explicitly exported or explicitly opened (or both).

  • the module's exported packages (exports com.example.foo.bar)

    For code outside a normal module, the reflective access granted to types in the module's exported (and not opened) packages is specifically to the public and protected types in those packages, and the public and protected members of those types.

  • the module's opened packages (opens com.example.foo.internal to com.example.bar)

    The reflective access granted to types in the module's opened packages (whether exported or not) is to all types in those packages, and all members of those types.

    No reflective access is granted to types, or their members, in packages which are not exported or opened.

  • within a module

    The code inside the module enjoys reflective access to all types, and all their members, in all packages in the module.

Open Module

An open module grants reflective access to types in all its packages, as if all packages had been opened.

  • the module's opened packages

    For code outside an open module, the reflective access granted to types in the module's opened packages (that is, all packages in the module) is to all types in those packages, and all members of those types.

  • within a module

    Code inside the module enjoys reflective access to all types, and all their members, in all packages in the module.

Naman
  • 27,789
  • 26
  • 218
  • 353
  • 1
    Apart from these `uses` pertains more towards consuming a Service implementation and `provides` over provisioning an implementation. โ€“ Naman Dec 26 '18 at 09:11
  • Excellent answer. There is just one section I don't understand. For `opens X to Y` why does the documentation say `the code inside the module enjoys reflective access to [...] all packages in the module`. Isn't access limited to package `X`? โ€“ Gili Dec 27 '18 at 13:10
  • 1
    @Gili When written as `module A { opens X to B; }` ... Within a module, using reflection, all package's types/member is accessible across. But a `module B {requires A;}` only gets to access all the types/members of `package X`. โ€“ Naman Dec 27 '18 at 14:46