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