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 p1.c1 defined in m1x tries to access p2.c2 defined in unnamed module. |
40631
ed82623d7831
8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
38306
diff
changeset
|
29 |
* @library /test/lib |
38152
80e5da81fb2c
8154258: [TESTBUG] Various serviceability tests fail compilation
dsamersoff
parents:
36508
diff
changeset
|
30 |
* @modules java.base/jdk.internal.misc |
36508 | 31 |
* @modules java.base/jdk.internal.module |
32 |
* @compile myloaders/MyDiffClassLoader.java |
|
33 |
* @compile p2/c2.java |
|
34 |
* @compile p1/c1.java |
|
35 |
* @compile p1/c1ReadEdgeDiffLoader.java |
|
36 |
* @compile p1/c1Loose.java |
|
37 |
* @run main/othervm -Xbootclasspath/a:. DiffCL_Umod |
|
38 |
*/ |
|
39 |
||
40 |
import static jdk.test.lib.Asserts.*; |
|
41 |
||
42 |
import java.lang.module.Configuration; |
|
43 |
import java.lang.module.ModuleDescriptor; |
|
44 |
import java.lang.module.ModuleFinder; |
|
45 |
import java.util.HashMap; |
|
46 |
import java.util.Map; |
|
47 |
import java.util.Set; |
|
48 |
import myloaders.MyDiffClassLoader; |
|
49 |
||
50 |
// |
|
43665
4bb003cad9b9
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42307
diff
changeset
|
51 |
// ClassLoader1 --> defines m1x --> packages p1 |
4bb003cad9b9
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42307
diff
changeset
|
52 |
// package p1 in m1x is exported unqualifiedly |
36508 | 53 |
// |
43665
4bb003cad9b9
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42307
diff
changeset
|
54 |
// class p1.c1 defined in m1x tries to access p2.c2 defined in |
36508 | 55 |
// in unnamed module. |
56 |
// |
|
57 |
// Three access attempts occur in this test: |
|
58 |
// 1. The first access is not allowed because a strict module |
|
59 |
// cannot read an unnamed module. |
|
60 |
// 2. In this scenario a strict module establishes readability |
|
61 |
// to the particular unnamed module it is trying to access. |
|
62 |
// Access is allowed. |
|
43665
4bb003cad9b9
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42307
diff
changeset
|
63 |
// 3. Module m1x in the test_looseModuleLayer() method |
36508 | 64 |
// is transitioned to a loose module, access |
65 |
// to all unnamed modules is allowed. |
|
66 |
// |
|
67 |
public class DiffCL_Umod { |
|
68 |
||
44520
0553e129e0ec
8177530: Module system implementation refresh (4/2017)
alanb
parents:
43665
diff
changeset
|
69 |
// Create layers over the boot layer to test different |
36508 | 70 |
// accessing scenarios of a named module to an unnamed module. |
71 |
||
43665
4bb003cad9b9
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42307
diff
changeset
|
72 |
// Module m1x is a strict module and has not established |
36508 | 73 |
// readability to an unnamed module that p2.c2 is defined in. |
74 |
public void test_strictModuleLayer() throws Throwable { |
|
75 |
||
43665
4bb003cad9b9
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42307
diff
changeset
|
76 |
// Define module: m1x |
36508 | 77 |
// Can read: java.base |
78 |
// Packages: p1 |
|
79 |
// Packages exported: p1 is exported unqualifiedly |
|
43665
4bb003cad9b9
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42307
diff
changeset
|
80 |
ModuleDescriptor descriptor_m1x = |
4bb003cad9b9
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42307
diff
changeset
|
81 |
ModuleDescriptor.newModule("m1x") |
36508 | 82 |
.requires("java.base") |
83 |
.exports("p1") |
|
84 |
.build(); |
|
85 |
||
86 |
// Set up a ModuleFinder containing all modules for this layer. |
|
43665
4bb003cad9b9
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42307
diff
changeset
|
87 |
ModuleFinder finder = ModuleLibrary.of(descriptor_m1x); |
36508 | 88 |
|
43665
4bb003cad9b9
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42307
diff
changeset
|
89 |
// Resolves "m1x" |
44520
0553e129e0ec
8177530: Module system implementation refresh (4/2017)
alanb
parents:
43665
diff
changeset
|
90 |
Configuration cf = ModuleLayer.boot() |
36508 | 91 |
.configuration() |
43665
4bb003cad9b9
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42307
diff
changeset
|
92 |
.resolve(finder, ModuleFinder.of(), Set.of("m1x")); |
36508 | 93 |
|
94 |
MyDiffClassLoader.loader1 = new MyDiffClassLoader(); |
|
95 |
MyDiffClassLoader.loader2 = new MyDiffClassLoader(); |
|
96 |
||
43665
4bb003cad9b9
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42307
diff
changeset
|
97 |
// map module m1x to class loader. |
36508 | 98 |
// class c2 will be loaded in an unnamed module/loader2 |
99 |
// to achieve differing class loaders. |
|
100 |
Map<String, ClassLoader> map = new HashMap<>(); |
|
43665
4bb003cad9b9
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42307
diff
changeset
|
101 |
map.put("m1x", MyDiffClassLoader.loader1); |
36508 | 102 |
|
44520
0553e129e0ec
8177530: Module system implementation refresh (4/2017)
alanb
parents:
43665
diff
changeset
|
103 |
// Create layer that contains m1x |
0553e129e0ec
8177530: Module system implementation refresh (4/2017)
alanb
parents:
43665
diff
changeset
|
104 |
ModuleLayer layer = ModuleLayer.boot().defineModules(cf, map::get); |
36508 | 105 |
|
43665
4bb003cad9b9
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42307
diff
changeset
|
106 |
assertTrue(layer.findLoader("m1x") == MyDiffClassLoader.loader1); |
36508 | 107 |
assertTrue(layer.findLoader("java.base") == null); |
108 |
||
109 |
// now use the same loader to load class p1.c1 |
|
110 |
Class p1_c1_class = MyDiffClassLoader.loader1.loadClass("p1.c1"); |
|
111 |
||
112 |
// Attempt access |
|
113 |
try { |
|
114 |
p1_c1_class.newInstance(); |
|
43665
4bb003cad9b9
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42307
diff
changeset
|
115 |
throw new RuntimeException("Test Failed, strict module m1x should not be able " + |
36508 | 116 |
"to access public type p2.c2 defined in unnamed module"); |
117 |
} catch (IllegalAccessError e) { |
|
118 |
} |
|
119 |
} |
|
120 |
||
43665
4bb003cad9b9
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42307
diff
changeset
|
121 |
// Module m1x is a strict module and has established |
36508 | 122 |
// readability to an unnamed module that p2.c2 is defined in. |
123 |
public void test_strictModuleUnnamedReadableLayer() throws Throwable { |
|
124 |
||
43665
4bb003cad9b9
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42307
diff
changeset
|
125 |
// Define module: m1x |
36508 | 126 |
// Can read: java.base |
127 |
// Packages: p1 |
|
128 |
// Packages exported: p1 is exported unqualifiedly |
|
43665
4bb003cad9b9
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42307
diff
changeset
|
129 |
ModuleDescriptor descriptor_m1x = |
4bb003cad9b9
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42307
diff
changeset
|
130 |
ModuleDescriptor.newModule("m1x") |
36508 | 131 |
.requires("java.base") |
132 |
.exports("p1") |
|
133 |
.build(); |
|
134 |
||
135 |
// Set up a ModuleFinder containing all modules for this layer. |
|
43665
4bb003cad9b9
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42307
diff
changeset
|
136 |
ModuleFinder finder = ModuleLibrary.of(descriptor_m1x); |
36508 | 137 |
|
43665
4bb003cad9b9
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42307
diff
changeset
|
138 |
// Resolves "m1x" |
44520
0553e129e0ec
8177530: Module system implementation refresh (4/2017)
alanb
parents:
43665
diff
changeset
|
139 |
Configuration cf = ModuleLayer.boot() |
36508 | 140 |
.configuration() |
43665
4bb003cad9b9
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42307
diff
changeset
|
141 |
.resolve(finder, ModuleFinder.of(), Set.of("m1x")); |
36508 | 142 |
|
143 |
MyDiffClassLoader.loader1 = new MyDiffClassLoader(); |
|
144 |
MyDiffClassLoader.loader2 = new MyDiffClassLoader(); |
|
145 |
||
43665
4bb003cad9b9
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42307
diff
changeset
|
146 |
// map module m1x to class loader. |
36508 | 147 |
// class c2 will be loaded in an unnamed module/loader2 |
148 |
// to achieve differing class loaders. |
|
149 |
Map<String, ClassLoader> map = new HashMap<>(); |
|
43665
4bb003cad9b9
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42307
diff
changeset
|
150 |
map.put("m1x", MyDiffClassLoader.loader1); |
36508 | 151 |
|
44520
0553e129e0ec
8177530: Module system implementation refresh (4/2017)
alanb
parents:
43665
diff
changeset
|
152 |
// Create layer that contains m1x |
0553e129e0ec
8177530: Module system implementation refresh (4/2017)
alanb
parents:
43665
diff
changeset
|
153 |
ModuleLayer layer = ModuleLayer.boot().defineModules(cf, map::get); |
36508 | 154 |
|
43665
4bb003cad9b9
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42307
diff
changeset
|
155 |
assertTrue(layer.findLoader("m1x") == MyDiffClassLoader.loader1); |
36508 | 156 |
assertTrue(layer.findLoader("java.base") == null); |
157 |
||
158 |
// now use the same loader to load class p1.c1ReadEdgeDiffLoader |
|
159 |
Class p1_c1_class = MyDiffClassLoader.loader1.loadClass("p1.c1ReadEdgeDiffLoader"); |
|
160 |
||
161 |
try { |
|
43665
4bb003cad9b9
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42307
diff
changeset
|
162 |
// Read edge between m1x and the unnamed module that loads p2.c2 is established in |
36508 | 163 |
// c1ReadEdgeDiffLoader's ctor before attempting access. |
164 |
p1_c1_class.newInstance(); |
|
165 |
} catch (IllegalAccessError e) { |
|
43665
4bb003cad9b9
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42307
diff
changeset
|
166 |
throw new RuntimeException("Test Failed, module m1x has established readability to p2/c2 loader's " + |
36508 | 167 |
"unnamed module, access should be allowed: " + e.getMessage()); |
168 |
} |
|
169 |
} |
|
170 |
||
43665
4bb003cad9b9
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42307
diff
changeset
|
171 |
// Module m1x is a loose module and thus can read all unnamed modules. |
36508 | 172 |
public void test_looseModuleLayer() throws Throwable { |
173 |
||
43665
4bb003cad9b9
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42307
diff
changeset
|
174 |
// Define module: m1x |
36508 | 175 |
// Can read: java.base |
176 |
// Packages: p1 |
|
177 |
// Packages exported: p1 is exported unqualifiedly |
|
43665
4bb003cad9b9
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42307
diff
changeset
|
178 |
ModuleDescriptor descriptor_m1x = |
4bb003cad9b9
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42307
diff
changeset
|
179 |
ModuleDescriptor.newModule("m1x") |
36508 | 180 |
.requires("java.base") |
181 |
.exports("p1") |
|
182 |
.build(); |
|
183 |
||
184 |
// Set up a ModuleFinder containing all modules for this layer. |
|
43665
4bb003cad9b9
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42307
diff
changeset
|
185 |
ModuleFinder finder = ModuleLibrary.of(descriptor_m1x); |
36508 | 186 |
|
43665
4bb003cad9b9
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42307
diff
changeset
|
187 |
// Resolves "m1x" |
44520
0553e129e0ec
8177530: Module system implementation refresh (4/2017)
alanb
parents:
43665
diff
changeset
|
188 |
Configuration cf = ModuleLayer.boot() |
36508 | 189 |
.configuration() |
43665
4bb003cad9b9
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42307
diff
changeset
|
190 |
.resolve(finder, ModuleFinder.of(), Set.of("m1x")); |
36508 | 191 |
|
192 |
MyDiffClassLoader.loader1 = new MyDiffClassLoader(); |
|
193 |
MyDiffClassLoader.loader2 = new MyDiffClassLoader(); |
|
194 |
||
43665
4bb003cad9b9
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42307
diff
changeset
|
195 |
// map module m1x to class loader. |
36508 | 196 |
// class c2 will be loaded in an unnamed module/loader2 |
197 |
// to achieve differing class loaders. |
|
198 |
Map<String, ClassLoader> map = new HashMap<>(); |
|
43665
4bb003cad9b9
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42307
diff
changeset
|
199 |
map.put("m1x", MyDiffClassLoader.loader1); |
36508 | 200 |
|
44520
0553e129e0ec
8177530: Module system implementation refresh (4/2017)
alanb
parents:
43665
diff
changeset
|
201 |
// Create layer that contains m1x |
0553e129e0ec
8177530: Module system implementation refresh (4/2017)
alanb
parents:
43665
diff
changeset
|
202 |
ModuleLayer layer = ModuleLayer.boot().defineModules(cf, map::get); |
36508 | 203 |
|
43665
4bb003cad9b9
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42307
diff
changeset
|
204 |
assertTrue(layer.findLoader("m1x") == MyDiffClassLoader.loader1); |
36508 | 205 |
assertTrue(layer.findLoader("java.base") == null); |
206 |
||
207 |
// now use the same loader to load class p1.c1Loose |
|
208 |
Class p1_c1_class = MyDiffClassLoader.loader1.loadClass("p1.c1Loose"); |
|
209 |
||
43665
4bb003cad9b9
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42307
diff
changeset
|
210 |
// change m1x to read all unnamed modules |
4bb003cad9b9
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42307
diff
changeset
|
211 |
Module m1x = layer.findModule("m1x").get(); |
4bb003cad9b9
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42307
diff
changeset
|
212 |
jdk.internal.module.Modules.addReadsAllUnnamed(m1x); |
36508 | 213 |
|
214 |
try { |
|
215 |
p1_c1_class.newInstance(); |
|
216 |
} catch (IllegalAccessError e) { |
|
43665
4bb003cad9b9
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42307
diff
changeset
|
217 |
throw new RuntimeException("Test Failed, loose module m1x should be able to access " + |
36508 | 218 |
"public type p2.c2 defined in unnamed module: " + e.getMessage()); |
219 |
} |
|
220 |
} |
|
221 |
||
222 |
public static void main(String args[]) throws Throwable { |
|
223 |
DiffCL_Umod test = new DiffCL_Umod(); |
|
224 |
test.test_strictModuleLayer(); // access denied |
|
225 |
test.test_strictModuleUnnamedReadableLayer(); // access allowed |
|
226 |
test.test_looseModuleLayer(); // access allowed |
|
227 |
} |
|
228 |
} |