Java Code Examples for java.lang.module.ModuleDescriptor#read()
The following examples show how to use
java.lang.module.ModuleDescriptor#read() .
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: Jar.java From helidon-build-tools with Apache License 2.0 | 6 votes |
private Jar(Path path) { this.path = assertFile(path); // Absolute and normalized this.isJmod = fileName(path).endsWith(JMOD_SUFFIX); try { this.jar = new JarFile(path.toFile()); this.manifest = jar.getManifest(); this.isMultiRelease = !isJmod && isMultiRelease(manifest); this.isSigned = !isJmod && hasSignatureFile(); this.isBeansArchive = !isJmod && hasEntry(BEANS_RESOURCE_PATH); final Entry moduleInfo = findEntry(isJmod ? JMOD_CLASSES_PREFIX + MODULE_INFO_CLASS : MODULE_INFO_CLASS); if (moduleInfo != null) { this.descriptor = ModuleDescriptor.read(moduleInfo.data()); } else { this.descriptor = null; } this.resources = new AtomicReference<>(); } catch (IOException e) { throw new UncheckedIOException(e); } }
Example 2
Source File: Main.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
private boolean checkModuleInfo(byte[] moduleInfoBytes, Set<String> entries) throws IOException { boolean ok = true; if (moduleInfoBytes != null) { // no root module-info.class if null try { // ModuleDescriptor.read() checks open/exported pkgs vs packages ModuleDescriptor md = ModuleDescriptor.read(ByteBuffer.wrap(moduleInfoBytes)); // A module must have the implementation class of the services it 'provides'. if (md.provides().stream().map(Provides::providers).flatMap(List::stream) .filter(p -> !entries.contains(toBinaryName(p))) .peek(p -> fatalError(formatMsg("error.missing.provider", p))) .count() != 0) { ok = false; } } catch (InvalidModuleDescriptorException x) { fatalError(x.getMessage()); ok = false; } } return ok; }
Example 3
Source File: ModuleDescriptorTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
/** * Test ModuleDescriptor with a packager finder */ public void testReadsWithPackageFinder() throws Exception { ModuleDescriptor descriptor = ModuleDescriptor.newModule("foo") .requires("java.base") .build(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ModuleInfoWriter.write(descriptor, baos); ByteBuffer bb = ByteBuffer.wrap(baos.toByteArray()); descriptor = ModuleDescriptor.read(bb, () -> Set.of("p", "q")); assertTrue(descriptor.packages().size() == 2); assertTrue(descriptor.packages().contains("p")); assertTrue(descriptor.packages().contains("q")); }
Example 4
Source File: SystemModulesPlugin.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
/** * Returns the bytes for the module-info.class with ModulePackages * attribute added and/or with ModuleTarget attribute dropped. */ byte[] getBytes() throws IOException { try (InputStream in = getInputStream()) { if (shouldRewrite()) { ModuleInfoRewriter rewriter = new ModuleInfoRewriter(in); if (addModulePackages) { rewriter.addModulePackages(packages); } if (dropModuleTarget) { rewriter.dropModuleTarget(); } // rewritten module descriptor byte[] bytes = rewriter.getBytes(); try (ByteArrayInputStream bain = new ByteArrayInputStream(bytes)) { this.descriptor = ModuleDescriptor.read(bain); } return bytes; } else { return in.readAllBytes(); } } }
Example 5
Source File: Main.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
static void validate(Module module) throws IOException { ModuleDescriptor md = module.getDescriptor(); // read m1/module-info.class FileSystem fs = FileSystems.newFileSystem(URI.create("jrt:/"), Collections.emptyMap()); Path path = fs.getPath("/", "modules", module.getName(), "module-info.class"); ModuleDescriptor md1 = ModuleDescriptor.read(Files.newInputStream(path)); // check the module descriptor of a system module and read from jimage checkPackages(md.packages(), "p1", "p2"); checkPackages(md1.packages(), "p1", "p2"); try (InputStream in = Files.newInputStream(path)) { checkModuleTargetAttribute(in, "p1"); } }
Example 6
Source File: LayerAndLoadersTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
private String readModuleName(URL url) { try (InputStream in = url.openStream()) { ModuleDescriptor descriptor = ModuleDescriptor.read(in); return descriptor.name(); } catch (IOException ioe) { throw new UncheckedIOException(ioe); } }
Example 7
Source File: ModuleDescriptorTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
@Test(expectedExceptions = InvalidModuleDescriptorException.class) public void testReadWithNoRequiresBase() { ModuleDescriptor descriptor = SharedSecrets.getJavaLangModuleAccess() .newModuleBuilder("m1", false, Set.of()).requires("m2").build(); ByteBuffer bb = ModuleInfoWriter.toByteBuffer(descriptor); ModuleDescriptor.read(bb); }
Example 8
Source File: ModuleDescriptorTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
/** * Test ModuleDescriptor with a packager finder that doesn't return the * complete set of packages. */ @Test(expectedExceptions = InvalidModuleDescriptorException.class) public void testReadsWithBadPackageFinder() throws Exception { ModuleDescriptor descriptor = ModuleDescriptor.newModule("foo") .requires("java.base") .exports("p") .build(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ModuleInfoWriter.write(descriptor, baos); ByteBuffer bb = ByteBuffer.wrap(baos.toByteArray()); // package finder returns a set that doesn't include p ModuleDescriptor.read(bb, () -> Set.of("q")); }
Example 9
Source File: ModuleDescriptorTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
private ModuleDescriptor newModule(String name, String vs) { JavaLangModuleAccess JLMA = SharedSecrets.getJavaLangModuleAccess(); Builder builder = JLMA.newModuleBuilder(name, false, Set.of()); if (vs != null) builder.version(vs); builder.requires("java.base"); ByteBuffer bb = ModuleInfoWriter.toByteBuffer(builder.build()); return ModuleDescriptor.read(bb); }
Example 10
Source File: ModuleNamesTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
@Test(dataProvider = "illegalModuleNames", expectedExceptions = InvalidModuleDescriptorException.class) public void testIllegalOpens(String mn, String ignore) throws Exception { ModuleDescriptor md = newBuilder("m") .requires("java.base") .opens("p", Set.of(mn)) .build(); ByteBuffer bb = toBuffer(md); ModuleDescriptor.read(bb); // throws InvalidModuleDescriptorException }
Example 11
Source File: ModuleNamesTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
@Test(dataProvider = "illegalModuleNames", expectedExceptions = InvalidModuleDescriptorException.class) public void testIllegalExports(String mn, String ignore) throws Exception { ModuleDescriptor md = newBuilder("m") .requires("java.base") .exports("p", Set.of(mn)) .build(); ByteBuffer bb = toBuffer(md); ModuleDescriptor.read(bb); // throws InvalidModuleDescriptorException }
Example 12
Source File: ModuleNamesTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
@Test(dataProvider = "illegalModuleNames", expectedExceptions = InvalidModuleDescriptorException.class) public void testIllegalRequires(String mn, String ignore) throws Exception { ModuleDescriptor md = newBuilder("m").requires("java.base").requires(mn).build(); ByteBuffer bb = toBuffer(md); ModuleDescriptor.read(bb); // throws InvalidModuleDescriptorException }
Example 13
Source File: Basic.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
static ModuleDescriptor getModuleDescriptor(Path jar) { ClassLoader cl = ClassLoader.getSystemClassLoader(); try (JarFile jf = new JarFile(jar.toFile())) { JarEntry entry = jf.getJarEntry("module-info.class"); try (InputStream in = jf.getInputStream(entry)) { return ModuleDescriptor.read(in); } } catch (IOException ioe) { throw new UncheckedIOException(ioe); } }
Example 14
Source File: ModuleSorter.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
private ModuleDescriptor readModuleDescriptor(ResourcePoolModule module) { String p = "/" + module.name() + "/module-info.class"; ResourcePoolEntry content = module.findEntry(p).orElseThrow(() -> new PluginException("module-info.class not found for " + module.name() + " module") ); ByteBuffer bb = ByteBuffer.wrap(content.contentBytes()); return ModuleDescriptor.read(bb); }
Example 15
Source File: Main.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
/** * Adds extended modules attributes to the given module-info's. The given * Map values are updated in-place. Returns false if an error occurs. */ private void addExtendedModuleAttributes(Map<String,byte[]> moduleInfos, Set<String> packages) throws IOException { for (Map.Entry<String,byte[]> e: moduleInfos.entrySet()) { ModuleDescriptor md = ModuleDescriptor.read(ByteBuffer.wrap(e.getValue())); e.setValue(extendedInfoBytes(md, e.getValue(), packages)); } }
Example 16
Source File: FileUtils.java From update4j with Apache License 2.0 | 5 votes |
public static ModuleDescriptor deriveModuleDescriptor(Path jar, String filename, boolean readZip) throws IOException { if (!readZip) return primitiveModuleDescriptor(jar, filename); try (FileSystem zip = FileSystems.newFileSystem(jar, ClassLoader.getSystemClassLoader())) { Path moduleInfo = zip.getPath("/module-info.class"); if (Files.exists(moduleInfo)) { try (InputStream in = Files.newInputStream(moduleInfo)) { return ModuleDescriptor.read(in, () -> { try { Path root = zip.getPath("/"); return Files.walk(root) .filter(f -> !Files.isDirectory(f)) .map(f -> root.relativize(f)) .map(Path::toString) .map(FileUtils::toPackageName) .flatMap(Optional::stream) .collect(Collectors.toSet()); } catch (IOException e) { throw new UncheckedIOException(e); } }); } } return automaticModule(zip, filename); } }
Example 17
Source File: JavaRuntime.java From helidon-build-tools with Apache License 2.0 | 5 votes |
private Runtime.Version findVersion() { final Path javaBase = assertFile(jmodsDir.resolve(JAVA_BASE_JMOD)); try (ZipFile zip = new ZipFile(javaBase.toFile())) { final ZipEntry entry = zip.getEntry(JMOD_MODULE_INFO_PATH); if (entry == null) { throw new IllegalStateException("Cannot find " + JMOD_MODULE_INFO_PATH + " in " + javaBase); } final ModuleDescriptor descriptor = ModuleDescriptor.read(zip.getInputStream(entry)); return Runtime.Version.parse(descriptor.version() .orElseThrow(() -> new IllegalStateException("No version in " + javaBase)) .toString()); } catch (IOException e) { throw new UncheckedIOException(e); } }
Example 18
Source File: JmodTask.java From openjdk-jdk9 with GNU General Public License v2.0 | 4 votes |
/** * Writes the updated module-info.class to the ZIP output stream. * * The updated module-info.class will have a Packages attribute * with the set of module-private/non-exported packages. * * If --module-version, --main-class, or other options were provided * then the corresponding class file attributes are added to the * module-info here. */ void writeModuleInfo(JmodOutputStream out, Set<String> packages) throws IOException { Supplier<InputStream> miSupplier = newModuleInfoSupplier(); if (miSupplier == null) { throw new IOException(MODULE_INFO + " not found"); } ModuleDescriptor descriptor; try (InputStream in = miSupplier.get()) { descriptor = ModuleDescriptor.read(in); } // copy the module-info.class into the jmod with the additional // attributes for the version, main class and other meta data try (InputStream in = miSupplier.get()) { ModuleInfoExtender extender = ModuleInfoExtender.newExtender(in); // Add (or replace) the Packages attribute if (packages != null) { validatePackages(descriptor, packages); extender.packages(packages); } // --main-class if (mainClass != null) extender.mainClass(mainClass); // --target-platform if (targetPlatform != null) { extender.targetPlatform(targetPlatform); } // --module-version if (moduleVersion != null) extender.version(moduleVersion); // --hash-modules if (options.modulesToHash != null) { // To compute hashes, it creates a Configuration to resolve // a module graph. The post-resolution check requires // the packages in ModuleDescriptor be available for validation. ModuleDescriptor md; try (InputStream is = miSupplier.get()) { md = ModuleDescriptor.read(is, () -> packages); } ModuleHashes moduleHashes = computeHashes(md); if (moduleHashes != null) { extender.hashes(moduleHashes); } else { warning("warn.no.module.hashes", descriptor.name()); } } if (moduleResolution != null && moduleResolution.value() != 0) { extender.moduleResolution(moduleResolution); } // write the (possibly extended or modified) module-info.class out.writeEntry(extender.toByteArray(), Section.CLASSES, MODULE_INFO); } }
Example 19
Source File: ModuleDescriptorTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 4 votes |
@Test(expectedExceptions = InvalidModuleDescriptorException.class) public void testReadFromEmptyInputStream() throws Exception { ModuleDescriptor.read(EMPTY_INPUT_STREAM); }
Example 20
Source File: ModuleDescriptorTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 4 votes |
@Test(expectedExceptions = InvalidModuleDescriptorException.class) public void testReadFromEmptyBuffer() { ByteBuffer bb = ByteBuffer.allocate(0); ModuleDescriptor.read(bb); }