Java Code Examples for java.lang.module.ModuleDescriptor#newModule()

The following examples show how to use java.lang.module.ModuleDescriptor#newModule() . 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: 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 4
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 5
Source File: ModuleHelper.java    From pro with GNU General Public License v3.0 5 votes vote down vote up
private static ModuleDescriptor createModuleDescriptor(ModuleNode moduleNode, Path moduleInfoPath) throws IOException {
  var modifiers = (moduleNode.access & ACC_OPEN) != 0? Set.of(Modifier.OPEN): Set.<Modifier>of();
  var builder = ModuleDescriptor.newModule(moduleNode.name, modifiers);

  moduleNode.requires.forEach(require -> builder.requires(requireModifiers(require.access), require.module));
  moduleNode.exports.forEach(export -> {
    if (export.modules.isEmpty()) {
      builder.exports(export.packaze);
    } else {
      builder.exports(export.packaze, export.modules.stream().collect(toSet()));
    }
  });
  moduleNode.opens.forEach(open -> {
    if (open.modules.isEmpty()) {
      builder.opens(open.packaze);
    } else {
      builder.opens(open.packaze, open.modules.stream().collect(toSet()));
    }
  });
  moduleNode.uses.forEach(builder::uses);
  moduleNode.provides.forEach(provide -> builder.provides(provide.service, provide.providers));

  var moduleDirectory = moduleInfoPath.getParent();
  var javaPackages = findJavaPackages(moduleDirectory);
  javaPackages.removeAll(moduleNode.exports.stream().map(export -> export.packaze).collect(toList()));
  javaPackages.removeAll(moduleNode.opens.stream().map(export -> export.packaze).collect(toList()));
  builder.packages(javaPackages);

  ModuleDescriptor descriptor = builder.build();
  //System.out.println(descriptor.name() + " " + descriptor.packages());

  return descriptor;
}
 
Example 6
Source File: ModuleFixerPlugin.java    From pro with GNU General Public License v3.0 5 votes vote down vote up
private static Path generateModuleInfo(ModuleReference ref,
                                       Map<String, RequireModifier> requires,
                                       Set<String> exports, Set<String> uses,
                                       Map<String, Set<String>> provides,
                                       Optional<Version> versionOpt,
                                       Path moduleDependencyFixerPath) throws IOException {
  var moduleName = ref.descriptor().name();
  
  //System.out.println(moduleName);
  //System.out.println("requires: " + requires);
  //System.out.println("exports: " + exports);
  //System.out.println("uses: " + uses);
  //System.out.println("provides: " + provides);
  
  var modulePatchPath = moduleDependencyFixerPath.resolve(moduleName);
  Files.createDirectories(modulePatchPath);
  
  var builder = ModuleDescriptor.newModule(moduleName, Set.of(Modifier.OPEN));
  ref.descriptor().version().ifPresent(version -> builder.version(version.toString()));
  requires.forEach((require, requireModifier) -> builder.requires(requireModifier == RequireModifier.STATIC? EnumSet.of(Requires.Modifier.STATIC): Set.of(), require));
  exports.forEach(export -> builder.exports(export.replace('/', '.')));
  uses.forEach(use -> builder.uses(use.replace('/', '.')));
  provides.forEach((service, providers) -> builder.provides(service, new ArrayList<>(providers)));
  versionOpt.ifPresent(version -> builder.version(version));
  
  var generatedModuleInfoPath = modulePatchPath.resolve("module-info.class");
  Files.write(generatedModuleInfoPath, ModuleHelper.moduleDescriptorToBinary(builder.build()));
  return generatedModuleInfoPath;
}
 
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: ModuleAPIUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenModuleName_whenBuildingModuleDescriptor_thenBuilt() {
    Builder moduleBuilder = ModuleDescriptor.newModule("baeldung.base");

    ModuleDescriptor moduleDescriptor = moduleBuilder.build();

    assertThat(moduleDescriptor.name(), is("baeldung.base"));
}
 
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: ModuleDescriptorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Test(dataProvider = "invalidNames",
      expectedExceptions = IllegalArgumentException.class )
public void testBadModuleName(String mn, String ignore) {
    ModuleDescriptor.newModule(mn);
}
 
Example 11
Source File: ModuleDescriptorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions = IllegalArgumentException.class)
public void testNewModuleToBuildAutomaticAndOpenModule() {
    Set<ModuleDescriptor.Modifier> ms = Set.of(ModuleDescriptor.Modifier.AUTOMATIC,
                                               ModuleDescriptor.Modifier.OPEN);
    ModuleDescriptor.newModule("foo", ms);
}
 
Example 12
Source File: ModuleDescriptorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Test(dataProvider = "invalidNames",
      expectedExceptions = IllegalArgumentException.class )
public void testMainClassWithBadName(String mainClass, String ignore) {
    Builder builder = ModuleDescriptor.newModule("foo");
    builder.mainClass(mainClass);
}
 
Example 13
Source File: ModuleHelper.java    From pro with GNU General Public License v3.0 4 votes vote down vote up
public static ModuleDescriptor mergeModuleDescriptor(ModuleDescriptor sourceModule, ModuleDescriptor testModule) {
  var open = sourceModule.isOpen() || testModule.isOpen();

  var moduleModifiers = open? Set.of(Modifier.OPEN): Set.<Modifier>of();
  var builder = ModuleDescriptor.newModule(testModule.name(), moduleModifiers);

  var requires = merge(ModuleDescriptor::requires,
      Requires::name, Requires::modifiers, ModuleHelper::mergeRequiresModifiers, sourceModule, testModule);
  var exports = merge(ModuleDescriptor::exports,
      Exports::source, Exports::targets, ModuleHelper::mergeRestrictions, sourceModule, testModule);
  var packages = merge(ModuleDescriptor::packages,
      x -> x, x -> true, (_1, _2) -> true, sourceModule, testModule);
  var opens = merge(ModuleDescriptor::opens,
      Opens::source, Opens::targets, ModuleHelper::mergeRestrictions, sourceModule, testModule);
  var uses = merge(ModuleDescriptor::uses,
      x -> x, x -> true, (_1, _2) -> true, sourceModule, testModule);
  var provides = merge(ModuleDescriptor::provides,
      Provides::service, p -> new HashSet<>(p.providers()), ModuleHelper::mergeAll, sourceModule, testModule);

  requires.forEach((name, modifiers) -> builder.requires(modifiers, name));
  exports.forEach((source, target) -> {
    if (target.isEmpty()) {
      builder.exports(Set.of(), source);
    } else {
      builder.exports(source, target);
    }
  });
  packages.keySet().removeAll(exports.keySet());
  builder.packages(packages.keySet());
  opens.forEach((source, target) -> {
    if (target.isEmpty()) {
      builder.opens(Set.of(), source);
    } else {
      builder.opens(source, target);
    }
  });
  uses.keySet().forEach(builder::uses);
  provides.forEach((service, providers) -> builder.provides(service, providers.stream().collect(toUnmodifiableList())));

  return builder.build();
}
 
Example 14
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();

}