|
| 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