Skip to content

Commit 0f5f5ec

Browse files
committed
Add logic to defeat JPMS encapsulation
1 parent 0fcb329 commit 0f5f5ec

3 files changed

Lines changed: 110 additions & 1 deletion

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</parent>
1111

1212
<artifactId>app-launcher</artifactId>
13-
<version>2.2.1-SNAPSHOT</version>
13+
<version>2.3.0-SNAPSHOT</version>
1414

1515
<name>SciJava App Launcher</name>
1616
<description>Launcher for SciJava applications.</description>

src/main/java/org/scijava/launcher/ClassLauncher.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ public static Path appDir() {
7777
}
7878

7979
public static void main(final String... args) {
80+
if (Boolean.getBoolean("scijava.app.unlock-modules")) {
81+
ReflectionUnlocker.unlockAll();
82+
}
8083
tryToRun(Splash::show);
8184
tryToRun(Java::check);
8285
String appName = appName();
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*-
2+
* #%L
3+
* Launcher for SciJava applications.
4+
* %%
5+
* Copyright (C) 2007 - 2025 SciJava developers.
6+
* %%
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions are met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright notice,
11+
* this list of conditions and the following disclaimer.
12+
* 2. Redistributions in binary form must reproduce the above copyright notice,
13+
* this list of conditions and the following disclaimer in the documentation
14+
* and/or other materials provided with the distribution.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
20+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26+
* POSSIBILITY OF SUCH DAMAGE.
27+
* #L%
28+
*/
29+
30+
package org.scijava.launcher;
31+
32+
import java.lang.reflect.Method;
33+
34+
/**
35+
* A utility class to defeat JPMS encapsulation.
36+
* <p>
37+
* In Java 17 and later, reflection on modularized Java code is disallowed.
38+
* Normally, the only way to enable it is by passing {@code --add-opens}
39+
* arguments at launch, each of which unlocks a single package. This class
40+
* calls shenanigans on that requirement and unlocks everything at runtime.
41+
* The only argument needed at launch is
42+
* {@code --add-opens=java.base/java.lang=ALL-UNNAMED}, and maybe a
43+
* {@code --module-path} along with {@code --add-modules=ALL-MODULE-PATH}
44+
* so that they are in the set of modules unlocked by {@link #unlockAll()}.
45+
* </p>
46+
*/
47+
public final class ReflectionUnlocker {
48+
private static Method addOpens;
49+
private static Method addExports;
50+
private static Method getPackages;
51+
52+
static {
53+
try {
54+
ClassLoader cl = Object.class.getClassLoader(); // java.base/java.lang
55+
Class<?> moduleClass = ClassLoaders.loadClass(cl, "java.lang.Module");
56+
getPackages = moduleClass.getMethod("getPackages");
57+
addOpens = moduleClass.getDeclaredMethod("implAddOpensToAllUnnamed", String.class);
58+
addOpens.setAccessible(true);
59+
addExports = moduleClass.getDeclaredMethod("implAddExportsToAllUnnamed", String.class);
60+
addExports.setAccessible(true);
61+
}
62+
catch (Exception e) {
63+
Log.debug("Failed to initialize ReflectionUnlocker");
64+
Log.debug(e);
65+
}
66+
}
67+
68+
private ReflectionUnlocker() { }
69+
70+
public static void unlockAll() {
71+
if (addOpens == null) return; // Assume not Java 9+.
72+
73+
// ModuleLayer.boot().modules().forEach(ReflectionUnlocker::unlockModule);
74+
try {
75+
ClassLoader cl = Object.class.getClassLoader(); // java.base/java.lang
76+
Class<?> moduleLayerClass = ClassLoaders.loadClass(cl, "java.lang.ModuleLayer");
77+
Object moduleLayerBoot = moduleLayerClass.getMethod("boot").invoke(null);
78+
Method bootModules = moduleLayerBoot.getClass().getMethod("modules");
79+
Iterable<?> modules = (Iterable<?>) bootModules.invoke(moduleLayerBoot);
80+
modules.forEach(ReflectionUnlocker::unlockModule);
81+
}
82+
catch (Exception e) {
83+
Log.error("Failed to discover modules");
84+
Log.debug(e);
85+
}
86+
}
87+
88+
private static void unlockModule(Object m) {
89+
try {
90+
for (String pkg : (Iterable<String>) getPackages.invoke(m)) {
91+
try {
92+
addOpens.invoke(m, pkg);
93+
addExports.invoke(m, pkg);
94+
}
95+
catch (Exception e) {
96+
Log.debug("Failed to unlock package: " + pkg);
97+
Log.debug(e);
98+
}
99+
}
100+
}
101+
catch (Exception e) {
102+
Log.error("Failed to unlock module: " + m);
103+
Log.debug(e);
104+
}
105+
}
106+
}

0 commit comments

Comments
 (0)