java.lang.module.ModuleDescriptor Java Examples
The following examples show how to use
java.lang.module.ModuleDescriptor.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AutomaticModulesTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
/** * Test that a JAR file with a Main-Class attribute that is not in the module */ public void testMissingMainClassPackage() 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, "p.Main"); Path dir = Files.createTempDirectory(USER_DIR, "mods"); createDummyJarFile(dir.resolve("m.jar"), man); // Main-Class should be ignored because package p is not in module Optional<ModuleReference> omref = ModuleFinder.of(dir).find("m"); assertTrue(omref.isPresent()); ModuleDescriptor descriptor = omref.get().descriptor(); assertFalse(descriptor.mainClass().isPresent()); }
Example #2
Source File: BasicLayerTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
/** * Test layers with a qualified export. The module exporting the package * does not read the target module. * * m1 { exports p to m2 } * m2 { } */ public void testQualifiedExports1() { ModuleDescriptor descriptor1 = newBuilder("m1"). exports("p", Set.of("m2")) .build(); ModuleDescriptor descriptor2 = newBuilder("m2") .build(); ModuleFinder finder1 = ModuleUtils.finderOf(descriptor1, descriptor2); Configuration cf = resolve(finder1, "m1", "m2"); ClassLoader cl = new ClassLoader() { }; ModuleLayer layer = ModuleLayer.empty().defineModules(cf, mn -> cl); assertTrue(layer.modules().size() == 2); Module m1 = layer.findModule("m1").get(); Module m2 = layer.findModule("m2").get(); // check m1 exports p to m2 assertFalse(m1.isExported("p")); assertTrue(m1.isExported("p", m2)); assertFalse(m1.isOpen("p", m2)); }
Example #3
Source File: Loader.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
/** * Creates a {@code Loader} that loads classes/resources from a collection * of modules. * * @throws IllegalArgumentException * If two or more modules have the same package */ public Loader(Collection<ResolvedModule> modules, ClassLoader parent) { super(parent); this.pool = null; this.parent = parent; Map<String, ModuleReference> nameToModule = new HashMap<>(); Map<String, LoadedModule> localPackageToModule = new HashMap<>(); for (ResolvedModule resolvedModule : modules) { ModuleReference mref = resolvedModule.reference(); ModuleDescriptor descriptor = mref.descriptor(); nameToModule.put(descriptor.name(), mref); descriptor.packages().forEach(pn -> { LoadedModule lm = new LoadedModule(mref); if (localPackageToModule.put(pn, lm) != null) throw new IllegalArgumentException("Package " + pn + " in more than one module"); }); } this.nameToModule = nameToModule; this.localPackageToModule = localPackageToModule; this.acc = AccessController.getContext(); }
Example #4
Source File: AutomaticModulesTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
/** * 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); }
Example #5
Source File: ConfigurationTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
/** * Test "uses p.S" where p is contained in a different module. */ @Test(expectedExceptions = { ResolutionException.class }) public void testContainsService2() { ModuleDescriptor descriptor1 = newBuilder("m1") .packages(Set.of("p")) .build(); ModuleDescriptor descriptor2 = newBuilder("m2") .requires("m1") .uses("p.S") .build(); ModuleFinder finder = ModuleUtils.finderOf(descriptor1, descriptor2); // m2 does not read a module that exports p resolve(finder, "m2"); }
Example #6
Source File: JvmtiGetAllModulesTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
public static void main(String[] args) throws Exception { final String MY_MODULE_NAME = "myModule"; // Verify that JVMTI reports exactly the same info as Java regarding the named modules Asserts.assertEquals(ModuleLayer.boot().modules(), getModulesJVMTI()); // Load a new named module ModuleDescriptor descriptor = ModuleDescriptor.newModule(MY_MODULE_NAME).build(); ModuleFinder finder = finderOf(descriptor); ClassLoader loader = new ClassLoader() {}; Configuration parent = ModuleLayer.boot().configuration(); Configuration cf = parent.resolve(finder, ModuleFinder.of(), Set.of(MY_MODULE_NAME)); ModuleLayer my = ModuleLayer.boot().defineModules(cf, m -> loader); // Verify that the loaded module is indeed reported by JVMTI Set<Module> jvmtiModules = getModulesJVMTI(); for (Module mod : my.modules()) { if (!jvmtiModules.contains(mod)) { throw new RuntimeException("JVMTI did not report the loaded named module: " + mod.getName()); } } }
Example #7
Source File: AutomaticModulesTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
/** * Basic test to ensure that no automatic modules are resolved when * an automatic module is not a root or required by other modules. */ public void testInConfiguration4() throws IOException { ModuleDescriptor descriptor1 = ModuleDescriptor.newModule("m1") .requires("java.base") .build(); // automatic modules Path dir = Files.createTempDirectory(USER_DIR, "mods"); createDummyJarFile(dir.resolve("auto1.jar"), "p1/C.class"); createDummyJarFile(dir.resolve("auto2.jar"), "p2/C.class"); createDummyJarFile(dir.resolve("auto3.jar"), "p3/C.class"); // module finder locates m1 and the modules in the directory ModuleFinder finder1 = ModuleUtils.finderOf(descriptor1); ModuleFinder finder2 = ModuleFinder.of(dir); ModuleFinder finder = ModuleFinder.compose(finder1, finder2); Configuration parent = ModuleLayer.boot().configuration(); Configuration cf = resolve(parent, finder, "m1"); // ensure that no automatic module is resolved assertTrue(cf.modules().size() == 1); assertTrue(cf.findModule("m1").isPresent()); }
Example #8
Source File: DiceGameApplication.java From commons-rng with Apache License 2.0 | 6 votes |
/** * Display JPMS information. */ private void displayModuleInfo() { final StringBuilder str = new StringBuilder(); for (Module mod : new Module[] {DiceGame.class.getModule(), DiceGameApplication.class.getModule()}) { System.out.println("--- " + mod + " ---"); final ModuleDescriptor desc = mod.getDescriptor(); for (ModuleDescriptor.Requires r : desc.requires()) { System.out.println(mod.getName() + " requires " + r.name()); } System.out.println(); } }
Example #9
Source File: AutomaticModulesTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
/** * 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()); }
Example #10
Source File: ClassLoaderTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
/** * Creates regular/modular jar files for TestClient and TestClassLoader. */ private static void setUp() throws Exception { // Generate regular jar files for TestClient and TestClassLoader JarUtils.createJarFile(CL_JAR, TEST_CLASSES, "cl/TestClassLoader.class"); JarUtils.createJarFile(C_JAR, TEST_CLASSES, "c/TestClient.class"); // Generate modular jar files for TestClient and TestClassLoader with // their corresponding ModuleDescriptor. Files.copy(CL_JAR, MCL_JAR, StandardCopyOption.REPLACE_EXISTING); updateModuleDescr(MCL_JAR, ModuleDescriptor.newModule("mcl") .exports("cl").requires("java.base").build()); Files.copy(C_JAR, MC_JAR, StandardCopyOption.REPLACE_EXISTING); updateModuleDescr(MC_JAR, ModuleDescriptor.newModule("mc") .exports("c").requires("java.base").requires("mcl").build()); Files.copy(C_JAR, AMC_JAR, StandardCopyOption.REPLACE_EXISTING); updateModuleDescr(AMC_JAR, ModuleDescriptor.newModule("mc") .exports("c").requires("java.base").requires("cl").build()); }
Example #11
Source File: ModuleLayer.java From Bytecoder with Apache License 2.0 | 6 votes |
/** * Checks a configuration and the module-to-loader mapping to ensure that * no two modules mapped to the same class loader have the same package. * It also checks that no two automatic modules have the same package. * * @throws LayerInstantiationException */ private static void checkForDuplicatePkgs(Configuration cf, Function<String, ClassLoader> clf) { // HashMap allows null keys Map<ClassLoader, Set<String>> loaderToPackages = new HashMap<>(); for (ResolvedModule resolvedModule : cf.modules()) { ModuleDescriptor descriptor = resolvedModule.reference().descriptor(); ClassLoader loader = clf.apply(descriptor.name()); Set<String> loaderPackages = loaderToPackages.computeIfAbsent(loader, k -> new HashSet<>()); for (String pkg : descriptor.packages()) { boolean added = loaderPackages.add(pkg); if (!added) { throw fail("More than one module with package %s mapped" + " to the same class loader", pkg); } } } }
Example #12
Source File: AutomaticModulesTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
/** * Basic test of a configuration created with automatic modules * a requires b* and c* * b* contains p * c* contains p */ @Test(expectedExceptions = { ResolutionException.class }) public void testDuplicateSuppliers1() throws IOException { ModuleDescriptor descriptor = ModuleDescriptor.newModule("a") .requires("b") .requires("c") .build(); // c and d are automatic modules with the same package Path dir = Files.createTempDirectory(USER_DIR, "mods"); createDummyJarFile(dir.resolve("b.jar"), "p/T.class"); createDummyJarFile(dir.resolve("c.jar"), "p/T.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(); resolve(parent, finder, "a"); }
Example #13
Source File: HashesTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
private void makeModule(String mn, ModuleDescriptor.Requires.Modifier mod, String... deps) throws IOException { if (mod != null && mod != TRANSITIVE && mod != STATIC) { throw new IllegalArgumentException(mod.toString()); } StringBuilder sb = new StringBuilder(); sb.append("module " + mn + " {").append("\n"); Arrays.stream(deps).forEach(req -> { sb.append(" requires "); if (mod != null) { sb.append(mod.toString().toLowerCase()).append(" "); } sb.append(req + ";\n"); }); sb.append("}\n"); builder.writeJavaFiles(mn, sb.toString()); compileModule(mn, srcDir); }
Example #14
Source File: ModuleAnalyzer.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
private boolean matches(ModuleDescriptor md, ModuleDescriptor other) { // build requires transitive from ModuleDescriptor Set<ModuleDescriptor.Requires> reqTransitive = md.requires().stream() .filter(req -> req.modifiers().contains(TRANSITIVE)) .collect(toSet()); Set<ModuleDescriptor.Requires> otherReqTransitive = other.requires().stream() .filter(req -> req.modifiers().contains(TRANSITIVE)) .collect(toSet()); if (!reqTransitive.equals(otherReqTransitive)) { trace("mismatch requires transitive: %s%n", reqTransitive); return false; } Set<ModuleDescriptor.Requires> unused = md.requires().stream() .filter(req -> !other.requires().contains(req)) .collect(Collectors.toSet()); if (!unused.isEmpty()) { trace("mismatch requires: %s%n", unused); return false; } return true; }
Example #15
Source File: ModuleDotGraph.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
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()); } }
Example #16
Source File: LauncherHelper.java From Bytecoder with Apache License 2.0 | 5 votes |
/** * Prints a single line with the module name, version and modifiers */ private static void showModule(ModuleReference mref) { ModuleDescriptor md = mref.descriptor(); ostream.print(md.toNameAndVersion()); mref.location() .filter(uri -> !isJrt(uri)) .ifPresent(uri -> ostream.format(" %s", uri)); if (md.isOpen()) ostream.print(" open"); if (md.isAutomatic()) ostream.print(" automatic"); ostream.println(); }
Example #17
Source File: JdepsConfiguration.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
private ModuleDescriptor dropHashes(ModuleDescriptor md) { ModuleDescriptor.Builder builder = ModuleDescriptor.newModule(md.name()); md.requires().forEach(builder::requires); md.exports().forEach(builder::exports); md.opens().forEach(builder::opens); md.provides().stream().forEach(builder::provides); md.uses().stream().forEach(builder::uses); builder.packages(md.packages()); return builder.build(); }
Example #18
Source File: ListModules.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
public static void main(String... args) { Set<String> modules = ModuleFinder.ofSystem().findAll() .stream() .map(ModuleReference::descriptor) .map(ModuleDescriptor::name) .collect(Collectors.toSet()); Set<String> expected = Arrays.stream(args).collect(Collectors.toSet()); if (!modules.equals(expected)) { throw new RuntimeException(modules + " != " + expected); } }
Example #19
Source File: BasicLayerTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
/** * Attempt to create a layer with a module defined to the boot loader */ @Test(expectedExceptions = { LayerInstantiationException.class }) public void testLayerWithBootLoader() { ModuleDescriptor descriptor = newBuilder("m1").build(); ModuleFinder finder = ModuleUtils.finderOf(descriptor); Configuration cf = ModuleLayer.boot() .configuration() .resolve(finder, ModuleFinder.of(), Set.of("m1")); assertTrue(cf.modules().size() == 1); ModuleLayer.boot().defineModules(cf, mn -> null ); }
Example #20
Source File: AutomaticModulesTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
/** * Test mapping of JAR file names to module names */ @Test(dataProvider = "jarnames") public void testNames(String fn, String mid) throws IOException { String[] s = mid.split("/"); String mn = s[0]; String vs = (s.length == 2) ? s[1] : null; Path dir = Files.createTempDirectory(USER_DIR, "mods"); Path jf = dir.resolve(fn); // create empty JAR file createDummyJarFile(jf); // create a ModuleFinder to find modules in the directory ModuleFinder finder = ModuleFinder.of(dir); // a module with the expected name should be found Optional<ModuleReference> mref = finder.find(mn); assertTrue(mref.isPresent(), mn + " not found"); ModuleDescriptor descriptor = mref.get().descriptor(); assertTrue(descriptor.isAutomatic()); assertEquals(descriptor.name(), mn); if (vs == null) { assertFalse(descriptor.version().isPresent()); } else { assertEquals(descriptor.version().get().toString(), vs); } }
Example #21
Source File: ModuleDescriptorTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
public void testVersion2() { String vs = "1.0"; Version v1 = ModuleDescriptor.newModule("foo") .version(vs) .build() .version() .get(); Version v2 = Version.parse(vs); assertEquals(v1, v2); }
Example #22
Source File: JLinkTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
private static int getNumJlinkPlugins() { ModuleDescriptor desc = Plugin.class.getModule().getDescriptor(); return desc.provides().stream() .filter(p -> p.service().equals(Plugin.class.getName())) .map(p -> p.providers().size()) .findAny() .orElse(0); }
Example #23
Source File: Module.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
private Module(String name, URI location, ModuleDescriptor descriptor, Map<String, Set<String>> exports, Map<String, Set<String>> opens, boolean isSystem, ClassFileReader reader) { super(name, location, reader); this.descriptor = descriptor; this.location = location; this.exports = Collections.unmodifiableMap(exports); this.opens = Collections.unmodifiableMap(opens); this.isSystem = isSystem; }
Example #24
Source File: JLinkNegativeTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
public void testDuplicateModule1() throws IOException { String moduleName1 = "dupRes1Jmod1"; String moduleName2 = "dupRes1Jmod2"; List<String> classNames = Arrays.asList("java.A", "javax.B"); Path module1 = helper.generateModuleCompiledClasses( helper.getJmodSrcDir(), helper.getJmodClassesDir(), moduleName1, classNames); Path module2 = helper.generateModuleCompiledClasses( helper.getJmodSrcDir(), helper.getJmodClassesDir(), moduleName2, classNames); try (OutputStream out = Files.newOutputStream(module2.resolve("module-info.class"))) { ModuleInfoWriter.write(ModuleDescriptor.newModule(moduleName1) .requires("java.base").build(), out); } Path jmod1 = JImageGenerator.getJModTask() .addClassPath(module1) .jmod(helper.createNewJmodFile(moduleName1)) .create() .assertSuccess(); Path jmod2 = JImageGenerator.getJModTask() .addClassPath(module2) .jmod(helper.createNewJmodFile(moduleName2)) .create() .assertSuccess(); try { helper.generateDefaultImage(moduleName1) .assertFailure("Error: Two versions of module dupRes1Jmod1 found in"); } finally { deleteDirectory(jmod1); deleteDirectory(jmod2); } }
Example #25
Source File: JaasModularDefaultHandlerTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
/** * Generate modular/regular jar based on module type for this test. */ private void generateJar(boolean isService, MODULE_TYPE moduleType, Path jar, Path compilePath, boolean depends) throws IOException { ModuleDescriptor mDescriptor = null; if (isService) { mDescriptor = generateModuleDescriptor(isService, moduleType, S_PKG, S_PKG, null, null, null, M_REQUIRED, depends); } else { mDescriptor = generateModuleDescriptor(isService, moduleType, C_PKG, C_PKG, null, null, S_PKG, M_REQUIRED, depends); } generateJar(mDescriptor, jar, compilePath); }
Example #26
Source File: ModuleDescriptorTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
public void testPackagesAndOpensPackage1() { Set<String> packages = ModuleDescriptor.newModule("foo") .packages(Set.of("p")) .opens("p") .build() .packages(); assertTrue(packages.size() == 1); assertTrue(packages.contains("p")); }
Example #27
Source File: BasicLayerTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
/** * Parent of configuration != configuration of parent layer */ @Test(expectedExceptions = { IllegalArgumentException.class }) public void testIncorrectParent2() { ModuleDescriptor descriptor1 = newBuilder("m1").build(); ModuleFinder finder = ModuleUtils.finderOf(descriptor1); Configuration cf = resolve(finder, "m1"); ClassLoader loader = new ClassLoader() { }; ModuleLayer.boot().defineModules(cf, mn -> loader); }
Example #28
Source File: ModuleDescriptorTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
public void testOpensToString() { String s = ModuleDescriptor.newModule("foo") .opens("p1", Set.of("bar")) .build() .opens() .iterator() .next() .toString(); assertTrue(s.contains("p1")); assertTrue(s.contains("bar")); }
Example #29
Source File: ModuleInfoWriter.java From Bytecoder with Apache License 2.0 | 5 votes |
/** * Writes a module descriptor to the given output stream as a * module-info.class. */ public static void write(ModuleDescriptor descriptor, ModuleTarget target, OutputStream out) throws IOException { write(descriptor, null, target, out); }
Example #30
Source File: ModuleNamesTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
@Test(dataProvider = "legalModuleNames") public void testLegalModuleName(String mn, String expected) throws Exception { ModuleDescriptor md = newBuilder(mn).requires("java.base").build(); ByteBuffer bb = toBuffer(md); String name = ModuleDescriptor.read(bb).name(); assertEquals(name, expected); }