ModuleNonBuiltinCLMain.java revision 12735:afedee84773e
1121326Sharti/*
2121326Sharti * Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
3121326Sharti * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4121326Sharti *
5121326Sharti * This code is free software; you can redistribute it and/or modify it
6121326Sharti * under the terms of the GNU General Public License version 2 only, as
7121326Sharti * published by the Free Software Foundation.  Oracle designates this
8121326Sharti * particular file as subject to the "Classpath" exception as provided
9121326Sharti * by Oracle in the LICENSE file that accompanied this code.
10121326Sharti *
11121326Sharti * This code is distributed in the hope that it will be useful, but WITHOUT
12121326Sharti * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13121326Sharti * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14121326Sharti * version 2 for more details (a copy is included in the LICENSE file that
15121326Sharti * accompanied this code).
16121326Sharti *
17121326Sharti * You should have received a copy of the GNU General Public License version
18121326Sharti * 2 along with this work; if not, write to the Free Software Foundation,
19121326Sharti * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20121326Sharti *
21121326Sharti * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22121326Sharti * or visit www.oracle.com if you need additional information or have any
23121326Sharti * questions.
24121326Sharti */
25121326Sharti
26121326Shartiimport static jdk.test.lib.Asserts.*;
27121326Sharti
28121326Shartiimport java.lang.module.Configuration;
29121326Shartiimport java.lang.module.ModuleDescriptor;
30121326Shartiimport java.lang.module.ModuleFinder;
31121326Shartiimport java.util.HashMap;
32121326Shartiimport java.util.HashSet;
33121326Shartiimport java.util.Map;
34121326Shartiimport java.util.Set;
35121326Sharti
36121326Sharti//
37121326Sharti// ClassLoader1 --> defines m1x --> packages p1
38121326Sharti// ClassLoader2 --> defines m2x --> packages p2
39121326Sharti// Java System Class Loader --> defines m3x --> packages p3
40121326Sharti//
41121326Sharti// m1x can read m2x
42121326Sharti// package p2 in m2x is exported to m1x and m3x
43121326Sharti//
44121326Sharti// class p1.c1 defined in m1x tries to access p2.c2 defined in m2x
45121326Sharti// Access allowed since m1x can read m2x and package p2 is exported to m1x.
46121326Sharti//
47121326Shartipublic class ModuleNonBuiltinCLMain {
48121326Sharti
49121326Sharti    // Create a layer over the boot layer.
50121326Sharti    // Define modules within this layer to test access between
51121326Sharti    // publically defined classes within packages of those modules.
52121326Sharti    public void createLayerOnBoot() throws Throwable {
53121326Sharti
54121326Sharti        // Define module:     m1x
55121326Sharti        // Can read:          java.base, m2x
56121326Sharti        // Packages:          p1
57121326Sharti        // Packages exported: p1 is exported to unqualifiedly
58121326Sharti        ModuleDescriptor descriptor_m1x =
59121326Sharti                ModuleDescriptor.newModule("m1x")
60121326Sharti                        .requires("java.base")
61121326Sharti                        .requires("m2x")
62121326Sharti                        .exports("p1")
63121326Sharti                        .build();
64121326Sharti
65121326Sharti        // Define module:     m2x
66121326Sharti        // Can read:          java.base, m3x
67121326Sharti        // Packages:          p2
68121326Sharti        // Packages exported: package p2 is exported to m1x and m3x
69121326Sharti        Set<String> targets = new HashSet<>();
70121326Sharti        targets.add("m1x");
71121326Sharti        targets.add("m3x");
72121326Sharti        ModuleDescriptor descriptor_m2x =
73121326Sharti                ModuleDescriptor.newModule("m2x")
74121326Sharti                        .requires("java.base")
75121326Sharti                        .requires("m3x")
76121326Sharti                        .exports("p2", targets)
77121326Sharti                        .build();
78121326Sharti
79121326Sharti        // Define module:     m3x
80121326Sharti        // Can read:          java.base
81121326Sharti        // Packages:          p3
82121326Sharti        // Packages exported: none
83121326Sharti        ModuleDescriptor descriptor_m3x =
84121326Sharti                ModuleDescriptor.newModule("m3x")
85121326Sharti                        .requires("java.base")
86121326Sharti                        .build();
87121326Sharti
88121326Sharti        // Set up a ModuleFinder containing all modules for this layer.
89121326Sharti        ModuleFinder finder = ModuleLibrary.of(descriptor_m1x, descriptor_m2x, descriptor_m3x);
90121326Sharti
91121326Sharti        // Resolves "m1x"
92121326Sharti        Configuration cf = ModuleLayer.boot()
93121326Sharti                .configuration()
94121326Sharti                .resolve(finder, ModuleFinder.of(), Set.of("m1x"));
95121326Sharti
96121326Sharti        // map each module to differing user defined class loaders for this test
97121326Sharti        Map<String, ClassLoader> map = new HashMap<>();
98121326Sharti        Loader1 cl1 = new Loader1();
99121326Sharti        Loader2 cl2 = new Loader2();
100121326Sharti        ClassLoader cl3 = ClassLoader.getSystemClassLoader();
101121326Sharti        map.put("m1x", cl1);
102121326Sharti        map.put("m2x", cl2);
103121326Sharti        map.put("m3x", cl3);
104121326Sharti
105121326Sharti        // Create layer that contains m1x & m2x
106121326Sharti        ModuleLayer layer = ModuleLayer.boot().defineModules(cf, map::get);
107121326Sharti        assertTrue(layer.findLoader("m1x") == cl1);
108121326Sharti        assertTrue(layer.findLoader("m2x") == cl2);
109121326Sharti        assertTrue(layer.findLoader("m3x") == cl3);
110121326Sharti        assertTrue(layer.findLoader("java.base") == null);
111121326Sharti
112121326Sharti        // now use the same loader to load class p1.c1
113121326Sharti        Class p1_c1_class = cl1.loadClass("p1.c1");
114121326Sharti        try {
115121326Sharti            p1_c1_class.newInstance();
116121326Sharti        } catch (IllegalAccessError e) {
117121326Sharti            throw new RuntimeException("Test Failed, an IAE should not be thrown since p2 is exported qualifiedly to m1x");
118121326Sharti        }
119121326Sharti    }
120121326Sharti
121121326Sharti    public static void main(String args[]) throws Throwable {
122121326Sharti      ModuleNonBuiltinCLMain test = new ModuleNonBuiltinCLMain();
123121326Sharti      test.createLayerOnBoot();
124121326Sharti    }
125121326Sharti
126121326Sharti    static class Loader1 extends ClassLoader { }
127121326Sharti    static class Loader2 extends ClassLoader { }
128121326Sharti}
129121326Sharti