Java Code Examples for java.lang.module.Configuration#resolveAndBind()

The following examples show how to use java.lang.module.Configuration#resolveAndBind() . 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: 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 2
Source File: NoInterferenceTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void test() throws Exception {
    ModuleFinder empty = ModuleFinder.of();
    ModuleFinder finder = ModuleFinder.of(MODS_DIR);

    ModuleLayer bootLayer = ModuleLayer.boot();

    Configuration cf0 = bootLayer.configuration();
    Configuration cf1 = cf0.resolveAndBind(finder, empty, Set.of("s1", "s2"));
    Configuration cf2 = cf1.resolveAndBind(finder, empty, Set.of("s1", "s2"));

    // cf1 contains s1, p1, s2, p2
    assertTrue(cf1.modules().size() == 4);

    // cf1 contains s1, p1, s2, p2
    assertTrue(cf2.modules().size() == 4);

    ClassLoader scl = ClassLoader.getSystemClassLoader();

    ModuleLayer layer1 = bootLayer.defineModulesWithManyLoaders(cf1, scl);
    testLayer(layer1);

    ModuleLayer layer2 = layer1.defineModulesWithManyLoaders(cf2, scl);
    testLayer(layer2);
}
 
Example 3
Source File: ConfigurationTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Invokes parent.resolveAndBind(...)
 */
private Configuration resolveAndBind(Configuration parent,
                                     ModuleFinder before,
                                     ModuleFinder after,
                                     String... roots) {
    return parent.resolveAndBind(before, after, Set.of(roots));
}
 
Example 4
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 5
Source File: ConfigurationTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions = { IllegalArgumentException.class })
public void testResolveRequiresAndUsesWithNoParents() {
    ModuleFinder empty = ModuleFinder.of();
    Configuration.resolveAndBind(empty, List.of(), empty, Set.of());
}
 
Example 6
Source File: ConfigurationTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions = { NullPointerException.class })
public void testResolveRequiresAndUsesWithNull3() {
    Configuration empty = Configuration.empty();
    Configuration.resolveAndBind(null, List.of(empty), ModuleFinder.of(), Set.of());
}
 
Example 7
Source File: ConfigurationTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions = { NullPointerException.class })
public void testResolveRequiresAndUsesWithNull4() {
    ModuleFinder empty = ModuleFinder.of();
    Configuration.resolveAndBind(empty, null, empty, Set.of());
}
 
Example 8
Source File: ConfigurationTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions = { NullPointerException.class })
public void testResolveRequiresAndUsesWithNull5() {
    Configuration cf = ModuleLayer.boot().configuration();
    Configuration.resolveAndBind(ModuleFinder.of(), List.of(cf), null, Set.of());
}
 
Example 9
Source File: ConfigurationTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions = { NullPointerException.class })
public void testResolveRequiresAndUsesWithNull6() {
    ModuleFinder empty = ModuleFinder.of();
    Configuration cf = ModuleLayer.boot().configuration();
    Configuration.resolveAndBind(empty, List.of(cf), empty, null);
}
 
Example 10
Source File: TestLayer.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    // disable security manager until Class.forName is called.
    SecurityManager sm = System.getSecurityManager();
    if (sm != null) {
        System.setSecurityManager(null);
    }

    ModuleFinder finder = ModuleFinder.of(MODS_DIR);

    Configuration parent = ModuleLayer.boot().configuration();
    Configuration cf = parent.resolveAndBind(ModuleFinder.of(),
                                             finder,
                                             modules);

    ClassLoader scl = ClassLoader.getSystemClassLoader();
    ModuleLayer layer = ModuleLayer.boot().defineModulesWithManyLoaders(cf, scl);

    Module m1 = layer.findModule("m1").get();
    Module m2 = layer.findModule("m2").get();

    if (sm != null) {
        System.setSecurityManager(sm);
    }

    // find exported and non-exported class from a named module
    findClass(m1, "p1.A");
    findClass(m1, "p1.internal.B");
    findClass(m2, "p2.C");

    // find class from unnamed module
    ClassLoader ld = TestLayer.class.getClassLoader();
    findClass(ld.getUnnamedModule(), "TestDriver");

    // check if clinit should not be initialized
    // compile without module-path; so use reflection
    Class<?> c = Class.forName(m1, "p1.Initializer");
    Method m = c.getMethod("isInited");
    Boolean isClinited = (Boolean) m.invoke(null);
    if (isClinited.booleanValue()) {
       throw new RuntimeException("clinit should not be invoked");
    }
}
 
Example 11
Source File: NoAccess.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    // disable security manager until Class.forName is called.
    SecurityManager sm = System.getSecurityManager();
    if (sm != null) {
        System.setSecurityManager(null);
    }

    ModuleFinder finder = ModuleFinder.of(Paths.get("mods1"), Paths.get("mods2"));

    ModuleLayer bootLayer = ModuleLayer.boot();
    Configuration parent = bootLayer.configuration();

    Configuration cf = parent.resolveAndBind(finder,
                                             ModuleFinder.of(),
                                             Set.of("m1", "m2"));

    ClassLoader scl = ClassLoader.getSystemClassLoader();
    ModuleLayer layer = bootLayer.defineModulesWithManyLoaders(cf, scl);

    if (sm != null) {
        System.setSecurityManager(sm);
    }

    Module m1 = bootLayer.findModule("m1").get();
    Module m2 = bootLayer.findModule("m2").get();
    Module m3 = bootLayer.findModule("m3").get();

    findClass(m1, "p1.internal.B");
    findClass(m2, "p2.C");
    findClass(m3, "p3.internal.Foo");

    // permissions granted
    findClass(m1, "p1.A");
    findClass(m1, "p1.internal.B");
    findClass(m2, "p2.C");
    findClass(m3, "p3.internal.Foo");


    // m1 and m2 from a different layer
    m1 = layer.findModule("m1").get();
    m2 = layer.findModule("m2").get();
    m3 = layer.findModule("m3").get();

    findClass(m1, "p1.A");
    findClass(m3, "p3.internal.Foo");

    // no permission
    Path path = MODS_DIR1.resolve("p1").resolve("internal").resolve("B.class");
    findClass(m1, "p1.internal.B", new FilePermission(path.toString(), "read"));
    path = MODS_DIR2.resolve("p2").resolve("C.class");
    findClass(m2, "p2.C", new FilePermission(path.toString(), "read"));
}
 
Example 12
Source File: ModulesTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Basic test of ServiceLoader.load with a tree of layers.
 *
 * Test scenario:
 * - boot layer contains "bananascript", maybe other script engines
 * - layer1, with boot layer as parent, contains "bananascript"
 * - layer2, with boot layer as parent, contains "bananascript"
 * - layer3, with layer1 ad layer as parents, contains "bananascript"
 *
 * ServiceLoader should locate all 4 script engine factories in DFS order.
 */
@Test
public void testWithCustomLayer3() {
    ModuleLayer bootLayer = ModuleLayer.boot();
    Configuration cf0 = bootLayer.configuration();

    // boot layer should contain "bananascript"
    List<ScriptEngineFactory> factories
        = collectAll(ServiceLoader.load(bootLayer, ScriptEngineFactory.class));
    int countInBootLayer = factories.size();
    assertTrue(countInBootLayer >= 1);
    assertTrue(factories.stream()
            .map(p -> p.getEngineName())
            .filter("BananaScriptEngine"::equals)
            .findAny()
            .isPresent());

    ClassLoader scl = ClassLoader.getSystemClassLoader();
    Path dir = Paths.get(System.getProperty("test.classes", "."), "modules");
    ModuleFinder finder = ModuleFinder.of(dir);

    // layer1
    Configuration cf1 = cf0.resolveAndBind(finder, ModuleFinder.of(), Set.of());
    ModuleLayer layer1 = bootLayer.defineModulesWithOneLoader(cf1, scl);
    assertTrue(layer1.modules().size() == 1);

    // layer2
    Configuration cf2 = cf0.resolveAndBind(finder, ModuleFinder.of(), Set.of());
    ModuleLayer layer2 = bootLayer.defineModulesWithOneLoader(cf2, scl);
    assertTrue(layer2.modules().size() == 1);

    // layer3 with layer1 and layer2 as parents
    Configuration cf3 = Configuration.resolveAndBind(finder,
            List.of(cf1, cf2),
            ModuleFinder.of(),
            Set.of());
    ModuleLayer layer3
        = ModuleLayer.defineModulesWithOneLoader(cf3, List.of(layer1, layer2), scl).layer();
    assertTrue(layer3.modules().size() == 1);


    // class loaders
    ClassLoader loader1 = layer1.findLoader("bananascript");
    ClassLoader loader2 = layer2.findLoader("bananascript");
    ClassLoader loader3 = layer3.findLoader("bananascript");
    assertTrue(loader1 != loader2);
    assertTrue(loader1 != loader3);
    assertTrue(loader2 != loader3);

    // load all factories with layer3 as the context
    factories = collectAll(ServiceLoader.load(layer3, ScriptEngineFactory.class));
    int count = factories.size();
    assertTrue(count == countInBootLayer + 3);

    // the ordering should be layer3, layer1, boot layer, layer2

    ScriptEngineFactory factory = factories.get(0);
    assertTrue(factory.getClass().getModule().getLayer() == layer3);
    assertTrue(factory.getClass().getClassLoader() == loader3);
    assertTrue(factory.getEngineName().equals("BananaScriptEngine"));

    factory = factories.get(1);
    assertTrue(factory.getClass().getModule().getLayer() == layer1);
    assertTrue(factory.getClass().getClassLoader() == loader1);
    assertTrue(factory.getEngineName().equals("BananaScriptEngine"));

    // boot layer "bananascript" and maybe other factories
    int last = count -1;
    boolean found = false;
    for (int i=2; i<last; i++) {
        factory = factories.get(i);
        assertTrue(factory.getClass().getModule().getLayer() == bootLayer);
        if (factory.getEngineName().equals("BananaScriptEngine")) {
            assertFalse(found);
            found = true;
        }
    }
    assertTrue(found);

    factory = factories.get(last);
    assertTrue(factory.getClass().getModule().getLayer() == layer2);
    assertTrue(factory.getClass().getClassLoader() == loader2);
    assertTrue(factory.getEngineName().equals("BananaScriptEngine"));
}
 
Example 13
Source File: ModulesTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Basic test of ServiceLoader.load with a tree of layers.
 *
 * Test scenario:
 * - boot layer contains "bananascript", maybe other script engines
 * - layer1, with boot layer as parent, contains "bananascript"
 * - layer2, with boot layer as parent, contains "bananascript"
 * - layer3, with layer1 ad layer as parents, contains "bananascript"
 *
 * ServiceLoader should locate all 4 script engine factories in DFS order.
 */
@Test
public void testWithCustomLayer3() {
    ModuleLayer bootLayer = ModuleLayer.boot();
    Configuration cf0 = bootLayer.configuration();

    // boot layer should contain "bananascript"
    List<ScriptEngineFactory> factories
        = collectAll(ServiceLoader.load(bootLayer, ScriptEngineFactory.class));
    int countInBootLayer = factories.size();
    assertTrue(countInBootLayer >= 1);
    assertTrue(factories.stream()
            .map(p -> p.getEngineName())
            .filter("BananaScriptEngine"::equals)
            .findAny()
            .isPresent());

    ClassLoader scl = ClassLoader.getSystemClassLoader();
    Path dir = Paths.get(System.getProperty("test.classes", "."), "modules");
    ModuleFinder finder = ModuleFinder.of(dir);

    // layer1
    Configuration cf1 = cf0.resolveAndBind(finder, ModuleFinder.of(), Set.of());
    ModuleLayer layer1 = bootLayer.defineModulesWithOneLoader(cf1, scl);
    assertTrue(layer1.modules().size() == 1);

    // layer2
    Configuration cf2 = cf0.resolveAndBind(finder, ModuleFinder.of(), Set.of());
    ModuleLayer layer2 = bootLayer.defineModulesWithOneLoader(cf2, scl);
    assertTrue(layer2.modules().size() == 1);

    // layer3 with layer1 and layer2 as parents
    Configuration cf3 = Configuration.resolveAndBind(finder,
            List.of(cf1, cf2),
            ModuleFinder.of(),
            Set.of());
    ModuleLayer layer3
        = ModuleLayer.defineModulesWithOneLoader(cf3, List.of(layer1, layer2), scl).layer();
    assertTrue(layer3.modules().size() == 1);


    // class loaders
    ClassLoader loader1 = layer1.findLoader("bananascript");
    ClassLoader loader2 = layer2.findLoader("bananascript");
    ClassLoader loader3 = layer3.findLoader("bananascript");
    assertTrue(loader1 != loader2);
    assertTrue(loader1 != loader3);
    assertTrue(loader2 != loader3);

    // load all factories with layer3 as the context
    factories = collectAll(ServiceLoader.load(layer3, ScriptEngineFactory.class));
    int count = factories.size();
    assertTrue(count == countInBootLayer + 3);

    // the ordering should be layer3, layer1, boot layer, layer2

    ScriptEngineFactory factory = factories.get(0);
    assertTrue(factory.getClass().getModule().getLayer() == layer3);
    assertTrue(factory.getClass().getClassLoader() == loader3);
    assertTrue(factory.getEngineName().equals("BananaScriptEngine"));

    factory = factories.get(1);
    assertTrue(factory.getClass().getModule().getLayer() == layer1);
    assertTrue(factory.getClass().getClassLoader() == loader1);
    assertTrue(factory.getEngineName().equals("BananaScriptEngine"));

    // boot layer "bananascript" and maybe other factories
    int last = count -1;
    boolean found = false;
    for (int i=2; i<last; i++) {
        factory = factories.get(i);
        assertTrue(factory.getClass().getModule().getLayer() == bootLayer);
        if (factory.getEngineName().equals("BananaScriptEngine")) {
            assertFalse(found);
            found = true;
        }
    }
    assertTrue(found);

    factory = factories.get(last);
    assertTrue(factory.getClass().getModule().getLayer() == layer2);
    assertTrue(factory.getClass().getClassLoader() == loader2);
    assertTrue(factory.getEngineName().equals("BananaScriptEngine"));
}