Java Code Examples for java.lang.module.ModuleDescriptor#Builder

The following examples show how to use java.lang.module.ModuleDescriptor#Builder . 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: ModuleAnalyzer.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private ModuleDescriptor descriptor(Set<Module> requiresTransitive,
                                    Set<Module> requires) {

    ModuleDescriptor.Builder builder = ModuleDescriptor.newModule(root.name());

    if (!root.name().equals(JAVA_BASE))
        builder.requires(Set.of(MANDATED), JAVA_BASE);

    requiresTransitive.stream()
        .filter(m -> !m.name().equals(JAVA_BASE))
        .map(Module::name)
        .forEach(mn -> builder.requires(Set.of(TRANSITIVE), mn));

    requires.stream()
        .filter(m -> !requiresTransitive.contains(m))
        .filter(m -> !m.name().equals(JAVA_BASE))
        .map(Module::name)
        .forEach(mn -> builder.requires(mn));

    return builder.build();
}
 
Example 2
Source File: Module.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Converts the given automatic module to a normal module.
 *
 * Replace this module's dependences with the given requires and also
 * declare service providers, if specified in META-INF/services configuration file
 */
private NormalModule(Module m, Map<String, Boolean> requires) {
    super(m.name(), m.location, m.descriptor, m.exports, m.opens, m.isSystem, m.reader());

    ModuleDescriptor.Builder builder = ModuleDescriptor.newModule(m.name());
    requires.keySet().forEach(mn -> {
        if (requires.get(mn).equals(Boolean.TRUE)) {
            builder.requires(Set.of(ModuleDescriptor.Requires.Modifier.TRANSITIVE), mn);
        } else {
            builder.requires(mn);
        }
    });
    // exports all packages
    m.descriptor.packages().forEach(builder::exports);
    m.descriptor.uses().forEach(builder::uses);
    m.descriptor.provides().forEach(builder::provides);
    this.md = builder.build();
}
 
Example 3
Source File: ScriptLoader.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private Module createModule(final String moduleName) {
    final Module structMod = context.getStructLoader().getModule();
    final ModuleDescriptor.Builder builder =
        ModuleDescriptor.newModule(moduleName, Set.of(Modifier.SYNTHETIC))
                .requires("java.logging")
                .requires(NASHORN_MODULE.getName())
                .requires(structMod.getName())
                .packages(Set.of(SCRIPTS_PKG));

    if (Context.javaSqlFound) {
        builder.requires("java.sql");
    }

    if (Context.javaSqlRowsetFound) {
        builder.requires("java.sql.rowset");
    }

    final ModuleDescriptor descriptor = builder.build();

    final Module mod = Context.createModuleTrusted(structMod.getLayer(), descriptor, this);
    loadModuleManipulator();
    return mod;
}
 
Example 4
Source File: ResourcePoolConfiguration.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private static ModuleDescriptor descriptorOf(ResourcePoolModule mod) {
    ModuleDescriptor md = mod.descriptor();

    // drop hashes
    ModuleDescriptor.Builder builder = ModuleDescriptor.newModule(md.name());
    md.requires().stream()
      .forEach(builder::requires);
    md.exports().stream()
      .forEach(builder::exports);
    md.opens().stream()
      .forEach(builder::opens);
    md.uses().stream()
      .forEach(builder::uses);
    md.provides().stream()
      .forEach(builder::provides);
    builder.packages(md.packages());

    md.version().ifPresent(builder::version);
    md.mainClass().ifPresent(builder::mainClass);

    return builder.build();
}
 
Example 5
Source File: ModuleFinderTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Parses a string of the form {@code name[@version]} and returns a
 * ModuleDescriptor with that name and version. The ModuleDescriptor
 * will have a requires on java.base.
 */
static ModuleDescriptor newModuleDescriptor(String mid) {
    String mn;
    String vs;
    int i = mid.indexOf("@");
    if (i == -1) {
        mn = mid;
        vs = null;
    } else {
        mn = mid.substring(0, i);
        vs = mid.substring(i+1);
    }
    ModuleDescriptor.Builder builder
        = ModuleDescriptor.newModule(mn).requires("java.base");
    if (vs != null)
        builder.version(vs);
    return builder.build();
}
 
Example 6
Source File: JdepsConfiguration.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
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 7
Source File: ModuleBuilder.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
Module defineModule(String moduleName, ClassLoader classLoader, URL[] urls) {
    if (moduleName == null) {
        throw new NullPointerException("moduleName");
    }
    if (urls == null) {
        throw new NullPointerException("urls");
    }
    if (urls.length == 0) {
        throw new IllegalArgumentException("urls.length is 0");
    }
    logger.info("bootstrap unnamedModule:" +  BootLoader.getUnnamedModule());
    logger.info("platform unnamedModule:" + ClassLoader.getPlatformClassLoader().getUnnamedModule());
    logger.info("system unnamedModule:" + ClassLoader.getSystemClassLoader().getUnnamedModule());

    Module unnamedModule = classLoader.getUnnamedModule();
    logger.info("defineModule classLoader: " + classLoader);
    logger.info("defineModule classLoader-unnamedModule: " + unnamedModule);


    List<PackageInfo> packageInfos = parsePackageInfo(urls);
    Set<String> packages = mergePackageInfo(packageInfos);
    logger.info("packages:" + packages);
    Map<String, Set<String>> serviceInfoMap = mergeServiceInfo(packageInfos);
    logger.info("providers:" + serviceInfoMap);

    ModuleDescriptor.Builder builder = ModuleDescriptor.newModule(moduleName);
    builder.packages(packages);
    for (Map.Entry<String, Set<String>> entry : serviceInfoMap.entrySet()) {
        builder.provides(entry.getKey(), new ArrayList<>(entry.getValue()));
    }

    ModuleDescriptor moduleDescriptor = builder.build();
    URI url = getInformationURI(urls);

    Module module = Modules.defineModule(classLoader, moduleDescriptor , url);
    logger.info("defineModule module:" + module);
    return module;
}
 
Example 8
Source File: JavaLangModuleAccess.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
/**
 * Adds a dependence on a module with the given (possibly un-parsable)
 * version string.
 */
void requires(ModuleDescriptor.Builder builder,
              Set<Requires.Modifier> ms,
              String mn,
              String rawCompiledVersion);
 
Example 9
Source File: ModuleExportsAnalyzer.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
RootModule(String name) {
    super(name);

    ModuleDescriptor.Builder builder = ModuleDescriptor.newModule(name);
    this.descriptor = builder.build();
}
 
Example 10
Source File: JavaLangModuleAccess.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Adds a dependence on a module with the given (possibly un-parsable)
 * version string.
 */
void requires(ModuleDescriptor.Builder builder,
              Set<Requires.Modifier> ms,
              String mn,
              String rawCompiledVersion);
 
Example 11
Source File: ConfigurationTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Creates a "non-strict" builder for building a module. This allows the
 * test the create ModuleDescriptor objects that do not require java.base.
 */
private static ModuleDescriptor.Builder newBuilder(String mn) {
    return SharedSecrets.getJavaLangModuleAccess()
            .newModuleBuilder(mn, false, Set.of());
}
 
Example 12
Source File: BasicLayerTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Creates a "non-strict" builder for building a module. This allows the
 * test the create ModuleDescriptor objects that do not require java.base.
 */
private static ModuleDescriptor.Builder newBuilder(String mn) {
    return SharedSecrets.getJavaLangModuleAccess()
            .newModuleBuilder(mn, false, Set.of());
}
 
Example 13
Source File: ModuleBuilderTest.java    From pinpoint with Apache License 2.0 3 votes vote down vote up
@Test
public void defineModuleTest() throws IOException, URISyntaxException, ClassNotFoundException {
    URL testClassJar = CodeSourceUtils.getCodeLocation(slf4jClass);
    URL[] urls = {testClassJar};

    String moduleName = "slf4j.test";


    JarFile jarFile = new JarFile(testClassJar.getFile());
    PackageAnalyzer packageAnalyzer = new JarFileAnalyzer(jarFile);
    PackageInfo packageInfo = packageAnalyzer.analyze();
    Set<String> slf4j = packageInfo.getPackage();
    logger.debug("slf4j packages:{}", slf4j);

    Java9ClassLoader classLoader = new Java9ClassLoader(moduleName, urls, this.getClass().getClassLoader(), new ArrayList<>(slf4j));

    ModuleDescriptor.Builder builder = ModuleDescriptor.newModule(moduleName);
    builder.packages(slf4j);
    ModuleDescriptor descriptor = builder.build();


    Module module = Modules.defineModule(classLoader, descriptor, testClassJar.toURI());
    logger.debug("module:{}", module);

    Class<?> slf4jModule = classLoader.loadClass(slf4jClass.getName());
    logger.debug("slf4j:{}", slf4jModule);

    Assert.assertSame(module, slf4jModule.getModule());
    Assert.assertEquals(module.getName(), slf4jModule.getModule().getName());

    classLoader.close();

}
 
Example 14
Source File: JavaLangModuleAccess.java    From openjdk-jdk9 with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Returns a snapshot of the packages in the module.
 */
Set<String> packages(ModuleDescriptor.Builder builder);
 
Example 15
Source File: JavaLangModuleAccess.java    From Bytecoder with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a builder for building a module with the given module name.
 *
 * @param strict
 *        Indicates whether module names are checked or not
 */
ModuleDescriptor.Builder newModuleBuilder(String mn,
                                          boolean strict,
                                          Set<ModuleDescriptor.Modifier> ms);
 
Example 16
Source File: JavaLangModuleAccess.java    From openjdk-jdk9 with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Creates a builder for building a module with the given module name.
 *
 * @param strict
 *        Indicates whether module names are checked or not
 */
ModuleDescriptor.Builder newModuleBuilder(String mn,
                                          boolean strict,
                                          Set<ModuleDescriptor.Modifier> ms);
 
Example 17
Source File: JavaLangModuleAccess.java    From Bytecoder with Apache License 2.0 2 votes vote down vote up
/**
 * Returns a snapshot of the packages in the module.
 */
Set<String> packages(ModuleDescriptor.Builder builder);