ModuleNonBuiltinCLMain.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.HashSet;
34import java.util.Map;
35import java.util.Set;
36
37//
38// ClassLoader1 --> defines m1 --> packages p1
39// ClassLoader2 --> defines m2 --> packages p2
40// Java System Class Loader --> defines m3 --> packages p3
41//
42// m1 can read m2
43// package p2 in m2 is exported to m1 and m3
44//
45// class p1.c1 defined in m1 tries to access p2.c2 defined in m2
46// Access allowed since m1 can read m2 and package p2 is exported to m1.
47//
48public class ModuleNonBuiltinCLMain {
49
50    // Create a Layer over the boot layer.
51    // Define modules within this layer to test access between
52    // publically defined classes within packages of those modules.
53    public void createLayerOnBoot() throws Throwable {
54
55        // Define module:     m1
56        // Can read:          java.base, m2
57        // Packages:          p1
58        // Packages exported: p1 is exported to unqualifiedly
59        ModuleDescriptor descriptor_m1 =
60                ModuleDescriptor.module("m1")
61                        .requires("java.base")
62                        .requires("m2")
63                        .exports("p1")
64                        .build();
65
66        // Define module:     m2
67        // Can read:          java.base, m3
68        // Packages:          p2
69        // Packages exported: package p2 is exported to m1 and m3
70        Set<String> targets = new HashSet<>();
71        targets.add("m1");
72        targets.add("m3");
73        ModuleDescriptor descriptor_m2 =
74                ModuleDescriptor.module("m2")
75                        .requires("java.base")
76                        .requires("m3")
77                        .exports("p2", targets)
78                        .build();
79
80        // Define module:     m3
81        // Can read:          java.base
82        // Packages:          p3
83        // Packages exported: none
84        ModuleDescriptor descriptor_m3 =
85                ModuleDescriptor.module("m3")
86                        .requires("java.base")
87                        .build();
88
89        // Set up a ModuleFinder containing all modules for this layer.
90        ModuleFinder finder = ModuleLibrary.of(descriptor_m1, descriptor_m2, descriptor_m3);
91
92        // Resolves "m1"
93        Configuration cf = Layer.boot()
94                .configuration()
95                .resolveRequires(finder, ModuleFinder.of(), Set.of("m1"));
96
97        // map each module to differing user defined class loaders for this test
98        Map<String, ClassLoader> map = new HashMap<>();
99        Loader1 cl1 = new Loader1();
100        Loader2 cl2 = new Loader2();
101        ClassLoader cl3 = ClassLoader.getSystemClassLoader();
102        map.put("m1", cl1);
103        map.put("m2", cl2);
104        map.put("m3", cl3);
105
106        // Create Layer that contains m1 & m2
107        Layer layer = Layer.boot().defineModules(cf, map::get);
108        assertTrue(layer.findLoader("m1") == cl1);
109        assertTrue(layer.findLoader("m2") == cl2);
110        assertTrue(layer.findLoader("m3") == cl3);
111        assertTrue(layer.findLoader("java.base") == null);
112
113        // now use the same loader to load class p1.c1
114        Class p1_c1_class = cl1.loadClass("p1.c1");
115        try {
116            p1_c1_class.newInstance();
117        } catch (IllegalAccessError e) {
118            throw new RuntimeException("Test Failed, an IAE should not be thrown since p2 is exported qualifiedly to m1");
119        }
120    }
121
122    public static void main(String args[]) throws Throwable {
123      ModuleNonBuiltinCLMain test = new ModuleNonBuiltinCLMain();
124      test.createLayerOnBoot();
125    }
126
127    static class Loader1 extends ClassLoader { }
128    static class Loader2 extends ClassLoader { }
129}
130