|
| 1 | +/* |
| 2 | + * Copyright 2025 the original author or authors. |
| 3 | + * <p> |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * <p> |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * <p> |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.openrewrite.java; |
| 17 | + |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | + |
| 20 | +import java.lang.reflect.Method; |
| 21 | + |
| 22 | +import static org.assertj.core.api.Assertions.assertThat; |
| 23 | +import static org.junit.jupiter.api.Assumptions.assumeTrue; |
| 24 | + |
| 25 | +class JavaUnrestrictedClassLoaderTest { |
| 26 | + |
| 27 | + private static boolean hasModuleSystem() { |
| 28 | + try { |
| 29 | + Class.forName("java.lang.Module"); |
| 30 | + return true; |
| 31 | + } catch (ClassNotFoundException e) { |
| 32 | + return false; |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Verifies that when a class from a named module (jdk.compiler) is passed to |
| 38 | + * addExportIfNeeded, the module export is added so that classes in the |
| 39 | + * classloader's unnamed module can access classes in that package. |
| 40 | + * <p> |
| 41 | + * This tests the fix for the JDK 25 IllegalAccessError where a module split |
| 42 | + * (some classes in unnamed module, some in jdk.compiler) caused access failures. |
| 43 | + */ |
| 44 | + @Test |
| 45 | + void addExportIfNeededExportsPackageFromNamedModule() throws Throwable { |
| 46 | + assumeTrue(hasModuleSystem(), "Requires JDK 9+ module system"); |
| 47 | + |
| 48 | + JavaUnrestrictedClassLoader loader = new JavaUnrestrictedClassLoader( |
| 49 | + getClass().getClassLoader()); |
| 50 | + |
| 51 | + try { |
| 52 | + Class<?> moduleClass = Class.forName("java.lang.Module"); |
| 53 | + Method getModule = Class.class.getMethod("getModule"); |
| 54 | + Method getUnnamedModule = ClassLoader.class.getMethod("getUnnamedModule"); |
| 55 | + Method isExported = moduleClass.getMethod("isExported", String.class, moduleClass); |
| 56 | + |
| 57 | + // Load a class that lives in jdk.compiler module via the system classloader. |
| 58 | + // Class.forName succeeds for non-exported packages; the class object is in |
| 59 | + // jdk.compiler module regardless of export status. |
| 60 | + Class<?> tokensClass = Class.forName( |
| 61 | + "com.sun.tools.javac.parser.Tokens", |
| 62 | + false, |
| 63 | + ClassLoader.getSystemClassLoader()); |
| 64 | + |
| 65 | + Object jdkCompilerModule = getModule.invoke(tokensClass); |
| 66 | + Object unnamedModule = getUnnamedModule.invoke(loader); |
| 67 | + String packageName = "com.sun.tools.javac.parser"; |
| 68 | + |
| 69 | + // Call addExportIfNeeded via reflection |
| 70 | + Method addExportIfNeeded = JavaUnrestrictedClassLoader.class |
| 71 | + .getDeclaredMethod("addExportIfNeeded", Class.class); |
| 72 | + addExportIfNeeded.setAccessible(true); |
| 73 | + addExportIfNeeded.invoke(loader, tokensClass); |
| 74 | + |
| 75 | + // After the call, jdk.compiler should export the package to our unnamed module |
| 76 | + boolean exported = (boolean) isExported.invoke( |
| 77 | + jdkCompilerModule, packageName, unnamedModule); |
| 78 | + assertThat(exported) |
| 79 | + .as("jdk.compiler should export com.sun.tools.javac.parser to the classloader's unnamed module") |
| 80 | + .isTrue(); |
| 81 | + } finally { |
| 82 | + loader.close(); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Verifies that addExportIfNeeded is a no-op for classes in the unnamed module |
| 88 | + * (i.e., classes defined by our own classloader via defineClass). |
| 89 | + */ |
| 90 | + @Test |
| 91 | + void addExportIfNeededSkipsUnnamedModuleClasses() throws Throwable { |
| 92 | + assumeTrue(hasModuleSystem(), "Requires JDK 9+ module system"); |
| 93 | + |
| 94 | + JavaUnrestrictedClassLoader loader = new JavaUnrestrictedClassLoader( |
| 95 | + getClass().getClassLoader()); |
| 96 | + |
| 97 | + try { |
| 98 | + // Load a class through our classloader's defineClass path (unnamed module) |
| 99 | + Class<?> contextClass = loader.loadClass("com.sun.tools.javac.util.Context"); |
| 100 | + |
| 101 | + Method getModule = Class.class.getMethod("getModule"); |
| 102 | + Object module = getModule.invoke(contextClass); |
| 103 | + |
| 104 | + Class<?> moduleClass = Class.forName("java.lang.Module"); |
| 105 | + Method isNamed = moduleClass.getMethod("isNamed"); |
| 106 | + |
| 107 | + // The class should be in our unnamed module (loaded via defineClass) |
| 108 | + assertThat((boolean) isNamed.invoke(module)) |
| 109 | + .as("Class loaded via defineClass should be in unnamed module") |
| 110 | + .isFalse(); |
| 111 | + |
| 112 | + // addExportIfNeeded should silently return without error |
| 113 | + Method addExportIfNeeded = JavaUnrestrictedClassLoader.class |
| 114 | + .getDeclaredMethod("addExportIfNeeded", Class.class); |
| 115 | + addExportIfNeeded.setAccessible(true); |
| 116 | + addExportIfNeeded.invoke(loader, contextClass); |
| 117 | + // No exception = success |
| 118 | + } finally { |
| 119 | + loader.close(); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Verifies that addExportIfNeeded is idempotent - calling it multiple times |
| 125 | + * for classes in the same package does not fail. |
| 126 | + */ |
| 127 | + @Test |
| 128 | + void addExportIfNeededIsIdempotent() throws Throwable { |
| 129 | + assumeTrue(hasModuleSystem(), "Requires JDK 9+ module system"); |
| 130 | + |
| 131 | + JavaUnrestrictedClassLoader loader = new JavaUnrestrictedClassLoader( |
| 132 | + getClass().getClassLoader()); |
| 133 | + |
| 134 | + try { |
| 135 | + Class<?> tokensClass = Class.forName( |
| 136 | + "com.sun.tools.javac.parser.Tokens", |
| 137 | + false, |
| 138 | + ClassLoader.getSystemClassLoader()); |
| 139 | + Class<?> tokenKindClass = Class.forName( |
| 140 | + "com.sun.tools.javac.parser.Tokens$TokenKind", |
| 141 | + false, |
| 142 | + ClassLoader.getSystemClassLoader()); |
| 143 | + |
| 144 | + Method addExportIfNeeded = JavaUnrestrictedClassLoader.class |
| 145 | + .getDeclaredMethod("addExportIfNeeded", Class.class); |
| 146 | + addExportIfNeeded.setAccessible(true); |
| 147 | + |
| 148 | + // Call multiple times for classes in the same package - should not throw |
| 149 | + addExportIfNeeded.invoke(loader, tokensClass); |
| 150 | + addExportIfNeeded.invoke(loader, tokenKindClass); |
| 151 | + addExportIfNeeded.invoke(loader, tokensClass); |
| 152 | + } finally { |
| 153 | + loader.close(); |
| 154 | + } |
| 155 | + } |
| 156 | +} |
0 commit comments