Java Code Examples for java.lang.module.ModuleReference#open()

The following examples show how to use java.lang.module.ModuleReference#open() . 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: BuiltinClassLoader.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the ModuleReader for the given module, creating it if needed.
 */
private ModuleReader moduleReaderFor(ModuleReference mref) {
    ModuleReader reader = moduleToReader.get(mref);
    if (reader == null) {
        // avoid method reference during startup
        Function<ModuleReference, ModuleReader> create = new Function<>() {
            public ModuleReader apply(ModuleReference moduleReference) {
                try {
                    return mref.open();
                } catch (IOException e) {
                    // Return a null module reader to avoid a future class
                    // load attempting to open the module again.
                    return new NullModuleReader();
                }
            }
        };
        reader = moduleToReader.computeIfAbsent(mref, create);
    }
    return reader;
}
 
Example 2
Source File: Main.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    String mn = args[0];

    ModuleReference mref = ModuleLayer.boot()
            .configuration()
            .findModule(mn)
            .map(ResolvedModule::reference)
            .orElseThrow(() -> new RuntimeException(mn + " not resolved!!"));

    try (ModuleReader reader = mref.open()) {
        reader.list().forEach(name -> {
            testFindUnchecked(name);

            // if the resource is a directory then find without trailing slash
            if (name.endsWith("/")) {
                testFindUnchecked(name.substring(0, name.length() - 1));
            }
        });
    }
}
 
Example 3
Source File: ModuleFixerPlugin.java    From pro with GNU General Public License v3.0 6 votes vote down vote up
private static void findModuleData(ModuleReference ref, Set<String> requirePackages,
                                   Set<String> exports, Set<String> uses, Map<String, Set<String>> provides,
                                   Map<String, Object> propertyMap) throws IOException {
  try(var reader = ref.open()) {
    try(var resources = reader.list()) {
      resources.forEach(suppress(resource -> {
        if (resource.endsWith(".class")) {
          scanJavaClass(resource, reader, requirePackages, exports, uses);  
        } else {
          if (resource.startsWith("META-INF/services/")) {
            // skip "META-INF/services/" directory resource entry
            if (resource.length() == "META-INF/services/".length()) {
              return;
            }
            scanServiceFile(resource, reader, provides);
          } else {
            if (resource.equals("META-INF/MANIFEST.MF")) {
              scanManifest(resource, reader, propertyMap);
            }
          }
        }
      })); 
    }
  }
  requirePackages.removeAll(exports);
}
 
Example 4
Source File: Loader.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a ModuleReader for the given module.
 */
private ModuleReader createModuleReader(ModuleReference mref) {
    try {
        return mref.open();
    } catch (IOException e) {
        // Return a null module reader to avoid a future class load
        // attempting to open the module again.
        return new NullModuleReader();
    }
}
 
Example 5
Source File: Loader.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a ModuleReader for the given module.
 */
private ModuleReader createModuleReader(ModuleReference mref) {
    try {
        return mref.open();
    } catch (IOException e) {
        // Return a null module reader to avoid a future class load
        // attempting to open the module again.
        return new NullModuleReader();
    }
}
 
Example 6
Source File: BuiltinClassLoader.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a ModuleReader for the given module.
 */
private static ModuleReader createModuleReader(ModuleReference mref) {
    try {
        return mref.open();
    } catch (IOException e) {
        // Return a null module reader to avoid a future class load
        // attempting to open the module again.
        return new NullModuleReader();
    }
}
 
Example 7
Source File: ModulesInCustomFileSystem.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * List all modules that the finder finds and the resources in the module.
 */
private void listAllModules(ModuleFinder finder) throws Exception {
    for (ModuleReference mref : finder.findAll()) {
        System.out.println(mref.descriptor());
        try (ModuleReader reader = mref.open()) {
            reader.list().forEach(name -> System.out.format("  %s%n", name));
        }
    }
}
 
Example 8
Source File: ModuleTargetHelper.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static ModuleTarget read(ModuleReference modRef) throws IOException {
    ModuleReader reader = modRef.open();
    try (InputStream in = reader.open("module-info.class").get()) {
        return read(in);
    } finally {
        reader.close();
    }
}
 
Example 9
Source File: ModuleLocationFactory.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
private static <T> T doWithModuleReader(ModuleReference moduleReference, ModuleReaderProcessor<T> readerProcessor) {
    try (ModuleReader moduleReader = moduleReference.open()) {
        return readerProcessor.process(moduleReader);
    } catch (IOException e) {
        String message = String.format("Unexpected error while processing module %s", moduleReference);
        throw new RuntimeException(message, e);
    }
}
 
Example 10
Source File: MultiReleaseJarTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Exercise ModuleReader on a multi-release JAR
 */
public void testModuleReader() throws Exception {
    String name = "m1";

    ModuleDescriptor descriptor1 = ModuleDescriptor.newModule(name)
            .requires("java.base")
            .build();

    // module descriptor for versioned section
    ModuleDescriptor descriptor2 = ModuleDescriptor.newModule(name)
            .requires("java.base")
            .requires("jdk.unsupported")
            .build();

    Path jar = new JarBuilder(name)
            .moduleInfo(MODULE_INFO, descriptor1)
            .moduleInfo("META-INF/versions/" + VERSION + "/" + MODULE_INFO, descriptor2)
            .build();

    // find the module
    ModuleFinder finder = ModuleFinder.of(jar);
    Optional<ModuleReference> omref = finder.find(name);
    assertTrue((omref.isPresent()));
    ModuleReference mref = omref.get();

    ModuleDescriptor expected;
    if (MULTI_RELEASE) {
        expected = descriptor2;
    } else {
        expected = descriptor1;
    }

    // test ModuleReader by reading module-info.class resource
    try (ModuleReader reader = mref.open()) {

        // open resource
        Optional<InputStream> oin = reader.open(MODULE_INFO);
        assertTrue(oin.isPresent());
        try (InputStream in = oin.get()) {
            checkRequires(ModuleDescriptor.read(in), expected);
        }

        // read resource
        Optional<ByteBuffer> obb = reader.read(MODULE_INFO);
        assertTrue(obb.isPresent());
        ByteBuffer bb = obb.get();
        try {
            checkRequires(ModuleDescriptor.read(bb), expected);
        } finally {
            reader.release(bb);
        }

        // find resource
        Optional<URI> ouri = reader.find(MODULE_INFO);
        assertTrue(ouri.isPresent());
        URI uri = ouri.get();

        String expectedTail = "!/";
        if (MULTI_RELEASE)
            expectedTail += "META-INF/versions/" + VERSION + "/";
        expectedTail += MODULE_INFO;
        assertTrue(uri.toString().endsWith(expectedTail));

        URLConnection uc = uri.toURL().openConnection();
        uc.setUseCaches(false);
        try (InputStream in = uc.getInputStream()) {
            checkRequires(ModuleDescriptor.read(in), expected);
        }

    }
}