java.lang.module.ModuleFinder Java Examples

The following examples show how to use java.lang.module.ModuleFinder. 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: ModuleFinderTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Test ModuleFinder.of with a directory containing hidden files
 */
public void testOfWithHiddenFiles() throws Exception {
    Path dir = Files.createTempDirectory(USER_DIR, "mods");
    createExplodedModule(dir.resolve("m"), "m",
            "com/.ignore",
            "com/foo/.ignore",
            "com/foo/foo.properties");

    ModuleFinder finder = ModuleFinder.of(dir);
    ModuleReference mref = finder.find("m").orElse(null);
    assertNotNull(mref);

    Set<String> expectedPackages;
    if (System.getProperty("os.name").startsWith("Windows")) {
        expectedPackages = Set.of("com", "com.foo");
    } else {
        expectedPackages = Set.of("com.foo");
    }
    assertEquals(mref.descriptor().packages(), expectedPackages);
}
 
Example #2
Source File: AutomaticModulesTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Test JAR file with META-INF/services configuration file listing a
 * provider that is not in the module.
 */
@Test(expectedExceptions = FindException.class)
public void testMissingProviderPackage() throws IOException {
    Path tmpdir = Files.createTempDirectory(USER_DIR, "tmp");

    // services configuration file
    Path services = tmpdir.resolve("META-INF").resolve("services");
    Files.createDirectories(services);
    Files.write(services.resolve("p.S"), Set.of("q.P"));

    Path dir = Files.createTempDirectory(USER_DIR, "mods");
    JarUtils.createJarFile(dir.resolve("m.jar"), tmpdir);

    // should throw FindException
    ModuleFinder.of(dir).findAll();
}
 
Example #3
Source File: AutomaticModulesTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Test JAR file with META-INF/services configuration file with bad
 * values or names.
 */
@Test(dataProvider = "badservices")
public void testBadServicesNames(String service, String provider)
    throws IOException
{
    Path tmpdir = Files.createTempDirectory(USER_DIR, "tmp");
    Path services = tmpdir.resolve("META-INF").resolve("services");
    Files.createDirectories(services);
    Files.write(services.resolve(service), Set.of(provider));
    Path dir = Files.createTempDirectory(USER_DIR, "mods");
    JarUtils.createJarFile(dir.resolve("m.jar"), tmpdir);

    Optional<ModuleReference> omref = ModuleFinder.of(dir).find("m");
    assertTrue(omref.isPresent());
    ModuleDescriptor descriptor = omref.get().descriptor();
    assertTrue(descriptor.provides().isEmpty());
}
 
Example #4
Source File: AutomaticModulesTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Test JAR file with META-INF/services configuration file with bad
 * values or names.
 */
@Test(dataProvider = "badproviders", expectedExceptions = FindException.class)
public void testBadProviderNames(String service, String provider)
    throws IOException
{
    Path tmpdir = Files.createTempDirectory(USER_DIR, "tmp");

    // provider class
    Path providerClass = tmpdir.resolve(provider.replace('.', '/') + ".class");
    Files.createDirectories(providerClass.getParent());
    Files.createFile(providerClass);

    // services configuration file
    Path services = tmpdir.resolve("META-INF").resolve("services");
    Files.createDirectories(services);
    Files.write(services.resolve(service), Set.of(provider));

    Path dir = Files.createTempDirectory(USER_DIR, "mods");
    JarUtils.createJarFile(dir.resolve("m.jar"), tmpdir);

    // should throw FindException
    ModuleFinder.of(dir).findAll();
}
 
Example #5
Source File: BasicLayerTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Attempt to use defineModules to create a layer with a module
 * containing a package in which a type is already loaded by the class
 * loader.
 */
@Test(expectedExceptions = { LayerInstantiationException.class })
public void testPackageAlreadyInUnnamedModule() throws Exception {

    Class<?> c = layertest.Test.class;
    assertFalse(c.getModule().isNamed());  // in unnamed module

    ModuleDescriptor md = newBuilder("m")
            .packages(Set.of(c.getPackageName()))
            .requires("java.base")
            .build();

    ModuleFinder finder = ModuleUtils.finderOf(md);

    Configuration parent = ModuleLayer.boot().configuration();
    Configuration cf = parent.resolve(finder, ModuleFinder.of(), Set.of("m"));

    ModuleLayer.boot().defineModules(cf, mn -> c.getClassLoader());
}
 
Example #6
Source File: LayerAndLoadersTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns a ModuleFinder that only finds the given test modules
 */
static ModuleFinder finderFor(String... names) {

    ModuleFinder finder = ModuleFinder.of(MODS_DIR);

    Map<String, ModuleReference> mrefs = new HashMap<>();
    for (String name : names) {
        Optional<ModuleReference> omref = finder.find(name);
        assert omref.isPresent();
        mrefs.put(name, omref.get());
    }

    return new ModuleFinder() {
        @Override
        public Optional<ModuleReference> find(String name) {
            ModuleReference mref = mrefs.get(name);
            return Optional.ofNullable(mref);
        }
        @Override
        public Set<ModuleReference> findAll() {
            return mrefs.values().stream().collect(Collectors.toSet());
        }
    };
}
 
Example #7
Source File: LauncherHelper.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Scan a JAR file or exploded module.
 */
private Optional<ModuleReference> scanModule(Path entry) {
    ModuleFinder finder = ModuleFinder.of(entry);
    try {
        return finder.findAll().stream().findFirst();
    } catch (FindException e) {
        ostream.println(entry);
        ostream.println(INDENT + e.getMessage());
        Throwable cause = e.getCause();
        if (cause != null) {
            ostream.println(INDENT + cause);
        }
        errorFound = true;
        return Optional.empty();
    }
}
 
Example #8
Source File: BasicLayerTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
  * Exercise defineModules with a configuration with a module that
  * contains a package that is the same name as a non-exported package in
  * a parent layer.
  */
 public void testContainsSamePackageAsBootLayer() {

     // check assumption that java.base contains sun.launcher
     ModuleDescriptor base = Object.class.getModule().getDescriptor();
     assertTrue(base.packages().contains("sun.launcher"));

     ModuleDescriptor descriptor = newBuilder("m1")
            .requires("java.base")
            .packages(Set.of("sun.launcher"))
            .build();

     ModuleFinder finder = ModuleUtils.finderOf(descriptor);

     Configuration parent = ModuleLayer.boot().configuration();
     Configuration cf = parent.resolve(finder, ModuleFinder.of(), Set.of("m1"));
     assertTrue(cf.modules().size() == 1);

     ClassLoader loader = new ClassLoader() { };
     ModuleLayer layer = ModuleLayer.boot().defineModules(cf, mn -> loader);
     assertTrue(layer.modules().size() == 1);
}
 
Example #9
Source File: ConfigurationTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Basic test to detect reading a module with the same name as itself
 *
 * The test consists of three configurations:
 * - Configuration cf1: m1, m2 requires transitive m1
 * - Configuration cf2: m1 requires m2
 */
@Test(expectedExceptions = { ResolutionException.class })
public void testReadModuleWithSameNameAsSelf() {
    ModuleDescriptor descriptor1_v1 = newBuilder("m1")
            .build();

    ModuleDescriptor descriptor2 = newBuilder("m2")
            .requires(Set.of(Requires.Modifier.TRANSITIVE), "m1")
            .build();

    ModuleDescriptor descriptor1_v2 = newBuilder("m1")
            .requires("m2")
            .build();

    ModuleFinder finder1 = ModuleUtils.finderOf(descriptor1_v1, descriptor2);
    Configuration cf1 = resolve(finder1, "m2");
    assertTrue(cf1.modules().size() == 2);

    // resolve should throw ResolutionException
    ModuleFinder finder2 = ModuleUtils.finderOf(descriptor1_v2);
    resolve(cf1, finder2, "m1");
}
 
Example #10
Source File: ModuleFinderTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Test ModuleFinder with an exploded module containing a mix of class
 * and non-class resources
 */
public void testOfOneExplodedModuleWithResources() throws Exception {
    Path dir = Files.createTempDirectory(USER_DIR, "mods");
    Path m_dir = createExplodedModule(dir.resolve("m"), "m",
            "LICENSE",
            "README",
            "WEB-INF/tags",
            "p/Type.class",
            "p/resources/m.properties",
            "q-/Type.class",                 // not a legal package name
            "q-/resources/m/properties");

    ModuleFinder finder = ModuleFinder.of(m_dir);
    Optional<ModuleReference> mref = finder.find("m");
    assertTrue(mref.isPresent(), "m not found");

    ModuleDescriptor descriptor = mref.get().descriptor();

    assertTrue(descriptor.packages().size() == 2);
    assertTrue(descriptor.packages().contains("p"));
    assertTrue(descriptor.packages().contains("p.resources"));
}
 
Example #11
Source File: KullaTesting.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public ClassLoader createAndRunFromModule(String moduleName, Path modPath) {
    ModuleFinder finder = ModuleFinder.of(modPath);
    ModuleLayer parent = ModuleLayer.boot();
    Configuration cf = parent.configuration()
            .resolve(finder, ModuleFinder.of(), Set.of(moduleName));
    ClassLoader scl = ClassLoader.getSystemClassLoader();
    ModuleLayer layer = parent.defineModulesWithOneLoader(cf, scl);
    ClassLoader loader = layer.findLoader(moduleName);
    ClassLoader ccl = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(loader);
    return ccl;
}
 
Example #12
Source File: ModuleFinderTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Test ModuleFinder.compose with two module finders
 */
public void testComposeOfTwo() throws Exception {
    Path dir1 = Files.createTempDirectory(USER_DIR, "mods1");
    createModularJar(dir1.resolve("m1.jar"), "[email protected]");
    createModularJar(dir1.resolve("m2.jar"), "[email protected]");

    Path dir2 = Files.createTempDirectory(USER_DIR, "mods2");
    createModularJar(dir2.resolve("m1.jar"), "[email protected]");
    createModularJar(dir2.resolve("m2.jar"), "[email protected]");
    createModularJar(dir2.resolve("m3.jar"), "m3");
    createModularJar(dir2.resolve("m4.jar"), "m4");

    ModuleFinder finder1 = ModuleFinder.of(dir1);
    ModuleFinder finder2 = ModuleFinder.of(dir2);

    ModuleFinder finder = ModuleFinder.compose(finder1, finder2);
    assertTrue(finder.findAll().size() == 4);
    assertTrue(finder.find("m1").isPresent());
    assertTrue(finder.find("m2").isPresent());
    assertTrue(finder.find("m3").isPresent());
    assertTrue(finder.find("m4").isPresent());
    assertFalse(finder.find("java.rhubarb").isPresent());

    // check that [email protected] is found
    ModuleDescriptor m1 = finder.find("m1").get().descriptor();
    assertEquals(m1.version().get().toString(), "1.0");

    // check that [email protected] is found
    ModuleDescriptor m2 = finder.find("m2").get().descriptor();
    assertEquals(m2.version().get().toString(), "1.0");
}
 
Example #13
Source File: ModuleInfoBuilder.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public ModuleInfoBuilder(JdepsConfiguration configuration,
                         List<String> args,
                         Path outputdir,
                         boolean open) {
    this.configuration = configuration;
    this.outputdir = outputdir;
    this.open = open;

    this.dependencyFinder = new DependencyFinder(configuration, DEFAULT_FILTER);
    this.analyzer = new Analyzer(configuration, Type.CLASS, DEFAULT_FILTER);

    // add targets to modulepath if it has module-info.class
    List<Path> paths = args.stream()
        .map(fn -> Paths.get(fn))
        .collect(toList());

    // automatic module to convert to normal module
    this.automaticToNormalModule = ModuleFinder.of(paths.toArray(new Path[0]))
            .findAll().stream()
            .map(configuration::toModule)
            .collect(toMap(Function.identity(), Function.identity()));

    Optional<Module> om = automaticToNormalModule.keySet().stream()
                                .filter(m -> !m.descriptor().isAutomatic())
                                .findAny();
    if (om.isPresent()) {
        throw new UncheckedBadArgs(new BadArgs("err.genmoduleinfo.not.jarfile",
                                               om.get().getPathName()));
    }
    if (automaticToNormalModule.isEmpty()) {
        throw new UncheckedBadArgs(new BadArgs("err.invalid.path", args));
    }
}
 
Example #14
Source File: ConfigurationTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Basic test of binding services
 *     m1 uses p.S
 *     m2 provides p.S
 */
public void testServiceBinding1() {

    ModuleDescriptor descriptor1 = newBuilder("m1")
            .exports("p")
            .uses("p.S")
            .build();

    ModuleDescriptor descriptor2 = newBuilder("m2")
            .requires("m1")
            .provides("p.S", List.of("q.T"))
            .build();

    ModuleFinder finder = ModuleUtils.finderOf(descriptor1, descriptor2);

    Configuration cf = resolveAndBind(finder, "m1");

    assertTrue(cf.modules().size() == 2);
    assertTrue(cf.findModule("m1").isPresent());
    assertTrue(cf.findModule("m2").isPresent());
    assertTrue(cf.parents().size() == 1);
    assertTrue(cf.parents().get(0) == Configuration.empty());

    ResolvedModule m1 = cf.findModule("m1").get();
    ResolvedModule m2 = cf.findModule("m2").get();

    assertTrue(m1.configuration() == cf);
    assertTrue(m1.reads().size() == 0);

    assertTrue(m2.configuration() == cf);
    assertTrue(m2.reads().size() == 1);
    assertTrue(m2.reads().contains(m1));
}
 
Example #15
Source File: JlinkTask.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private JlinkConfiguration initJlinkConfig() throws BadArgs {
    Set<String> roots = new HashSet<>();
    for (String mod : options.addMods) {
        if (mod.equals(ALL_MODULE_PATH)) {
            Path[] entries = options.modulePath.toArray(new Path[0]);
            ModuleFinder finder = ModulePath.of(Runtime.version(), true, entries);
            if (!options.limitMods.isEmpty()) {
                // finder for the observable modules specified in
                // the --module-path and --limit-modules options
                finder = limitFinder(finder, options.limitMods, Collections.emptySet());
            }

            // all observable modules are roots
            finder.findAll()
                  .stream()
                  .map(ModuleReference::descriptor)
                  .map(ModuleDescriptor::name)
                  .forEach(mn -> roots.add(mn));
        } else {
            roots.add(mod);
        }
    }

    return new JlinkConfiguration(options.output,
                                  options.modulePath,
                                  roots,
                                  options.limitMods,
                                  options.endian);
}
 
Example #16
Source File: ConfigurationTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Basic test of using the beforeFinder to override a module in a parent
 * configuration.
 */
public void testOverriding1() {
    ModuleDescriptor descriptor1 = newBuilder("m1").build();

    ModuleFinder finder = ModuleUtils.finderOf(descriptor1);

    Configuration cf1 = resolve(finder, "m1");
    assertTrue(cf1.modules().size() == 1);
    assertTrue(cf1.findModule("m1").isPresent());

    Configuration cf2 = resolve(cf1, finder, "m1");
    assertTrue(cf2.modules().size() == 1);
    assertTrue(cf2.findModule("m1").isPresent());
}
 
Example #17
Source File: SystemModulesTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testSystemModules() {
    Path jimage = Paths.get(System.getProperty("java.home"), "lib", "modules");
    if (Files.notExists(jimage))
        return;

    ModuleFinder.ofSystem().findAll().stream()
                .forEach(this::checkAttributes);
}
 
Example #18
Source File: ConfigurationTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Test "provides p.S" where p is contained in the same module.
 */
public void testContainsService3() {
    ModuleDescriptor descriptor1 = newBuilder("m1")
            .packages(Set.of("p", "q"))
            .provides("p.S", List.of("q.S1"))
            .build();

    ModuleFinder finder = ModuleUtils.finderOf(descriptor1);

    Configuration cf = resolve(finder, "m1");

    assertTrue(cf.modules().size() == 1);
    assertTrue(cf.findModule("m1").isPresent());
}
 
Example #19
Source File: MultiReleaseJarTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Basic test of a multi-release JAR.
 */
public void testBasic() throws Exception {
    String name = "m1";

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

    Path jar = new JarBuilder(name)
            .moduleInfo("module-info.class", descriptor)
            .resource("p/Main.class")
            .resource("p/Helper.class")
            .resource("META-INF/versions/" + VERSION + "/p/Helper.class")
            .resource("META-INF/versions/" + VERSION + "/p/internal/Helper.class")
            .build();

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

    // check module packages
    descriptor = mref.descriptor();
    Set<String> packages = descriptor.packages();
    assertTrue(packages.contains("p"));
    if (MULTI_RELEASE) {
        assertTrue(packages.size() == 2);
        assertTrue(packages.contains("p.internal"));
    } else {
        assertTrue(packages.size() == 1);
    }
}
 
Example #20
Source File: AutomaticModulesTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Test impossible mapping of JAR files to modules names
 */
@Test(dataProvider = "badjarnames", expectedExceptions = FindException.class)
public void testBadNames(String fn, String ignore) throws IOException {
    Path dir = Files.createTempDirectory(USER_DIR, "mods");
    Path jf = dir.resolve(fn);

    // create empty JAR file
    createDummyJarFile(jf);

    // should throw FindException
    ModuleFinder.of(dir).findAll();
}
 
Example #21
Source File: ModuleLocationResolver.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Override
public UrlSource resolveClassPath() {
    Iterable<URL> classpath = UrlSource.From.classPathSystemProperties();
    Iterable<URL> modulepath = ModuleFinder.ofSystem().findAll().stream()
            .flatMap(moduleReference -> moduleReference.location().stream())
            .map(this::toUrl)
            .collect(toList());

    return UrlSource.From.iterable(concat(classpath, modulepath));
}
 
Example #22
Source File: LayerAndLoadersTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Test ModuleLayer.defineModulesWithXXX when the modules that override same
 * named modules in the parent layer.
 *
 * layer1: m1, m2, m3 => same loader
 * layer2: m1, m3 => same loader
 */
public void testOverriding3() throws Exception {
    Configuration cf1 = resolve("m1");

    ModuleLayer layer1 = ModuleLayer.boot().defineModulesWithOneLoader(cf1, null);
    checkLayer(layer1, "m1", "m2", "m3");

    ModuleFinder finder = finderFor("m1", "m3");

    Configuration cf2 = cf1.resolve(finder, ModuleFinder.of(),
                                            Set.of("m1"));

    ModuleLayer layer2 = layer1.defineModulesWithOneLoader(cf2, null);
    checkLayer(layer2, "m1", "m3");
    invoke(layer1, "m1", "p.Main");

    ClassLoader loader1 = layer1.findLoader("m1");
    ClassLoader loader2 = layer2.findLoader("m1");

    assertTrue(loader1.loadClass("p.Main").getClassLoader() == loader1);
    assertTrue(loader1.loadClass("q.Hello").getClassLoader() == loader1);
    assertTrue(loader1.loadClass("w.Hello").getClassLoader() == loader1);

    assertTrue(loader2.loadClass("p.Main").getClassLoader() == loader2);
    assertTrue(loader2.loadClass("q.Hello").getClassLoader() == loader1);
    assertTrue(loader2.loadClass("w.Hello").getClassLoader() == loader2);
}
 
Example #23
Source File: ModuleHelper.java    From pro with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Return the system modules currently installed.
 * This filter out modules that do not starts with java.* or jdk.*.
 * @return the system modules currently installed.
 */
public static ModuleFinder systemModulesFinder() {
  return filter(ModuleFinder.ofSystem(), ref -> {
    var moduleName = ref.descriptor().name();
    return moduleName.startsWith("java.") || moduleName.startsWith("jdk.");
  });
}
 
Example #24
Source File: MultiReleaseJarTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Test multi-release JAR as an automatic module.
 */
public void testAutomaticModule() throws Exception {
    String name = "m";

    Path jar = new JarBuilder(name)
            .resource("p/Main.class")
            .resource("p/Helper.class")
            .resource("META-INF/versions/" + VERSION + "/p/Helper.class")
            .resource("META-INF/versions/" + VERSION + "/p/internal/Helper.class")
            .build();

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

    // check module packages
    ModuleDescriptor descriptor = mref.descriptor();
    Set<String> packages = descriptor.packages();
    if (MULTI_RELEASE) {
        assertTrue(packages.size() == 2);
        assertTrue(packages.contains("p.internal"));
    } else {
        assertTrue(packages.size() == 1);
    }
}
 
Example #25
Source File: ConfigurationTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Basic test of binding services with configurations.
 *
 * The test consists of two configurations:
 * - Configuration cf1: m1 uses p.S
 * - Configuration cf2: m2 provides p.S
 */
public void testServiceBindingWithConfigurations1() {

    ModuleDescriptor descriptor1 = newBuilder("m1")
            .exports("p")
            .uses("p.S")
            .build();

    ModuleFinder finder1 = ModuleUtils.finderOf(descriptor1);

    Configuration cf1 = resolve(finder1, "m1");

    assertTrue(cf1.modules().size() == 1);
    assertTrue(cf1.findModule("m1").isPresent());

    ModuleDescriptor descriptor2 = newBuilder("m2")
            .requires("m1")
            .provides("p.S", List.of("q.T"))
            .build();

    ModuleFinder finder2 = ModuleUtils.finderOf(descriptor2);

    Configuration cf2 = resolveAndBind(cf1, finder2); // no roots

    assertTrue(cf2.parents().size() == 1);
    assertTrue(cf2.parents().get(0) == cf1);

    assertTrue(cf2.modules().size() == 1);
    assertTrue(cf2.findModule("m2").isPresent());

    ResolvedModule m1 = cf1.findModule("m1").get();
    ResolvedModule m2 = cf2.findModule("m2").get();

    assertTrue(m2.reads().size() == 1);
    assertTrue(m2.reads().contains(m1));
}
 
Example #26
Source File: ConfigurationTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Transitive dependency not found
 */
@Test(expectedExceptions = { FindException.class })
public void testTransitiveDependencyNotFound() {
    ModuleDescriptor descriptor1 = newBuilder("m1").requires("m2").build();
    ModuleDescriptor descriptor2 = newBuilder("m2").requires("m3").build();
    ModuleFinder finder = ModuleUtils.finderOf(descriptor1, descriptor2);
    resolve(finder, "m1");
}
 
Example #27
Source File: BasicLayerTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Test layers with a qualified export. The module exporting the package
 * does not read the target module.
 *
 * - Configuration/layer1: m1
 * - Configuration/layer2: m1, m2 { exports p to m1; }
 */
public void testQualifiedExports5() {
    // create layer1 with m1
    ModuleDescriptor descriptor1 = newBuilder("m1").build();
    ModuleFinder finder1 = ModuleUtils.finderOf(descriptor1);
    Configuration cf1 = resolve(finder1, "m1");
    ClassLoader cl1 = new ClassLoader() { };
    ModuleLayer layer1 = ModuleLayer.empty().defineModules(cf1, mn -> cl1);
    assertTrue(layer1.modules().size() == 1);

    // create layer2 with m1 and m2
    ModuleDescriptor descriptor2 = newBuilder("m2").exports("p", Set.of("m1")).build();
    ModuleFinder finder2 = ModuleUtils.finderOf(descriptor1, descriptor2);
    Configuration cf2 = resolve(cf1, finder2, "m1", "m2");
    ClassLoader cl2 = new ClassLoader() { };
    ModuleLayer layer2 = layer1.defineModules(cf2, mn -> cl2);
    assertTrue(layer2.modules().size() == 2);

    Module m1_v1 = layer1.findModule("m1").get();
    Module m1_v2 = layer2.findModule("m1").get();
    Module m2 = layer2.findModule("m2").get();

    // check m2 exports p to layer2/m2
    assertFalse(m2.isExported("p"));
    assertTrue(m2.isExported("p", m1_v2));
    assertFalse(m2.isExported("p", m1_v1));
}
 
Example #28
Source File: TesterPlugin.java    From pro with GNU General Public License v3.0 5 votes vote down vote up
private static List<ModuleReference> modules(List<Path> paths) {
  return ModuleFinder.of(paths.toArray(Path[]::new))
      .findAll()
      .stream()
      .filter(ref -> ref.location().isPresent())
      .sorted(Comparator.comparing(ref -> ref.descriptor().name()))
      .collect(toUnmodifiableList());
}
 
Example #29
Source File: ConfigurationTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Basic test to detect reading two modules with the same name
 *
 * The test consists of three configurations:
 * - Configuration cf1: m1, m2 requires transitive m1
 * - Configuration cf2: m1, m3 requires transitive m1
 * - Configuration cf3(cf1,cf2): m4 requires m2, m3
 */
@Test(expectedExceptions = { ResolutionException.class })
public void testReadTwoModuleWithSameName() {
    ModuleDescriptor descriptor1 = newBuilder("m1")
            .build();

    ModuleDescriptor descriptor2 = newBuilder("m2")
            .requires(Set.of(Requires.Modifier.TRANSITIVE), "m1")
            .build();

    ModuleDescriptor descriptor3 = newBuilder("m3")
            .requires(Set.of(Requires.Modifier.TRANSITIVE), "m1")
            .build();

    ModuleDescriptor descriptor4 = newBuilder("m4")
            .requires("m2")
            .requires("m3")
            .build();

    ModuleFinder finder1 = ModuleUtils.finderOf(descriptor1, descriptor2);
    Configuration cf1 = resolve(finder1, "m2");
    assertTrue(cf1.modules().size() == 2);

    ModuleFinder finder2 = ModuleUtils.finderOf(descriptor1, descriptor3);
    Configuration cf2 = resolve(finder2, "m3");
    assertTrue(cf2.modules().size() == 2);

    // should throw ResolutionException as m4 will read modules named "m1".
    ModuleFinder finder3 = ModuleUtils.finderOf(descriptor4);
    Configuration.resolve(finder3, List.of(cf1, cf2), ModuleFinder.of(), Set.of("m4"));
}
 
Example #30
Source File: ModuleLocationFactoryTest.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Test
public void filters_out_module_infos() {
    URI jrtUri = ModuleFinder.ofSystem().find("java.base").flatMap(ModuleReference::location).get();

    ClassFileSource source = Location.of(jrtUri).asClassFileSource(new ImportOptions());

    Stream<String> allUris = StreamSupport.stream(source.spliterator(), false)
            .map(ClassFileLocation::getUri)
            .map(URI::toString);
    assertThat(allUris.anyMatch(uri -> uri.contains(MODULE_INFO_FILE_NAME)))
            .as("any URI contains " + MODULE_INFO_FILE_NAME)
            .isFalse();
}