java.lang.module.Configuration Java Examples

The following examples show how to use java.lang.module.Configuration. 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: ProxyLayerTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Test proxy implementing interfaces that the target module has no access
 */
@Test
public void testNoReadAccess() throws Exception {
    ModuleFinder finder = ModuleFinder.of(MODS_DIR);
    ModuleLayer bootLayer = ModuleLayer.boot();
    Configuration cf = bootLayer
            .configuration()
            .resolveAndBind(ModuleFinder.of(), finder, Arrays.asList(modules));
    ClassLoader scl = ClassLoader.getSystemClassLoader();
    ModuleLayer layer = bootLayer.defineModulesWithOneLoader(cf, scl);

    ClassLoader loader = layer.findLoader("m1");

    assertTrue(layer.findModule("m1").isPresent());
    assertTrue(layer.findModule("m2").isPresent());
    assertTrue(layer.findModule("m3").isPresent());

    Class<?>[] interfaces = new Class<?>[] {
            Class.forName("p.one.I", false, loader),
            Class.forName("p.two.B", false, loader)   // non-public interface but exported package
    };
    checkIAE(loader, interfaces);
}
 
Example #2
Source File: ConfigurationTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Service provider dependency not found
 */
@Test(expectedExceptions = { FindException.class })
public void testServiceProviderDependencyNotFound() {

    // service provider dependency (on m3) not found

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

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

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

    // should throw ResolutionException because m3 is not found
    Configuration cf = resolveAndBind(finder, "m1");
}
 
Example #3
Source File: LoaderPool.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a pool of class loaders. Each module in the given configuration
 * is mapped to its own class loader in the pool. The class loader is
 * created with the given parent class loader as its parent.
 */
public LoaderPool(Configuration cf,
                  List<ModuleLayer> parentLayers,
                  ClassLoader parentLoader)
{
    Map<String, Loader> loaders = new HashMap<>();
    for (ResolvedModule resolvedModule : cf.modules()) {
        Loader loader = new Loader(resolvedModule, this, parentLoader);
        String mn = resolvedModule.name();
        loaders.put(mn, loader);
    }
    this.loaders = loaders;

    // complete the initialization
    loaders.values().forEach(l -> l.initRemotePackageMap(cf, parentLayers));
}
 
Example #4
Source File: ConfigurationTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Test the scenario where a module contains a package p and reads
 * a module that also contains a package p.
 */
public void testContainsPackageInSelfAndOther() {
    ModuleDescriptor descriptor1 = newBuilder("m1")
            .requires("m2")
            .packages(Set.of("p"))
            .build();

    ModuleDescriptor descriptor2 = newBuilder("m2")
            .packages(Set.of("p"))
            .build();

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

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

    assertTrue(cf.modules().size() == 2);
    assertTrue(cf.findModule("m1").isPresent());
    assertTrue(cf.findModule("m2").isPresent());

    // m1 reads m2, m2 reads nothing
    ResolvedModule m1 = cf.findModule("m1").get();
    ResolvedModule m2 = cf.findModule("m2").get();
    assertTrue(m1.reads().size() == 1);
    assertTrue(m1.reads().contains(m2));
    assertTrue(m2.reads().size() == 0);
}
 
Example #5
Source File: ModuleBootstrap.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Checks incubating status of modules in the configuration
 */
private static void checkIncubatingStatus(Configuration cf) {
    String incubating = null;
    for (ResolvedModule resolvedModule : cf.modules()) {
        ModuleReference mref = resolvedModule.reference();

        // emit warning if the WARN_INCUBATING module resolution bit set
        if (ModuleResolution.hasIncubatingWarning(mref)) {
            String mn = mref.descriptor().name();
            if (incubating == null) {
                incubating = mn;
            } else {
                incubating += ", " + mn;
            }
        }
    }
    if (incubating != null)
        warn("Using incubator modules: " + incubating);
}
 
Example #6
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 #7
Source File: ProxyClassAccessTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Test unnamed module has no access to other proxy interface
 */
@Test
public void testNoReadAccess() throws Exception {
    ModuleFinder finder = ModuleFinder.of(MODS_DIR);
    ModuleLayer bootLayer = ModuleLayer.boot();
    Configuration cf = bootLayer
            .configuration()
            .resolveAndBind(ModuleFinder.of(), finder, modules);
    ClassLoader parentLoader = this.getClass().getClassLoader();
    ModuleLayer layer = bootLayer.defineModulesWithOneLoader(cf, parentLoader);

    ClassLoader loader = layer.findLoader("m1");
    Class<?>[] interfaces = new Class<?>[] {
            Class.forName("p.one.I", false, loader),
            Class.forName("q.NP", false, loader)     // non-public interface in unnamed module
    };
    checkIAE(loader, interfaces);
}
 
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: LoaderPool.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates a pool of class loaders. Each module in the given configuration
 * will be loaded its own class loader in the pool. The class loader is
 * created with the given parent class loader as its parent.
 */
public LoaderPool(Configuration cf,
                  List<ModuleLayer> parentLayers,
                  ClassLoader parentLoader)
{
    Map<String, Loader> loaders = new HashMap<>();
    for (ResolvedModule resolvedModule : cf.modules()) {
        Loader loader = new Loader(resolvedModule, this, parentLoader);
        String mn = resolvedModule.name();
        loaders.put(mn, loader);
    }
    this.loaders = loaders;

    // complete the initialization
    loaders.values().forEach(l -> l.initRemotePackageMap(cf, parentLayers));
}
 
Example #10
Source File: ModuleDotGraph.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Generate dotfile of the given path
 */
public void genDotFile(Path path, String name,
                       Configuration configuration,
                       Attributes attributes)
    throws IOException
{
    // transitive reduction
    Graph<String> graph = apiOnly
            ? requiresTransitiveGraph(configuration, Set.of(name))
            : gengraph(configuration);

    DotGraphBuilder builder = new DotGraphBuilder(name, graph, attributes);
    builder.subgraph("se", "java", attributes.javaSubgraphColor(),
                     DotGraphBuilder.JAVA_SE_SUBGRAPH)
           .subgraph("jdk", "jdk", attributes.jdkSubgraphColor(),
                     DotGraphBuilder.JDK_SUBGRAPH)
           .modules(graph.nodes().stream()
                             .map(mn -> configuration.findModule(mn).get()
                                            .reference().descriptor()));
    // build dot file
    builder.build(path);
}
 
Example #11
Source File: AutomaticModulesTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Basic test of a configuration created with automatic modules
 *   a requires b* and c*
 *   b* contains p
 *   c* contains p
 */
@Test(expectedExceptions = { ResolutionException.class })
public void testDuplicateSuppliers1() throws IOException {
    ModuleDescriptor descriptor
        = ModuleDescriptor.newModule("a")
            .requires("b")
            .requires("c")
            .build();

    // c and d are automatic modules with the same package
    Path dir = Files.createTempDirectory(USER_DIR, "mods");
    createDummyJarFile(dir.resolve("b.jar"), "p/T.class");
    createDummyJarFile(dir.resolve("c.jar"), "p/T.class");

    // module finder locates 'a' and the modules in the directory
    ModuleFinder finder
        = ModuleFinder.compose(ModuleUtils.finderOf(descriptor),
                               ModuleFinder.of(dir));

    Configuration parent = ModuleLayer.boot().configuration();
    resolve(parent, finder, "a");
}
 
Example #12
Source File: TaskHelper.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
static ModuleLayer createPluginsLayer(List<Path> paths) {

        Path[] dirs = paths.toArray(new Path[0]);
        ModuleFinder finder = ModulePath.of(Runtime.version(), true, dirs);
        Configuration bootConfiguration = ModuleLayer.boot().configuration();
        try {
            Configuration cf = bootConfiguration
                .resolveAndBind(ModuleFinder.of(),
                                finder,
                                Collections.emptySet());
            ClassLoader scl = ClassLoader.getSystemClassLoader();
            return ModuleLayer.boot().defineModulesWithOneLoader(cf, scl);
        } catch (Exception ex) {
            // Malformed plugin modules (e.g.: same package in multiple modules).
            throw new PluginException("Invalid modules in the plugins path: " + ex);
        }
    }
 
Example #13
Source File: LayerAndLoadersTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Tests that the class loaders created by defineModulesWithXXX delegate
 * to the given parent class loader.
 */
public void testDelegationToParent() throws Exception {
    Configuration cf = resolve("m1");

    ClassLoader parent = this.getClass().getClassLoader();
    String cn = this.getClass().getName();

    // one loader
    ModuleLayer layer = ModuleLayer.boot().defineModulesWithOneLoader(cf, parent);
    testLoad(layer, cn);

     // one loader with boot loader as parent
    layer = ModuleLayer.boot().defineModulesWithOneLoader(cf, null);
    testLoadFail(layer, cn);

    // many loaders
    layer = ModuleLayer.boot().defineModulesWithManyLoaders(cf, parent);
    testLoad(layer, cn);

    // many loader with boot loader as parent
    layer = ModuleLayer.boot().defineModulesWithManyLoaders(cf, null);
    testLoadFail(layer, cn);
}
 
Example #14
Source File: LayerAndLoadersTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Resolve the given modules, by name, and returns the resulting
 * Configuration.
 */
private static Configuration resolveAndBind(String... roots) {
    ModuleFinder finder = ModuleFinder.of(MODS_DIR);
    return ModuleLayer.boot()
        .configuration()
        .resolveAndBind(finder, ModuleFinder.of(), Set.of(roots));
}
 
Example #15
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 #16
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 in the parent layer.
 *
 * - Configuration/layer1: m1 { }
 * - Configuration/layer2: m2 { exports p to m1; }
 */
public void testQualifiedExports3() {
    // 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 m2
    ModuleDescriptor descriptor2 = newBuilder("m2")
            .exports("p", Set.of("m1"))
            .build();
    ModuleFinder finder2 = ModuleUtils.finderOf(descriptor2);
    Configuration cf2 = resolve(cf1, finder2, "m2");
    ClassLoader cl2 = new ClassLoader() { };
    ModuleLayer layer2 = layer1.defineModules(cf2, mn -> cl2);
    assertTrue(layer2.modules().size() == 1);

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

    // check m2 exports p to layer1/m1
    assertFalse(m2.isExported("p"));
    assertTrue(m2.isExported("p", m1));
    assertFalse(m2.isOpen("p", m1));
}
 
Example #17
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 #18
Source File: ConfigurationTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Basic test of "requires static":
 * - Configuration cf1: m2 requires transitive static m1
 * - Configuration cf2: m3 requires m2
 */
public void testRequiresStatic8() {
    ModuleDescriptor descriptor1 = null;  // not observable

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

    ModuleFinder finder1 = ModuleUtils.finderOf(descriptor2);

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

    assertTrue(cf1.modules().size() == 1);
    assertTrue(cf1.findModule("m2").isPresent());
    ResolvedModule m2 = cf1.findModule("m2").get();
    assertTrue(m2.reads().isEmpty());

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

    ModuleFinder finder2 = ModuleUtils.finderOf(descriptor3);

    Configuration cf2 = resolve(cf1, finder2, "m3");

    assertTrue(cf2.modules().size() == 1);
    assertTrue(cf2.findModule("m3").isPresent());
    ResolvedModule m3 = cf2.findModule("m3").get();
    assertTrue(m3.reads().size() == 1);
    assertTrue(m3.reads().contains(m2));
}
 
Example #19
Source File: ModulesTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Basic test of ServiceLoader.load where the service provider module is an
 * automatic module.
 */
@Test
public void testWithAutomaticModule() throws Exception {
    Path classes = Paths.get(System.getProperty("test.classes"));
    Path jar = Files.createTempDirectory("lib").resolve("pearscript.jar");
    JarUtils.createJarFile(jar, classes, "META-INF", "org");

    ModuleFinder finder = ModuleFinder.of(jar);
    ModuleLayer bootLayer = ModuleLayer.boot();
    Configuration parent = bootLayer.configuration();
    Configuration cf = parent.resolveAndBind(finder, ModuleFinder.of(), Set.of());
    assertTrue(cf.modules().size() == 1);

    ClassLoader scl = ClassLoader.getSystemClassLoader();
    ModuleLayer layer = bootLayer.defineModulesWithOneLoader(cf, scl);
    assertTrue(layer.modules().size() == 1);

    ClassLoader loader = layer.findLoader("pearscript");
    ScriptEngineFactory factory;

    // load using the class loader as context
    factory = ServiceLoader.load(ScriptEngineFactory.class, loader)
            .findFirst()
            .orElse(null);
    assertNotNull(factory);
    assertTrue(factory.getClass().getClassLoader() == loader);

    // load using the layer as context
    factory = ServiceLoader.load(layer, ScriptEngineFactory.class)
            .findFirst()
            .orElse(null);
    assertNotNull(factory);
    assertTrue(factory.getClass().getClassLoader() == loader);
}
 
Example #20
Source File: LayerAndLoadersTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Basic test of ModuleLayer.defineModulesWithOneLoader where one of the
 * modules is a service provider module.
 *
 * Test scenario:
 *    m1 requires m2 and m3
 *    m1 uses S
 *    m4 provides S with ...
 */
public void testServicesWithOneLoader() throws Exception {
    Configuration cf = resolveAndBind("m1");

    ClassLoader scl = ClassLoader.getSystemClassLoader();

    ModuleLayer layer = ModuleLayer.boot().defineModulesWithOneLoader(cf, scl);

    checkLayer(layer, "m1", "m2", "m3", "m4");

    ClassLoader cl1 = layer.findLoader("m1");
    ClassLoader cl2 = layer.findLoader("m2");
    ClassLoader cl3 = layer.findLoader("m3");
    ClassLoader cl4 = layer.findLoader("m4");

    assertTrue(cl1.getParent() == scl);
    assertTrue(cl2 == cl1);
    assertTrue(cl3 == cl1);
    assertTrue(cl4 == cl1);

    Class<?> serviceType = cl1.loadClass("p.Service");
    assertTrue(serviceType.getClassLoader() == cl1);

    Iterator<?> iter = ServiceLoader.load(serviceType, cl1).iterator();
    Object provider = iter.next();
    assertTrue(serviceType.isInstance(provider));
    assertTrue(provider.getClass().getClassLoader() == cl1);
    assertFalse(iter.hasNext());
}
 
Example #21
Source File: BasicLayerTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Exercise ModuleLayer.boot()
 */
public void testBoot() {
    ModuleLayer bootLayer = ModuleLayer.boot();

    // configuration
    Configuration cf = bootLayer.configuration();
    assertTrue(cf.findModule("java.base").get()
            .reference()
            .descriptor()
            .exports()
            .stream().anyMatch(e -> (e.source().equals("java.lang")
                                     && !e.isQualified())));

    // modules
    Set<Module> modules = bootLayer.modules();
    assertTrue(modules.contains(Object.class.getModule()));
    int count = (int) modules.stream().map(Module::getName).count();
    assertEquals(count, modules.size()); // module names are unique

    // findModule
    Module base = Object.class.getModule();
    assertTrue(bootLayer.findModule("java.base").get() == base);
    assertTrue(base.getLayer() == bootLayer);

    // findLoader
    assertTrue(bootLayer.findLoader("java.base") == null);

    // parents
    assertTrue(bootLayer.parents().size() == 1);
    assertTrue(bootLayer.parents().get(0) == ModuleLayer.empty());
}
 
Example #22
Source File: JdkQualifiedExportTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns true if target is an upgradeable module but not required
 * by the source module directly and indirectly.
 */
private static boolean accept(ModuleDescriptor source, String target) {
    if (HashedModules.contains(target))
        return false;

    if (!ModuleFinder.ofSystem().find(target).isPresent())
        return false;

    Configuration cf = Configuration.empty().resolve(ModuleFinder.of(),
                                                     ModuleFinder.ofSystem(),
                                                     Set.of(source.name()));
    return !cf.findModule(target).isPresent();
}
 
Example #23
Source File: ConfigurationTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Basic test of "requires static":
 *     (m1 not observable)
 *     m2 requires transitive static m1
 *     m3 requires m2
 */
public void testRequiresStatic7() {
    ModuleDescriptor descriptor1 = null;  // not observable

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

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

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

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

    assertTrue(cf.modules().size() == 2);
    assertTrue(cf.findModule("m2").isPresent());
    assertTrue(cf.findModule("m3").isPresent());
    ResolvedModule m2 = cf.findModule("m2").get();
    ResolvedModule m3 = cf.findModule("m3").get();
    assertTrue(m2.reads().isEmpty());
    assertTrue(m3.reads().size() == 1);
    assertTrue(m3.reads().contains(m2));
}
 
Example #24
Source File: ConfigurationTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Test the scenario where a module that exports a package that is also
 * exported by a module that it reads in a parent layer.
 */
@Test(expectedExceptions = { ResolutionException.class })
public void testExportSamePackageAsBootLayer() {
    ModuleDescriptor descriptor = newBuilder("m1")
            .requires("java.base")
            .exports("java.lang")
            .build();

    ModuleFinder finder = ModuleUtils.finderOf(descriptor);

    Configuration bootConfiguration = ModuleLayer.boot().configuration();

    // m1 contains package java.lang, java.base exports package java.lang to m1
    resolve(bootConfiguration, finder, "m1");
}
 
Example #25
Source File: Jlink.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns a {@link Configuration} of the given module path,
 * root modules with full service binding.
 */
public Configuration resolveAndBind()
{
    return Configuration.empty().resolveAndBind(finder,
                                                ModuleFinder.of(),
                                                modules);
}
 
Example #26
Source File: JlinkTask.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
ImageHelper(Configuration cf,
            Map<String, Path> modsPaths,
            ByteOrder order,
            Path packagedModulesPath,
            boolean ignoreSigning) throws IOException {
    this.order = order;
    this.packagedModulesPath = packagedModulesPath;
    this.ignoreSigning = ignoreSigning;
    this.archives = modsPaths.entrySet().stream()
                        .map(e -> newArchive(e.getKey(), e.getValue()))
                        .collect(Collectors.toSet());
}
 
Example #27
Source File: JdepsConfiguration.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns Configuration with the given roots
 */
public Configuration resolve(Set<String> roots) {
    if (roots.isEmpty())
        throw new IllegalArgumentException("empty roots");

    return Configuration.empty()
                .resolve(finder, ModuleFinder.of(), roots);
}
 
Example #28
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 #29
Source File: BasicLayerTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Exercise defineModules with a configuration of two modules that
 * have the same module-private package.
 */
public void testPackageContainedInSelfAndOther() {
    ModuleDescriptor descriptor1 =  newBuilder("m1")
            .requires("m2")
            .packages(Set.of("p"))
            .build();

    ModuleDescriptor descriptor2 = newBuilder("m2")
            .packages(Set.of("p"))
            .build();

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

    Configuration cf = resolve(finder, "m1");
    assertTrue(cf.modules().size() == 2);

    // one loader per module, should be okay
    ModuleLayer.empty().defineModules(cf, mn -> new ClassLoader() { });

    // same class loader
    try {
        ClassLoader loader = new ClassLoader() { };
        ModuleLayer.empty().defineModules(cf, mn -> loader);
        assertTrue(false);
    } catch (LayerInstantiationException expected) { }
}
 
Example #30
Source File: LayerAndLoadersTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Resolve the given modules, by name, and returns the resulting
 * Configuration.
 */
private static Configuration resolve(String... roots) {
    ModuleFinder finder = ModuleFinder.of(MODS_DIR);
    return ModuleLayer.boot()
        .configuration()
        .resolve(finder, ModuleFinder.of(), Set.of(roots));
}