java.lang.module.Configuration#resolveAndBind ( )源码实例Demo

下面列出了java.lang.module.Configuration#resolveAndBind ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: openjdk-jdk9   文件: TaskHelper.java
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);
        }
    }
 
源代码2 项目: openjdk-jdk9   文件: NoInterferenceTest.java
@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);
}
 
源代码3 项目: openjdk-jdk9   文件: ConfigurationTest.java
/**
 * Invokes parent.resolveAndBind(...)
 */
private Configuration resolveAndBind(Configuration parent,
                                     ModuleFinder before,
                                     ModuleFinder after,
                                     String... roots) {
    return parent.resolveAndBind(before, after, Set.of(roots));
}
 
源代码4 项目: openjdk-jdk9   文件: ModulesTest.java
/**
 * 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);
}
 
源代码5 项目: openjdk-jdk9   文件: ConfigurationTest.java
@Test(expectedExceptions = { IllegalArgumentException.class })
public void testResolveRequiresAndUsesWithNoParents() {
    ModuleFinder empty = ModuleFinder.of();
    Configuration.resolveAndBind(empty, List.of(), empty, Set.of());
}
 
源代码6 项目: openjdk-jdk9   文件: ConfigurationTest.java
@Test(expectedExceptions = { NullPointerException.class })
public void testResolveRequiresAndUsesWithNull3() {
    Configuration empty = Configuration.empty();
    Configuration.resolveAndBind(null, List.of(empty), ModuleFinder.of(), Set.of());
}
 
源代码7 项目: openjdk-jdk9   文件: ConfigurationTest.java
@Test(expectedExceptions = { NullPointerException.class })
public void testResolveRequiresAndUsesWithNull4() {
    ModuleFinder empty = ModuleFinder.of();
    Configuration.resolveAndBind(empty, null, empty, Set.of());
}
 
源代码8 项目: openjdk-jdk9   文件: ConfigurationTest.java
@Test(expectedExceptions = { NullPointerException.class })
public void testResolveRequiresAndUsesWithNull5() {
    Configuration cf = ModuleLayer.boot().configuration();
    Configuration.resolveAndBind(ModuleFinder.of(), List.of(cf), null, Set.of());
}
 
源代码9 项目: openjdk-jdk9   文件: ConfigurationTest.java
@Test(expectedExceptions = { NullPointerException.class })
public void testResolveRequiresAndUsesWithNull6() {
    ModuleFinder empty = ModuleFinder.of();
    Configuration cf = ModuleLayer.boot().configuration();
    Configuration.resolveAndBind(empty, List.of(cf), empty, null);
}
 
源代码10 项目: openjdk-jdk9   文件: TestLayer.java
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");
    }
}
 
源代码11 项目: openjdk-jdk9   文件: NoAccess.java
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"));
}
 
源代码12 项目: openjdk-jdk9   文件: ModulesTest.java
/**
 * 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"));
}
 
源代码13 项目: openjdk-jdk9   文件: ModulesTest.java
/**
 * 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"));
}