author | alanb |
Fri, 07 Apr 2017 08:04:46 +0000 | |
changeset 44520 | 0553e129e0ec |
parent 43665 | 4bb003cad9b9 |
permissions | -rw-r--r-- |
36508 | 1 |
/* |
43665
4bb003cad9b9
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42307
diff
changeset
|
2 |
* Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved. |
36508 | 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 |
||
26 |
/* |
|
27 |
* @test |
|
43665
4bb003cad9b9
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42307
diff
changeset
|
28 |
* @summary class p3.c3 defined in module m1x tries to access c4 defined in an unnamed package |
36508 | 29 |
* and an unnamed module. |
38152
80e5da81fb2c
8154258: [TESTBUG] Various serviceability tests fail compilation
dsamersoff
parents:
36508
diff
changeset
|
30 |
* @modules java.base/jdk.internal.misc |
40631
ed82623d7831
8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
38306
diff
changeset
|
31 |
* @library /test/lib |
36508 | 32 |
* @compile myloaders/MyDiffClassLoader.java |
33 |
* @compile c4.java |
|
34 |
* @compile p3/c3.jcod |
|
35 |
* @compile p3/c3ReadEdgeDiffLoader.jcod |
|
36 |
* @run main/othervm -Xbootclasspath/a:. DiffCL_UmodUpkg |
|
37 |
*/ |
|
38 |
||
39 |
import static jdk.test.lib.Asserts.*; |
|
40 |
||
41 |
import java.lang.module.Configuration; |
|
42 |
import java.lang.module.ModuleDescriptor; |
|
43 |
import java.lang.module.ModuleFinder; |
|
44 |
import java.util.HashMap; |
|
45 |
import java.util.Map; |
|
46 |
import java.util.Set; |
|
47 |
import myloaders.MyDiffClassLoader; |
|
48 |
||
49 |
// |
|
43665
4bb003cad9b9
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42307
diff
changeset
|
50 |
// ClassLoader1 --> defines m1x --> packages p3 |
4bb003cad9b9
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42307
diff
changeset
|
51 |
// package p3 in m1x is exported unqualifiedly |
36508 | 52 |
// |
43665
4bb003cad9b9
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42307
diff
changeset
|
53 |
// class p3.c3 defined in m1x tries to access c4 defined in |
36508 | 54 |
// in unnamed module. |
55 |
// |
|
56 |
// Two access attempts occur in this test: |
|
57 |
// 1. The first access is not allowed because a strict module |
|
58 |
// cannot read an unnamed module. |
|
59 |
// 2. In this scenario a strict module establishes readability |
|
60 |
// to the particular unnamed module it is trying to access. |
|
61 |
// Access is allowed. |
|
62 |
// |
|
63 |
public class DiffCL_UmodUpkg { |
|
64 |
||
44520
0553e129e0ec
8177530: Module system implementation refresh (4/2017)
alanb
parents:
43665
diff
changeset
|
65 |
// Create layers over the boot layer to test different |
36508 | 66 |
// accessing scenarios of a named module to an unnamed module. |
67 |
||
43665
4bb003cad9b9
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42307
diff
changeset
|
68 |
// Module m1x is a strict module and has not established |
36508 | 69 |
// readability to an unnamed module that c4 is defined in. |
70 |
public void test_strictModuleLayer() throws Throwable { |
|
71 |
||
43665
4bb003cad9b9
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42307
diff
changeset
|
72 |
// Define module: m1x |
36508 | 73 |
// Can read: java.base |
74 |
// Packages: p3 |
|
75 |
// Packages exported: p3 is exported unqualifiedly |
|
43665
4bb003cad9b9
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42307
diff
changeset
|
76 |
ModuleDescriptor descriptor_m1x = |
4bb003cad9b9
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42307
diff
changeset
|
77 |
ModuleDescriptor.newModule("m1x") |
36508 | 78 |
.requires("java.base") |
79 |
.exports("p3") |
|
80 |
.build(); |
|
81 |
||
82 |
// Set up a ModuleFinder containing all modules for this layer. |
|
43665
4bb003cad9b9
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42307
diff
changeset
|
83 |
ModuleFinder finder = ModuleLibrary.of(descriptor_m1x); |
36508 | 84 |
|
43665
4bb003cad9b9
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42307
diff
changeset
|
85 |
// Resolves "m1x" |
44520
0553e129e0ec
8177530: Module system implementation refresh (4/2017)
alanb
parents:
43665
diff
changeset
|
86 |
Configuration cf = ModuleLayer.boot() |
36508 | 87 |
.configuration() |
43665
4bb003cad9b9
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42307
diff
changeset
|
88 |
.resolve(finder, ModuleFinder.of(), Set.of("m1x")); |
36508 | 89 |
|
90 |
MyDiffClassLoader.loader1 = new MyDiffClassLoader(); |
|
91 |
MyDiffClassLoader.loader2 = new MyDiffClassLoader(); |
|
92 |
||
43665
4bb003cad9b9
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42307
diff
changeset
|
93 |
// map module m1x to class loader. |
36508 | 94 |
// class c2 will be loaded in an unnamed module/loader2 |
95 |
// to achieve differing class loaders. |
|
96 |
Map<String, ClassLoader> map = new HashMap<>(); |
|
43665
4bb003cad9b9
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42307
diff
changeset
|
97 |
map.put("m1x", MyDiffClassLoader.loader1); |
36508 | 98 |
|
44520
0553e129e0ec
8177530: Module system implementation refresh (4/2017)
alanb
parents:
43665
diff
changeset
|
99 |
// Create layer that contains m1x |
0553e129e0ec
8177530: Module system implementation refresh (4/2017)
alanb
parents:
43665
diff
changeset
|
100 |
ModuleLayer layer = ModuleLayer.boot().defineModules(cf, map::get); |
36508 | 101 |
|
43665
4bb003cad9b9
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42307
diff
changeset
|
102 |
assertTrue(layer.findLoader("m1x") == MyDiffClassLoader.loader1); |
36508 | 103 |
assertTrue(layer.findLoader("java.base") == null); |
104 |
||
105 |
// now use the same loader to load class p3.c3 |
|
106 |
Class p3_c3_class = MyDiffClassLoader.loader1.loadClass("p3.c3"); |
|
107 |
||
108 |
// Attempt access |
|
109 |
try { |
|
110 |
p3_c3_class.newInstance(); |
|
43665
4bb003cad9b9
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42307
diff
changeset
|
111 |
throw new RuntimeException("Test Failed, strict module m1x should not be able to access " + |
36508 | 112 |
"public type c4 defined in unnamed module"); |
113 |
} catch (IllegalAccessError e) { |
|
114 |
} |
|
115 |
} |
|
116 |
||
43665
4bb003cad9b9
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42307
diff
changeset
|
117 |
// Module m1x is a strict module and has established |
36508 | 118 |
// readability to an unnamed module that c4 is defined in. |
119 |
public void test_strictModuleUnnamedReadableLayer() throws Throwable { |
|
120 |
||
43665
4bb003cad9b9
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42307
diff
changeset
|
121 |
// Define module: m1x |
36508 | 122 |
// Can read: java.base |
123 |
// Packages: p3 |
|
124 |
// Packages exported: p3 is exported unqualifiedly |
|
43665
4bb003cad9b9
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42307
diff
changeset
|
125 |
ModuleDescriptor descriptor_m1x = |
4bb003cad9b9
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42307
diff
changeset
|
126 |
ModuleDescriptor.newModule("m1x") |
36508 | 127 |
.requires("java.base") |
128 |
.exports("p3") |
|
129 |
.build(); |
|
130 |
||
131 |
// Set up a ModuleFinder containing all modules for this layer. |
|
43665
4bb003cad9b9
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42307
diff
changeset
|
132 |
ModuleFinder finder = ModuleLibrary.of(descriptor_m1x); |
36508 | 133 |
|
43665
4bb003cad9b9
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42307
diff
changeset
|
134 |
// Resolves "m1x" |
44520
0553e129e0ec
8177530: Module system implementation refresh (4/2017)
alanb
parents:
43665
diff
changeset
|
135 |
Configuration cf = ModuleLayer.boot() |
36508 | 136 |
.configuration() |
43665
4bb003cad9b9
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42307
diff
changeset
|
137 |
.resolve(finder, ModuleFinder.of(), Set.of("m1x")); |
36508 | 138 |
|
139 |
MyDiffClassLoader.loader1 = new MyDiffClassLoader(); |
|
140 |
MyDiffClassLoader.loader2 = new MyDiffClassLoader(); |
|
141 |
||
43665
4bb003cad9b9
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42307
diff
changeset
|
142 |
// map module m1x to class loader. |
36508 | 143 |
// class c2 will be loaded in an unnamed module/loader2 |
144 |
// to achieve differing class loaders. |
|
145 |
Map<String, ClassLoader> map = new HashMap<>(); |
|
43665
4bb003cad9b9
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42307
diff
changeset
|
146 |
map.put("m1x", MyDiffClassLoader.loader1); |
36508 | 147 |
|
44520
0553e129e0ec
8177530: Module system implementation refresh (4/2017)
alanb
parents:
43665
diff
changeset
|
148 |
// Create layer that contains m1x |
0553e129e0ec
8177530: Module system implementation refresh (4/2017)
alanb
parents:
43665
diff
changeset
|
149 |
ModuleLayer layer = ModuleLayer.boot().defineModules(cf, map::get); |
36508 | 150 |
|
43665
4bb003cad9b9
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42307
diff
changeset
|
151 |
assertTrue(layer.findLoader("m1x") == MyDiffClassLoader.loader1); |
36508 | 152 |
assertTrue(layer.findLoader("java.base") == null); |
153 |
||
154 |
// now use the same loader to load class p3.c3ReadEdgeDiffLoader |
|
155 |
Class p3_c3_class = MyDiffClassLoader.loader1.loadClass("p3.c3ReadEdgeDiffLoader"); |
|
156 |
||
157 |
try { |
|
43665
4bb003cad9b9
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42307
diff
changeset
|
158 |
// Read edge between m1x and the unnamed module that loads c4 is established in |
36508 | 159 |
// C3ReadEdgeDiffLoader's ctor before attempting access. |
160 |
p3_c3_class.newInstance(); |
|
161 |
} catch (IllegalAccessError e) { |
|
43665
4bb003cad9b9
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42307
diff
changeset
|
162 |
throw new RuntimeException("Test Failed, module m1x has established readability to " + |
36508 | 163 |
"c4 loader's unnamed module, access should be allowed: " + e.getMessage()); |
164 |
} |
|
165 |
} |
|
166 |
||
167 |
public static void main(String args[]) throws Throwable { |
|
168 |
DiffCL_UmodUpkg test = new DiffCL_UmodUpkg(); |
|
169 |
test.test_strictModuleLayer(); // access denied |
|
170 |
test.test_strictModuleUnnamedReadableLayer(); // access allowed |
|
171 |
} |
|
172 |
} |