You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 313843f
Browse filesBrowse the repository at this point in the historyBrowse files
Constructor injection was the wrong call, for the reasons raised in
review: it publishes every dependency in a signature, so changing an
internal dependency becomes an API change and a binary compatibility
break unless old constructors are kept forever. It also cannot serve the
ServiceLoader tier, which requires a public no-arg constructor or a
static provider() factory.
The premise behind it was also wrong. I had assumed that letting the
container instantiate a plugin means exporting its package, which would
indeed break encapsulation -- callers could cast to implementations and
call non-API methods. But `opens` is not `exports`: a qualified
`opens <pkg> to <container>` grants deep reflective access at runtime
while leaving the package invisible at compile time.
Measured with a three-module test project on JDK 21, rather than assumed:
- ServiceLoader finds a provider declared with `provides` in a package
that is neither exported nor opened.
- Reflective newInstance on such a package fails with
IllegalAccessException; with a qualified `opens` it succeeds.
- setAccessible injection into a private field works with that opens.
- An ordinary module importing a class from the opened but unexported
package fails to compile: "package ... is declared in module ..., which
does not export it".
- A service with a private constructor and a public static provider()
factory works; ServiceLoader prefers the factory.
So declarative field injection survives JPMS intact, encapsulation is
preserved, and the cost to a plugin module is one `opens` line. The claim
that we must give up private-field injection is removed.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013gJpzGd3ksbME4CN6Tu2ni
This is the one thing forcing every plugin module to open itself.
193
+
These were measured, not reasoned about, with a three-module test project
194
+
(`javac --module-source-path`, run on the module path, JDK 21):
198
195
199
-
Therefore the SJ3 context uses **constructor injection** (or an explicit
200
-
`initialize(Context)`), so that `exports` suffices and `@Plugin` coexists with
201
-
JPMS.
196
+
| Situation | Result |
197
+
| --- | --- |
198
+
| Index resources (`META-INF/json/...`) read via `ClassLoader.getResources`|**works** — `META-INF` is not a valid package name, so it is never encapsulated |
199
+
|`ServiceLoader` finding a provider declared with `provides`, in a package that is neither exported nor opened |**works** — the `provides` declaration itself grants the access |
200
+
| Reflective `newInstance` on a public class in a package that is neither exported nor opened |**fails**: `IllegalAccessException`|
201
+
| The same, with a qualified `opens <pkg> to <container>`|**works**|
202
+
|`setAccessible` injection into a **private** field, with that qualified `opens`|**works**|
203
+
| An ordinary module importing a class from that opened-but-unexported package |**compile error**: "package ... is declared in module ..., which does not export it" |
204
+
| A service with a **private** constructor plus a `public static provider()` factory |**works** — `ServiceLoader` prefers the factory |
205
+
206
+
The load-bearing conclusion is the second-to-last row: **`opens` is not
207
+
`exports`.** A qualified `opens` grants the container deep reflective access at
208
+
runtime while leaving the package invisible to ordinary callers at compile
0 commit comments