ModuleSameCLMain.java revision 12290:8953c0318163
1/*
2 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26import static jdk.test.lib.Asserts.*;
27
28import java.lang.reflect.Layer;
29import java.lang.module.Configuration;
30import java.lang.module.ModuleDescriptor;
31import java.lang.module.ModuleFinder;
32import java.util.HashMap;
33import java.util.Map;
34import java.util.Set;
35
36//
37// ClassLoader1 --> defines m1 --> packages p1
38// ClassLoader1 --> defines m2 --> packages p2
39//
40// m1 can read m2
41// package p2 in m2 is exported to m1
42//
43// class p1.c1 defined in m1 tries to access p2.c2 defined in m2
44// Access allowed since m1 can read m2 and package p2 is exported to m1.
45//
46public class ModuleSameCLMain {
47
48    // Create a Layer over the boot layer.
49    // Define modules within this layer to test access between
50    // publically defined classes within packages of those modules.
51    public void createLayerOnBoot() throws Throwable {
52
53        // Define module:     m1
54        // Can read:          java.base, m2
55        // Packages:          p1
56        // Packages exported: p1 is exported to unqualifiedly
57        ModuleDescriptor descriptor_m1 =
58                ModuleDescriptor.module("m1")
59                        .requires("java.base")
60                        .requires("m2")
61                        .exports("p1")
62                        .build();
63
64        // Define module:     m2
65        // Can read:          java.base
66        // Packages:          p2
67        // Packages exported: package p2 is exported to m1
68        ModuleDescriptor descriptor_m2 =
69                ModuleDescriptor.module("m2")
70                        .requires("java.base")
71                        .exports("p2", Set.of("m1"))
72                        .build();
73
74        // Set up a ModuleFinder containing all modules for this layer.
75        ModuleFinder finder = ModuleLibrary.of(descriptor_m1, descriptor_m2);
76
77        // Resolves "m1"
78        Configuration cf = Layer.boot()
79                .configuration()
80                .resolveRequires(finder, ModuleFinder.of(), Set.of("m1"));
81
82        // map each module to the same class loader for this test
83        Map<String, ClassLoader> map = new HashMap<>();
84        Loader1 cl1 = new Loader1();
85        map.put("m1", cl1);
86        map.put("m2", cl1);
87
88        // Create Layer that contains m1 & m2
89        Layer layer = Layer.boot().defineModules(cf, map::get);
90        assertTrue(layer.findLoader("m1") == cl1);
91        assertTrue(layer.findLoader("m2") == cl1);
92        assertTrue(layer.findLoader("java.base") == null);
93
94        // now use the same loader to load class p1.c1
95        Class p1_c1_class = cl1.loadClass("p1.c1");
96        try {
97            p1_c1_class.newInstance();
98        } catch (IllegalAccessError e) {
99            throw new RuntimeException("Test Failed, an IAE should not be thrown since p2 is exported qualifiedly to m1");
100        }
101    }
102
103    public static void main(String args[]) throws Throwable {
104      ModuleSameCLMain test = new ModuleSameCLMain();
105      test.createLayerOnBoot();
106    }
107
108    static class Loader1 extends ClassLoader { }
109}
110