/** * Test JAR file with META-INF/services configuration file listing a * provider that is not in the module. */ @Test(expectedExceptions = FindException.class) public void testMissingProviderPackage() throws IOException { Path tmpdir = Files.createTempDirectory(USER_DIR, "tmp"); // services configuration file Path services = tmpdir.resolve("META-INF").resolve("services"); Files.createDirectories(services); Files.write(services.resolve("p.S"), Set.of("q.P")); Path dir = Files.createTempDirectory(USER_DIR, "mods"); JarUtils.createJarFile(dir.resolve("m.jar"), tmpdir); // should throw FindException ModuleFinder.of(dir).findAll(); }
/** * Test non-class resources in a JAR file. */ public void testNonClassResources() throws IOException { Path dir = Files.createTempDirectory(USER_DIR, "mods"); createDummyJarFile(dir.resolve("m.jar"), "LICENSE", "README", "WEB-INF/tags", "p/Type.class", "p/resources/m.properties"); ModuleFinder finder = ModuleFinder.of(dir); Optional<ModuleReference> mref = finder.find("m"); assertTrue(mref.isPresent(), "m not found"); ModuleDescriptor descriptor = mref.get().descriptor(); assertTrue(descriptor.isAutomatic()); assertTrue(descriptor.packages().size() == 1); assertTrue(descriptor.packages().contains("p")); }
/** * Test ModuleFinder.ofSystem */ public void testOfSystem() { ModuleFinder finder = ModuleFinder.ofSystem(); assertTrue(finder.find("java.se").isPresent()); assertTrue(finder.find("java.base").isPresent()); assertFalse(finder.find("java.rhubarb").isPresent()); Set<String> names = finder.findAll().stream() .map(ModuleReference::descriptor) .map(ModuleDescriptor::name) .collect(Collectors.toSet()); assertTrue(names.contains("java.se")); assertTrue(names.contains("java.base")); assertFalse(names.contains("java.rhubarb")); }
/** * Basic test for detecting cycles involving a service provider module */ @Test(expectedExceptions = { ResolutionException.class }) public void testCycleInProvider() { ModuleDescriptor descriptor1 = newBuilder("m1") .exports("p") .uses("p.S") .build(); ModuleDescriptor descriptor2 = newBuilder("m2") .requires("m1") .requires("m3") .provides("p.S", List.of("q.T")) .build(); ModuleDescriptor descriptor3 = newBuilder("m3") .requires("m2") .build(); ModuleFinder finder = ModuleUtils.finderOf(descriptor1, descriptor2, descriptor3); // should throw ResolutionException because of the m2 <--> m3 cycle resolveAndBind(finder, "m1"); }
private static Set<String> javaSE() { String root = "java.se.ee"; ModuleFinder system = ModuleFinder.ofSystem(); if (system.find(root).isPresent()) { return Stream.concat(Stream.of(root), Configuration.empty().resolve(system, ModuleFinder.of(), Set.of(root)) .findModule(root).get() .reads().stream() .map(ResolvedModule::name)) .collect(toSet()); } else { // approximation return system.findAll().stream() .map(ModuleReference::descriptor) .map(ModuleDescriptor::name) .filter(name -> name.startsWith("java.") && !name.equals("java.smartcardio")) .collect(Collectors.toSet()); } }
/** * Test that a JAR file with a Main-Class attribute results * in a module with a main class. */ public void testMainClass() throws IOException { String mainClass = "p.Main"; Manifest man = new Manifest(); Attributes attrs = man.getMainAttributes(); attrs.put(Attributes.Name.MANIFEST_VERSION, "1.0.0"); attrs.put(Attributes.Name.MAIN_CLASS, mainClass); Path dir = Files.createTempDirectory(USER_DIR, "mods"); String entry = mainClass.replace('.', '/') + ".class"; createDummyJarFile(dir.resolve("m.jar"), man, entry); ModuleFinder finder = ModuleFinder.of(dir); Configuration parent = ModuleLayer.boot().configuration(); Configuration cf = resolve(parent, finder, "m"); ModuleDescriptor descriptor = findDescriptor(cf, "m"); assertTrue(descriptor.mainClass().isPresent()); assertEquals(descriptor.mainClass().get(), mainClass); }
/** * Test ModuleFinder with an exploded module containing a mix of class * and non-class resources */ public void testOfOneExplodedModuleWithResources() throws Exception { Path dir = Files.createTempDirectory(USER_DIR, "mods"); Path m_dir = createExplodedModule(dir.resolve("m"), "m", "LICENSE", "README", "WEB-INF/tags", "p/Type.class", "p/resources/m.properties", "q-/Type.class", // not a legal package name "q-/resources/m/properties"); ModuleFinder finder = ModuleFinder.of(m_dir); Optional<ModuleReference> mref = finder.find("m"); assertTrue(mref.isPresent(), "m not found"); ModuleDescriptor descriptor = mref.get().descriptor(); assertTrue(descriptor.packages().size() == 2); assertTrue(descriptor.packages().contains("p")); assertTrue(descriptor.packages().contains("p.resources")); }
public static void main(String[] args) throws IOException { if (args.length != 1) { System.err.println("Usage AddPackagesAttribute exploded-java-home"); System.exit(-1); } String home = args[0]; Path dir = Paths.get(home, "modules"); ModuleFinder finder = ModuleFinder.of(dir); try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) { for (Path entry : stream) { Path mi = entry.resolve("module-info.class"); if (Files.isRegularFile(mi)) { String mn = entry.getFileName().toString(); Optional<ModuleReference> omref = finder.find(mn); if (omref.isPresent()) { Set<String> packages = omref.get().descriptor().packages(); addPackagesAttribute(mi, packages); } } } } }
@Test public void test() throws Exception { ModuleFinder empty = ModuleFinder.of(); ModuleFinder finder = ModuleFinder.of(MODS_DIR); ModuleLayer bootLayer = ModuleLayer.boot(); Configuration cf0 = bootLayer.configuration(); Configuration cf1 = cf0.resolveAndBind(finder, empty, Set.of("s1", "s2")); Configuration cf2 = cf1.resolveAndBind(finder, empty, Set.of("s1", "s2")); // cf1 contains s1, p1, s2, p2 assertTrue(cf1.modules().size() == 4); // cf1 contains s1, p1, s2, p2 assertTrue(cf2.modules().size() == 4); ClassLoader scl = ClassLoader.getSystemClassLoader(); ModuleLayer layer1 = bootLayer.defineModulesWithManyLoaders(cf1, scl); testLayer(layer1); ModuleLayer layer2 = layer1.defineModulesWithManyLoaders(cf2, scl); testLayer(layer2); }
/** * Test "provides p.S" where p is contained in a different module. */ @Test(expectedExceptions = { ResolutionException.class }) public void testContainsService4() { ModuleDescriptor descriptor1 = newBuilder("m1") .packages(Set.of("p")) .build(); ModuleDescriptor descriptor2 = newBuilder("m2") .requires("m1") .provides("p.S", List.of("q.S1")) .build(); ModuleFinder finder = ModuleUtils.finderOf(descriptor1, descriptor2); // m2 does not read a module that exports p resolve(finder, "m2"); }
/** * Scan a JAR file or exploded module. */ private Optional<ModuleReference> scanModule(Path entry) { ModuleFinder finder = ModuleFinder.of(entry); try { return finder.findAll().stream().findFirst(); } catch (FindException e) { ostream.println(entry); ostream.println(INDENT + e.getMessage()); Throwable cause = e.getCause(); if (cause != null) { ostream.println(INDENT + cause); } errorFound = true; return Optional.empty(); } }
/** * Test all packages are exported */ public void testPackages() throws IOException { Path dir = Files.createTempDirectory(USER_DIR, "mods"); createDummyJarFile(dir.resolve("m.jar"), "p/C1.class", "p/C2.class", "q/C1.class"); ModuleFinder finder = ModuleFinder.of(dir); Optional<ModuleReference> mref = finder.find("m"); assertTrue(mref.isPresent(), "m not found"); ModuleDescriptor descriptor = mref.get().descriptor(); assertTrue(descriptor.isAutomatic()); assertTrue(descriptor.packages().size() == 2); assertTrue(descriptor.packages().contains("p")); assertTrue(descriptor.packages().contains("q")); assertTrue(descriptor.exports().isEmpty()); assertTrue(descriptor.opens().isEmpty()); }
static Configuration validate(ResourcePool pool) { checkPackages(pool); final Map<String, ModuleReference> nameToModRef = allModRefs(pool); final Set<ModuleReference> allRefs = new HashSet<>(nameToModRef.values()); final ModuleFinder finder = new ModuleFinder() { @Override public Optional<ModuleReference> find(String name) { return Optional.ofNullable(nameToModRef.get(name)); } @Override public Set<ModuleReference> findAll() { return allRefs; } }; return Configuration.empty().resolve( finder, ModuleFinder.of(), nameToModRef.keySet()); }
/** * Service provider dependency not found */ @Test(expectedExceptions = { FindException.class }) public void testServiceProviderDependencyNotFound() { // service provider dependency (on m3) not found ModuleDescriptor descriptor1 = newBuilder("m1") .exports("p") .uses("p.S") .build(); ModuleDescriptor descriptor2 = newBuilder("m2") .requires("m1") .requires("m3") .provides("p.S", List.of("q.T")) .build(); ModuleFinder finder = ModuleUtils.finderOf(descriptor1, descriptor2); // should throw ResolutionException because m3 is not found Configuration cf = resolveAndBind(finder, "m1"); }
/** * Test that a JAR file with a Main-Class attribute that is not a qualified * type name. */ @Test(dataProvider = "badmainclass") public void testBadMainClass(String mainClass, String ignore) throws IOException { Manifest man = new Manifest(); Attributes attrs = man.getMainAttributes(); attrs.put(Attributes.Name.MANIFEST_VERSION, "1.0.0"); attrs.put(Attributes.Name.MAIN_CLASS, mainClass); Path dir = Files.createTempDirectory(USER_DIR, "mods"); String entry = mainClass.replace('.', '/') + ".class"; createDummyJarFile(dir.resolve("m.jar"), man, entry); // bad Main-Class value should be ignored Optional<ModuleReference> omref = ModuleFinder.of(dir).find("m"); assertTrue(omref.isPresent()); ModuleDescriptor descriptor = omref.get().descriptor(); assertFalse(descriptor.mainClass().isPresent()); }
/** * Test unnamed module has no access to other proxy interface */ @Test public void testNoReadAccess() throws Exception { ModuleFinder finder = ModuleFinder.of(MODS_DIR); ModuleLayer bootLayer = ModuleLayer.boot(); Configuration cf = bootLayer .configuration() .resolveAndBind(ModuleFinder.of(), finder, modules); ClassLoader parentLoader = this.getClass().getClassLoader(); ModuleLayer layer = bootLayer.defineModulesWithOneLoader(cf, parentLoader); ClassLoader loader = layer.findLoader("m1"); Class<?>[] interfaces = new Class<?>[] { Class.forName("p.one.I", false, loader), Class.forName("q.NP", false, loader) // non-public interface in unnamed module }; checkIAE(loader, interfaces); }
/** * Basic test of "requires static": * m1 requires static m2 * m2 * resolve m1 */ public void testRequiresStatic2() { ModuleDescriptor descriptor1 = newBuilder("m1") .requires(Set.of(Requires.Modifier.STATIC), "m2") .build(); ModuleDescriptor descriptor2 = newBuilder("m2") .build(); ModuleFinder finder = ModuleUtils.finderOf(descriptor1, descriptor2); Configuration cf = resolve(finder, "m1"); assertTrue(cf.modules().size() == 1); ResolvedModule m1 = cf.findModule("m1").get(); assertTrue(m1.reads().size() == 0); }
/** * Resolve the given modules, by name, and returns the resulting * Configuration. */ private static Configuration resolveAndBind(String... roots) { ModuleFinder finder = ModuleFinder.of(MODS_DIR); return ModuleLayer.boot() .configuration() .resolveAndBind(finder, ModuleFinder.of(), Set.of(roots)); }
private static Map<String, Set<String>> indexMap() { return ModuleFinder.ofSystem() .findAll() .stream() .flatMap(JITTraceReader::signatures) .reduce((m1, m2) -> { m1.putAll(m2); return m1; }) .orElse(Map.of()); }
private static void validateImageJmodsTest(HashesTest ht, Path mpath) throws IOException { // hash is recorded in m1 and not any other packaged modules on module path ht.checkHashes("m1", "m2"); assertTrue(ht.hashes("m2") == null); // should not override any JDK packaged modules ModuleFinder finder = ModulePath.of(Runtime.version(), true, mpath); assertTrue(ht.hashes(finder,"jdk.compiler") == null); assertTrue(ht.hashes(finder,"jdk.attach") == null); }
/** * Test ModuleFinder.of with a file path to a directory containing a file * that will not be recognized as a module. */ public void testOfWithUnrecognizedEntryInDirectory1() throws Exception { Path dir = Files.createTempDirectory(USER_DIR, "mods"); Files.createTempFile(dir, "m", ".junk"); ModuleFinder finder = ModuleFinder.of(dir); assertFalse(finder.find("java.rhubarb").isPresent()); finder = ModuleFinder.of(dir); assertTrue(finder.findAll().isEmpty()); }
/** * Invokes parent.resolve(...) */ private Configuration resolve(Configuration parent, ModuleFinder before, ModuleFinder after, String... roots) { return parent.resolve(before, after, Set.of(roots)); }
/** * Test ModuleFinder.of with one directory of modules */ public void testOfOneDirectory() throws Exception { Path dir = Files.createTempDirectory(USER_DIR, "mods"); createExplodedModule(dir.resolve("m1"), "m1"); createModularJar(dir.resolve("m2.jar"), "m2"); ModuleFinder finder = ModuleFinder.of(dir); assertTrue(finder.findAll().size() == 2); assertTrue(finder.find("m1").isPresent()); assertTrue(finder.find("m2").isPresent()); assertFalse(finder.find("java.rhubarb").isPresent()); }
/** * Test ModuleLayer.defineModulesWithXXX when the modules that override same * named modules in the parent layer. * * layer1: m1, m2, m3 => same loader * layer2: m1, m3 => same loader */ public void testOverriding3() throws Exception { Configuration cf1 = resolve("m1"); ModuleLayer layer1 = ModuleLayer.boot().defineModulesWithOneLoader(cf1, null); checkLayer(layer1, "m1", "m2", "m3"); ModuleFinder finder = finderFor("m1", "m3"); Configuration cf2 = cf1.resolve(finder, ModuleFinder.of(), Set.of("m1")); ModuleLayer layer2 = layer1.defineModulesWithOneLoader(cf2, null); checkLayer(layer2, "m1", "m3"); invoke(layer1, "m1", "p.Main"); ClassLoader loader1 = layer1.findLoader("m1"); ClassLoader loader2 = layer2.findLoader("m1"); assertTrue(loader1.loadClass("p.Main").getClassLoader() == loader1); assertTrue(loader1.loadClass("q.Hello").getClassLoader() == loader1); assertTrue(loader1.loadClass("w.Hello").getClassLoader() == loader1); assertTrue(loader2.loadClass("p.Main").getClassLoader() == loader2); assertTrue(loader2.loadClass("q.Hello").getClassLoader() == loader1); assertTrue(loader2.loadClass("w.Hello").getClassLoader() == loader2); }
/** * Basic test of using the beforeFinder to override a module in a parent * configuration. */ public void testOverriding1() { ModuleDescriptor descriptor1 = newBuilder("m1").build(); ModuleFinder finder = ModuleUtils.finderOf(descriptor1); Configuration cf1 = resolve(finder, "m1"); assertTrue(cf1.modules().size() == 1); assertTrue(cf1.findModule("m1").isPresent()); Configuration cf2 = resolve(cf1, finder, "m1"); assertTrue(cf2.modules().size() == 1); assertTrue(cf2.findModule("m1").isPresent()); }
/** * Test ModuleLayer.defineModulesWithXXX when the modules that override same * named modules in the parent layer. * * Test scenario: * layer1: m1, m2, m3 => same loader * layer2: m1, m2, m4 => same loader */ public void testOverriding1() throws Exception { Configuration cf1 = resolve("m1"); ModuleLayer layer1 = ModuleLayer.boot().defineModulesWithOneLoader(cf1, null); checkLayer(layer1, "m1", "m2", "m3"); ModuleFinder finder = ModuleFinder.of(MODS_DIR); Configuration cf2 = cf1.resolve(finder, ModuleFinder.of(), Set.of("m1")); ModuleLayer layer2 = layer1.defineModulesWithOneLoader(cf2, null); checkLayer(layer2, "m1", "m2", "m3"); invoke(layer1, "m1", "p.Main"); ClassLoader loader1 = layer1.findLoader("m1"); ClassLoader loader2 = layer1.findLoader("m2"); ClassLoader loader3 = layer1.findLoader("m3"); ClassLoader loader4 = layer2.findLoader("m1"); ClassLoader loader5 = layer2.findLoader("m2"); ClassLoader loader6 = layer2.findLoader("m3"); assertTrue(loader1 == loader2); assertTrue(loader1 == loader3); assertTrue(loader4 == loader5); assertTrue(loader4 == loader6); assertTrue(loader4 != loader1); assertTrue(loader1.loadClass("p.Main").getClassLoader() == loader1); assertTrue(loader1.loadClass("q.Hello").getClassLoader() == loader1); assertTrue(loader1.loadClass("w.Hello").getClassLoader() == loader1); assertTrue(loader4.loadClass("p.Main").getClassLoader() == loader4); assertTrue(loader4.loadClass("q.Hello").getClassLoader() == loader4); assertTrue(loader4.loadClass("w.Hello").getClassLoader() == loader4); }
/** * Basic test of using the beforeFinder to override a module in a parent * configuration. */ public void testOverriding2() { ModuleDescriptor descriptor1 = newBuilder("m1").build(); Configuration cf1 = resolve(ModuleUtils.finderOf(descriptor1), "m1"); assertTrue(cf1.modules().size() == 1); assertTrue(cf1.findModule("m1").isPresent()); ModuleDescriptor descriptor2 = newBuilder("m2").build(); Configuration cf2 = resolve(ModuleUtils.finderOf(descriptor2), "m2"); assertTrue(cf2.modules().size() == 1); assertTrue(cf2.findModule("m2").isPresent()); ModuleDescriptor descriptor3 = newBuilder("m3").build(); Configuration cf3 = resolve(ModuleUtils.finderOf(descriptor3), "m3"); assertTrue(cf3.modules().size() == 1); assertTrue(cf3.findModule("m3").isPresent()); // override m2, m1 and m3 should be found in parent configurations ModuleFinder finder = ModuleUtils.finderOf(descriptor2); Configuration cf4 = Configuration.resolve( finder, List.of(cf1, cf2, cf3), ModuleFinder.of(), Set.of("m1", "m2", "m3")); assertTrue(cf4.modules().size() == 1); assertTrue(cf4.findModule("m2").isPresent()); ResolvedModule m2 = cf4.findModule("m2").get(); assertTrue(m2.configuration() == cf4); }
/** * Test JAR files with the Automatic-Module-Name attribute */ @Test(dataProvider = "modulenames") public void testAutomaticModuleNameAttribute(String name, String vs) throws IOException { Manifest man = new Manifest(); Attributes attrs = man.getMainAttributes(); attrs.put(Attributes.Name.MANIFEST_VERSION, "1.0.0"); attrs.put(new Attributes.Name("Automatic-Module-Name"), name); Path dir = Files.createTempDirectory(USER_DIR, "mods"); String jar; if (vs == null) { jar = "m.jar"; } else { jar = "m-" + vs + ".jar"; } createDummyJarFile(dir.resolve(jar), man); ModuleFinder finder = ModuleFinder.of(dir); assertTrue(finder.findAll().size() == 1); assertTrue(finder.find(name).isPresent()); ModuleReference mref = finder.find(name).get(); ModuleDescriptor descriptor = mref.descriptor(); assertEquals(descriptor.name(), name); assertEquals(descriptor.version() .map(ModuleDescriptor.Version::toString) .orElse(null), vs); }
/** * Test .class file in unnamed package (top-level directory) */ @Test(expectedExceptions = FindException.class) public void testClassInUnnamedPackage() throws IOException { Path dir = Files.createTempDirectory(USER_DIR, "mods"); createDummyJarFile(dir.resolve("m.jar"), "Mojo.class"); ModuleFinder finder = ModuleFinder.of(dir); finder.findAll(); }
private static Set<String> jdk() { return ModuleFinder.ofSystem().findAll().stream() .map(ModuleReference::descriptor) .map(ModuleDescriptor::name) .filter(name -> !JAVA_SE_SUBGRAPH.contains(name) && (name.startsWith("java.") || name.startsWith("jdk.") || name.startsWith("javafx."))) .collect(Collectors.toSet()); }
SystemModuleFinder() { if (Files.isRegularFile(Paths.get(JAVA_HOME, "lib", "modules"))) { // jrt file system this.fileSystem = FileSystems.getFileSystem(URI.create("jrt:/")); this.root = fileSystem.getPath("/modules"); this.systemModules = walk(root); } else { // exploded image this.fileSystem = FileSystems.getDefault(); root = Paths.get(JAVA_HOME, "modules"); this.systemModules = ModuleFinder.ofSystem().findAll().stream() .collect(toMap(mref -> mref.descriptor().name(), Function.identity())); } }
/** * Creates a Controller for a module layer containing modules m1 and m2. * Module m1 contains p1, reads java.base, does not export/open any package * Module m2 contains p2, reads java.base, does not export/open any package */ private ModuleLayer.Controller createTestLayer() { ModuleDescriptor descriptor1 = ModuleDescriptor.newModule("m1") .packages(Set.of("p1")) .requires("java.base") .build(); ModuleDescriptor descriptor2 = ModuleDescriptor.newModule("m2") .requires("java.base") .packages(Set.of("p2")) .build(); ModuleDescriptor descriptor3 = ModuleDescriptor.newModule("m3") .requires("java.base") .packages(Set.of("p3")) .build(); ModuleFinder finder = ModuleUtils.finderOf(descriptor1, descriptor2, descriptor3); ModuleLayer bootLayer = ModuleLayer.boot(); Configuration cf = bootLayer.configuration() .resolve(finder, ModuleFinder.of(), Set.of("m1", "m2", "m3")); ClassLoader scl = ClassLoader.getSystemClassLoader(); ModuleLayer.Controller controller = ModuleLayer.defineModulesWithOneLoader(cf, List.of(bootLayer), scl); ModuleLayer layer = controller.layer(); assertTrue(layer.modules().size() == 3); assertTrue(layer.findModule("m1").isPresent()); assertTrue(layer.findModule("m2").isPresent()); assertTrue(layer.findModule("m3").isPresent()); return controller; }
/** * Test multi-release JAR as an automatic module. */ public void testAutomaticModule() throws Exception { String name = "m"; Path jar = new JarBuilder(name) .resource("p/Main.class") .resource("p/Helper.class") .resource("META-INF/versions/" + VERSION + "/p/Helper.class") .resource("META-INF/versions/" + VERSION + "/p/internal/Helper.class") .build(); // find the module ModuleFinder finder = ModuleFinder.of(jar); Optional<ModuleReference> omref = finder.find(name); assertTrue((omref.isPresent())); ModuleReference mref = omref.get(); // check module packages ModuleDescriptor descriptor = mref.descriptor(); Set<String> packages = descriptor.packages(); if (MULTI_RELEASE) { assertTrue(packages.size() == 2); assertTrue(packages.contains("p.internal")); } else { assertTrue(packages.size() == 1); } }
/** * Basic test of layer containing automatic modules */ public void testInLayer() throws IOException { ModuleDescriptor descriptor = ModuleDescriptor.newModule("a") .requires("b") .requires("c") .build(); // b and c are simple JAR files Path dir = Files.createTempDirectory(USER_DIR, "mods"); createDummyJarFile(dir.resolve("b.jar"), "p/T.class"); createDummyJarFile(dir.resolve("c.jar"), "q/T2.class"); // module finder locates a and the modules in the directory ModuleFinder finder = ModuleFinder.compose(ModuleUtils.finderOf(descriptor), ModuleFinder.of(dir)); Configuration parent = ModuleLayer.boot().configuration(); Configuration cf = resolve(parent, finder, "a"); assertTrue(cf.modules().size() == 3); // each module gets its own loader ModuleLayer layer = ModuleLayer.boot().defineModules(cf, mn -> new ClassLoader() { }); // an unnamed module Module unnamed = (new ClassLoader() { }).getUnnamedModule(); Module b = layer.findModule("b").get(); assertTrue(b.isNamed()); assertTrue(b.canRead(unnamed)); testsReadsAll(b, layer); Module c = layer.findModule("c").get(); assertTrue(c.isNamed()); assertTrue(b.canRead(unnamed)); testsReadsAll(c, layer); }
public static void main(String... args) throws Exception { ModuleFinder afterFinder = ModuleFinder.of(Paths.get("mods")); Configuration cf = ModuleLayer.boot().configuration() .resolveAndBind(ModuleFinder.of(), afterFinder, List.of("m1", "m2")); System.out.println("Configuration: " + cf); ModuleLayer l = ModuleLayer.defineModulesWithManyLoaders(cf, List.of(ModuleLayer.boot()), ClassLoader.getPlatformClassLoader()) .layer(); Module m1 = l.findModule("m1").get(); ResourceBundle bundle = ResourceBundle.getBundle("p.resources.MyResource", Locale.US, m1); ResourceBundle jabundle = ResourceBundle.getBundle("p.resources.MyResource", Locale.JAPANESE, m1); String enResult = bundle.getString("key"); String jaResult = jabundle.getString("key"); if (!"hi".equals(enResult) || !"ja".equals(jaResult)) { throw new RuntimeException("Unexpected resources loaded: en: " + enResult + ", ja: " + jaResult); } Class<?> c = Class.forName(m1, "p.Main"); Method m = c.getDeclaredMethod("run"); m.invoke(null); }
/** * Test "provides p.S" where p is contained in the same module. */ public void testContainsService3() { ModuleDescriptor descriptor1 = newBuilder("m1") .packages(Set.of("p", "q")) .provides("p.S", List.of("q.S1")) .build(); ModuleFinder finder = ModuleUtils.finderOf(descriptor1); Configuration cf = resolve(finder, "m1"); assertTrue(cf.modules().size() == 1); assertTrue(cf.findModule("m1").isPresent()); }
static Set<String> systemModules() { Set<String> mods = Set.of("javafx.deploy", "jdk.deploy", "jdk.plugin", "jdk.javaws", // All JVMCI packages other than jdk.vm.ci.services are dynamically // exported to jdk.internal.vm.compiler and jdk.aot "jdk.internal.vm.compiler", "jdk.aot" ); return ModuleFinder.ofSystem().findAll().stream() .map(mref -> mref.descriptor().name()) .filter(mn -> !mods.contains(mn)) .collect(Collectors.toSet()); }
/** * Returns a ModuleFinder that is backed by the given SystemModules object. * * @apiNote The returned ModuleFinder is thread safe. */ static ModuleFinder of(SystemModules systemModules) { ModuleDescriptor[] descriptors = systemModules.moduleDescriptors(); ModuleTarget[] targets = systemModules.moduleTargets(); ModuleHashes[] recordedHashes = systemModules.moduleHashes(); ModuleResolution[] moduleResolutions = systemModules.moduleResolutions(); int moduleCount = descriptors.length; ModuleReference[] mrefs = new ModuleReference[moduleCount]; @SuppressWarnings(value = {"rawtypes", "unchecked"}) Map.Entry<String, ModuleReference>[] map = (Map.Entry<String, ModuleReference>[])new Map.Entry[moduleCount]; Map<String, byte[]> nameToHash = generateNameToHash(recordedHashes); for (int i = 0; i < moduleCount; i++) { String name = descriptors[i].name(); HashSupplier hashSupplier = hashSupplier(nameToHash, name); ModuleReference mref = toModuleReference(descriptors[i], targets[i], recordedHashes[i], hashSupplier, moduleResolutions[i]); mrefs[i] = mref; map[i] = Map.entry(name, mref); } return new SystemModuleFinder(mrefs, map); }
@Test public void testSystemModules() { Path jimage = Paths.get(System.getProperty("java.home"), "lib", "modules"); if (Files.notExists(jimage)) return; ModuleFinder.ofSystem().findAll().stream() .forEach(this::checkAttributes); }