|
| 1 | +package cpw.mods.cl.test; |
| 2 | + |
| 3 | +import cpw.mods.cl.JarModuleFinder; |
| 4 | +import cpw.mods.cl.ModuleClassLoader; |
| 5 | +import cpw.mods.jarhandling.SecureJar; |
| 6 | + |
| 7 | +import java.io.File; |
| 8 | +import java.lang.module.Configuration; |
| 9 | +import java.lang.module.ModuleFinder; |
| 10 | +import java.nio.file.Path; |
| 11 | +import java.nio.file.Paths; |
| 12 | +import java.util.List; |
| 13 | +import java.util.ServiceLoader; |
| 14 | +import java.util.stream.Stream; |
| 15 | + |
| 16 | +public class TestjarUtil { |
| 17 | + private record BuiltLayer(ModuleClassLoader cl, ModuleLayer layer) {} |
| 18 | + |
| 19 | + /** |
| 20 | + * Build a layer for a {@code testjarX} source set. |
| 21 | + */ |
| 22 | + private static BuiltLayer buildTestjarLayer(int testjar, List<ModuleLayer> parentLayers) { |
| 23 | + var paths = Stream.of(System.getenv("sjh.testjar" + testjar).split(File.pathSeparator)) |
| 24 | + .map(Paths::get) |
| 25 | + .toArray(Path[]::new); |
| 26 | + var jar = SecureJar.from(paths); |
| 27 | + |
| 28 | + var roots = List.of(jar.name()); |
| 29 | + var jf = JarModuleFinder.of(jar); |
| 30 | + var conf = Configuration.resolveAndBind( |
| 31 | + jf, |
| 32 | + parentLayers.stream().map(ModuleLayer::configuration).toList(), |
| 33 | + ModuleFinder.of(), |
| 34 | + roots); |
| 35 | + var cl = new ModuleClassLoader("testjar2-layer", conf, parentLayers); |
| 36 | + var layer = ModuleLayer.defineModules(conf, parentLayers, m -> cl).layer(); |
| 37 | + return new BuiltLayer(cl, layer); |
| 38 | + } |
| 39 | + |
| 40 | + private static void withClassLoader(ClassLoader cl, TestCallback callback) throws Exception { |
| 41 | + // Replace context classloader during the callback |
| 42 | + var previousCl = Thread.currentThread().getContextClassLoader(); |
| 43 | + Thread.currentThread().setContextClassLoader(cl); |
| 44 | + |
| 45 | + try { |
| 46 | + callback.test(cl); |
| 47 | + } finally { |
| 48 | + Thread.currentThread().setContextClassLoader(previousCl); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Load the {@code testjar1} source set as new module into a new layer, |
| 54 | + * and run the callback with the new layer's classloader. |
| 55 | + */ |
| 56 | + public static void withTestjar1Setup(TestCallback callback) throws Exception { |
| 57 | + var built = buildTestjarLayer(1, List.of(ModuleLayer.boot())); |
| 58 | + |
| 59 | + withClassLoader(built.cl, callback); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Load the {@code testjar2} source set as new module into a new layer, |
| 64 | + * whose parent is a layer loaded from the {@code testjar1} source set. |
| 65 | + */ |
| 66 | + public static void withTestjar2Setup(TestCallback callback) throws Exception { |
| 67 | + var built1 = buildTestjarLayer(1, List.of(ModuleLayer.boot())); |
| 68 | + var built2 = buildTestjarLayer(2, List.of(built1.layer)); |
| 69 | + |
| 70 | + withClassLoader(built2.cl, callback); |
| 71 | + } |
| 72 | + |
| 73 | + @FunctionalInterface |
| 74 | + public interface TestCallback { |
| 75 | + void test(ClassLoader cl) throws Exception; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Instantiates a {@link ServiceLoader} within the testjar1 module. |
| 80 | + */ |
| 81 | + public static <S> ServiceLoader<S> loadTestjar1(ClassLoader cl, Class<S> clazz) throws Exception { |
| 82 | + // Use the `load` method from the testjar sourceset. |
| 83 | + var testClass = cl.loadClass("cpw.mods.cl.testjar1.ServiceLoaderTest"); |
| 84 | + var loadMethod = testClass.getMethod("load", Class.class); |
| 85 | + //noinspection unchecked |
| 86 | + return (ServiceLoader<S>) loadMethod.invoke(null, clazz); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Instantiates a {@link ServiceLoader} within the testjar2 module. |
| 91 | + */ |
| 92 | + public static <S> ServiceLoader<S> loadTestjar2(ClassLoader cl, Class<S> clazz) throws Exception { |
| 93 | + // Use the `load` method from the testjar sourceset. |
| 94 | + var testClass = cl.loadClass("cpw.mods.cl.testjar2.ServiceLoaderTest"); |
| 95 | + var loadMethod = testClass.getMethod("load", Class.class); |
| 96 | + //noinspection unchecked |
| 97 | + return (ServiceLoader<S>) loadMethod.invoke(null, clazz); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Instantiates a {@link ServiceLoader} within the classpath source set. |
| 102 | + */ |
| 103 | + public static <S> ServiceLoader<S> loadClasspath(ClassLoader cl, Class<S> clazz) throws Exception { |
| 104 | + // Use the `load` method from the testjar sourceset. |
| 105 | + var testClass = cl.loadClass("cpw.mods.testjar_cp.ServiceLoaderTest"); |
| 106 | + var loadMethod = testClass.getMethod("load", Class.class); |
| 107 | + //noinspection unchecked |
| 108 | + return (ServiceLoader<S>) loadMethod.invoke(null, clazz); |
| 109 | + } |
| 110 | +} |
0 commit comments