48 import java.util.Map; |
48 import java.util.Map; |
49 import java.util.Set; |
49 import java.util.Set; |
50 import myloaders.MySameClassLoader; |
50 import myloaders.MySameClassLoader; |
51 |
51 |
52 // |
52 // |
53 // ClassLoader1 --> defines m1 --> packages p1 |
53 // ClassLoader1 --> defines m1x --> packages p1 |
54 // package p1 in m1 is exported unqualifiedly |
54 // package p1 in m1x is exported unqualifiedly |
55 // |
55 // |
56 // class p1.c1 defined in m1 tries to access p2.c2 defined in |
56 // class p1.c1 defined in m1x tries to access p2.c2 defined in |
57 // in unnamed module. |
57 // in unnamed module. |
58 // |
58 // |
59 // Three access attempts occur in this test: |
59 // Three access attempts occur in this test: |
60 // 1. The first access is not allowed because a strict module |
60 // 1. The first access is not allowed because a strict module |
61 // cannot read an unnamed module. |
61 // cannot read an unnamed module. |
62 // 2. In this scenario a strict module establishes readability |
62 // 2. In this scenario a strict module establishes readability |
63 // to the particular unnamed module it is trying to access. |
63 // to the particular unnamed module it is trying to access. |
64 // Access is allowed. |
64 // Access is allowed. |
65 // 3. Module m1 in the test_looseModuleLayer() method |
65 // 3. Module m1x in the test_looseModuleLayer() method |
66 // is transitioned to a loose module, access |
66 // is transitioned to a loose module, access |
67 // to all unnamed modules is allowed. |
67 // to all unnamed modules is allowed. |
68 // |
68 // |
69 public class Umod { |
69 public class Umod { |
70 |
70 |
71 // Create Layers over the boot layer to test different |
71 // Create Layers over the boot layer to test different |
72 // accessing scenarios of a named module to an unnamed module. |
72 // accessing scenarios of a named module to an unnamed module. |
73 |
73 |
74 // Module m1 is a strict module and has not established |
74 // Module m1x is a strict module and has not established |
75 // readability to an unnamed module that p2.c2 is defined in. |
75 // readability to an unnamed module that p2.c2 is defined in. |
76 public void test_strictModuleLayer() throws Throwable { |
76 public void test_strictModuleLayer() throws Throwable { |
77 |
77 |
78 // Define module: m1 |
78 // Define module: m1x |
79 // Can read: java.base |
79 // Can read: java.base |
80 // Packages: p1 |
80 // Packages: p1 |
81 // Packages exported: p1 is exported unqualifiedly |
81 // Packages exported: p1 is exported unqualifiedly |
82 ModuleDescriptor descriptor_m1 = |
82 ModuleDescriptor descriptor_m1x = |
83 ModuleDescriptor.module("m1") |
83 ModuleDescriptor.newModule("m1x") |
84 .requires("java.base") |
84 .requires("java.base") |
85 .exports("p1") |
85 .exports("p1") |
86 .build(); |
86 .build(); |
87 |
87 |
88 // Set up a ModuleFinder containing all modules for this layer. |
88 // Set up a ModuleFinder containing all modules for this layer. |
89 ModuleFinder finder = ModuleLibrary.of(descriptor_m1); |
89 ModuleFinder finder = ModuleLibrary.of(descriptor_m1x); |
90 |
90 |
91 // Resolves "m1" |
91 // Resolves "m1x" |
92 Configuration cf = Layer.boot() |
92 Configuration cf = Layer.boot() |
93 .configuration() |
93 .configuration() |
94 .resolveRequires(finder, ModuleFinder.of(), Set.of("m1")); |
94 .resolve(finder, ModuleFinder.of(), Set.of("m1x")); |
95 |
95 |
96 // map module m1 to class loader. |
96 // map module m1x to class loader. |
97 // class c2 will be loaded in an unnamed module/loader. |
97 // class c2 will be loaded in an unnamed module/loader. |
98 MySameClassLoader loader = new MySameClassLoader(); |
98 MySameClassLoader loader = new MySameClassLoader(); |
99 Map<String, ClassLoader> map = new HashMap<>(); |
99 Map<String, ClassLoader> map = new HashMap<>(); |
100 map.put("m1", loader); |
100 map.put("m1x", loader); |
101 |
101 |
102 // Create Layer that contains m1 |
102 // Create Layer that contains m1x |
103 Layer layer = Layer.boot().defineModules(cf, map::get); |
103 Layer layer = Layer.boot().defineModules(cf, map::get); |
104 |
104 |
105 assertTrue(layer.findLoader("m1") == loader); |
105 assertTrue(layer.findLoader("m1x") == loader); |
106 assertTrue(layer.findLoader("java.base") == null); |
106 assertTrue(layer.findLoader("java.base") == null); |
107 |
107 |
108 // now use the same loader to load class p1.c1 |
108 // now use the same loader to load class p1.c1 |
109 Class p1_c1_class = loader.loadClass("p1.c1"); |
109 Class p1_c1_class = loader.loadClass("p1.c1"); |
110 |
110 |
111 // Attempt access |
111 // Attempt access |
112 try { |
112 try { |
113 p1_c1_class.newInstance(); |
113 p1_c1_class.newInstance(); |
114 throw new RuntimeException("Test Failed, strict module m1, type p1.c1, should not be able " + |
114 throw new RuntimeException("Test Failed, strict module m1x, type p1.c1, should not be able " + |
115 "to access public type p2.c2 defined in unnamed module"); |
115 "to access public type p2.c2 defined in unnamed module"); |
116 } catch (IllegalAccessError e) { |
116 } catch (IllegalAccessError e) { |
117 } |
117 } |
118 } |
118 } |
119 |
119 |
120 // Module m1 is a strict module and has established |
120 // Module m1x is a strict module and has established |
121 // readability to an unnamed module that p2.c2 is defined in. |
121 // readability to an unnamed module that p2.c2 is defined in. |
122 public void test_strictModuleUnnamedReadableLayer() throws Throwable { |
122 public void test_strictModuleUnnamedReadableLayer() throws Throwable { |
123 |
123 |
124 // Define module: m1 |
124 // Define module: m1x |
125 // Can read: java.base |
125 // Can read: java.base |
126 // Packages: p1 |
126 // Packages: p1 |
127 // Packages exported: p1 is exported unqualifiedly |
127 // Packages exported: p1 is exported unqualifiedly |
128 ModuleDescriptor descriptor_m1 = |
128 ModuleDescriptor descriptor_m1x = |
129 ModuleDescriptor.module("m1") |
129 ModuleDescriptor.newModule("m1x") |
130 .requires("java.base") |
130 .requires("java.base") |
131 .exports("p1") |
131 .exports("p1") |
132 .build(); |
132 .build(); |
133 |
133 |
134 // Set up a ModuleFinder containing all modules for this layer. |
134 // Set up a ModuleFinder containing all modules for this layer. |
135 ModuleFinder finder = ModuleLibrary.of(descriptor_m1); |
135 ModuleFinder finder = ModuleLibrary.of(descriptor_m1x); |
136 |
136 |
137 // Resolves "m1" |
137 // Resolves "m1x" |
138 Configuration cf = Layer.boot() |
138 Configuration cf = Layer.boot() |
139 .configuration() |
139 .configuration() |
140 .resolveRequires(finder, ModuleFinder.of(), Set.of("m1")); |
140 .resolve(finder, ModuleFinder.of(), Set.of("m1x")); |
141 |
141 |
142 MySameClassLoader loader = new MySameClassLoader(); |
142 MySameClassLoader loader = new MySameClassLoader(); |
143 // map module m1 to class loader. |
143 // map module m1x to class loader. |
144 // class c2 will be loaded in an unnamed module/loader. |
144 // class c2 will be loaded in an unnamed module/loader. |
145 Map<String, ClassLoader> map = new HashMap<>(); |
145 Map<String, ClassLoader> map = new HashMap<>(); |
146 map.put("m1", loader); |
146 map.put("m1x", loader); |
147 |
147 |
148 // Create Layer that contains m1 |
148 // Create Layer that contains m1x |
149 Layer layer = Layer.boot().defineModules(cf, map::get); |
149 Layer layer = Layer.boot().defineModules(cf, map::get); |
150 |
150 |
151 assertTrue(layer.findLoader("m1") == loader); |
151 assertTrue(layer.findLoader("m1x") == loader); |
152 assertTrue(layer.findLoader("java.base") == null); |
152 assertTrue(layer.findLoader("java.base") == null); |
153 |
153 |
154 // now use the same loader to load class p1.c1ReadEdge |
154 // now use the same loader to load class p1.c1ReadEdge |
155 Class p1_c1_class = loader.loadClass("p1.c1ReadEdge"); |
155 Class p1_c1_class = loader.loadClass("p1.c1ReadEdge"); |
156 |
156 |
157 try { |
157 try { |
158 // Read edge between m1 and the unnamed module that loads p2.c2 is established in |
158 // Read edge between m1x and the unnamed module that loads p2.c2 is established in |
159 // c1ReadEdge's ctor before attempting access. |
159 // c1ReadEdge's ctor before attempting access. |
160 p1_c1_class.newInstance(); |
160 p1_c1_class.newInstance(); |
161 } catch (IllegalAccessError e) { |
161 } catch (IllegalAccessError e) { |
162 throw new RuntimeException("Test Failed, strict module m1, type p1.c1ReadEdge, should be able to acccess public type " + |
162 throw new RuntimeException("Test Failed, strict module m1x, type p1.c1ReadEdge, should be able to acccess public type " + |
163 "p2.c2 defined in unnamed module: " + e.getMessage()); |
163 "p2.c2 defined in unnamed module: " + e.getMessage()); |
164 } |
164 } |
165 } |
165 } |
166 |
166 |
167 // Module m1 is a loose module and thus can read all unnamed modules. |
167 // Module m1x is a loose module and thus can read all unnamed modules. |
168 public void test_looseModuleLayer() throws Throwable { |
168 public void test_looseModuleLayer() throws Throwable { |
169 |
169 |
170 // Define module: m1 |
170 // Define module: m1x |
171 // Can read: java.base |
171 // Can read: java.base |
172 // Packages: p1 |
172 // Packages: p1 |
173 // Packages exported: p1 is exported unqualifiedly |
173 // Packages exported: p1 is exported unqualifiedly |
174 ModuleDescriptor descriptor_m1 = |
174 ModuleDescriptor descriptor_m1x = |
175 ModuleDescriptor.module("m1") |
175 ModuleDescriptor.newModule("m1x") |
176 .requires("java.base") |
176 .requires("java.base") |
177 .exports("p1") |
177 .exports("p1") |
178 .build(); |
178 .build(); |
179 |
179 |
180 // Set up a ModuleFinder containing all modules for this layer. |
180 // Set up a ModuleFinder containing all modules for this layer. |
181 ModuleFinder finder = ModuleLibrary.of(descriptor_m1); |
181 ModuleFinder finder = ModuleLibrary.of(descriptor_m1x); |
182 |
182 |
183 // Resolves "m1" |
183 // Resolves "m1x" |
184 Configuration cf = Layer.boot() |
184 Configuration cf = Layer.boot() |
185 .configuration() |
185 .configuration() |
186 .resolveRequires(finder, ModuleFinder.of(), Set.of("m1")); |
186 .resolve(finder, ModuleFinder.of(), Set.of("m1x")); |
187 |
187 |
188 MySameClassLoader loader = new MySameClassLoader(); |
188 MySameClassLoader loader = new MySameClassLoader(); |
189 // map module m1 to class loader. |
189 // map module m1x to class loader. |
190 // class c2 will be loaded in an unnamed module/loader. |
190 // class c2 will be loaded in an unnamed module/loader. |
191 Map<String, ClassLoader> map = new HashMap<>(); |
191 Map<String, ClassLoader> map = new HashMap<>(); |
192 map.put("m1", loader); |
192 map.put("m1x", loader); |
193 |
193 |
194 // Create Layer that contains m1 |
194 // Create Layer that contains m1x |
195 Layer layer = Layer.boot().defineModules(cf, map::get); |
195 Layer layer = Layer.boot().defineModules(cf, map::get); |
196 |
196 |
197 assertTrue(layer.findLoader("m1") == loader); |
197 assertTrue(layer.findLoader("m1x") == loader); |
198 assertTrue(layer.findLoader("java.base") == null); |
198 assertTrue(layer.findLoader("java.base") == null); |
199 |
199 |
200 // now use the same loader to load class p1.c1Loose |
200 // now use the same loader to load class p1.c1Loose |
201 Class p1_c1_class = loader.loadClass("p1.c1Loose"); |
201 Class p1_c1_class = loader.loadClass("p1.c1Loose"); |
202 |
202 |
203 // change m1 to read all unnamed modules |
203 // change m1x to read all unnamed modules |
204 Module m1 = layer.findModule("m1").get(); |
204 Module m1x = layer.findModule("m1x").get(); |
205 jdk.internal.module.Modules.addReadsAllUnnamed(m1); |
205 jdk.internal.module.Modules.addReadsAllUnnamed(m1x); |
206 |
206 |
207 try { |
207 try { |
208 p1_c1_class.newInstance(); |
208 p1_c1_class.newInstance(); |
209 } catch (IllegalAccessError e) { |
209 } catch (IllegalAccessError e) { |
210 throw new RuntimeException("Test Failed, strict module m1, type p1.c1Loose, should be able to acccess public type " + |
210 throw new RuntimeException("Test Failed, strict module m1x, type p1.c1Loose, should be able to acccess public type " + |
211 "p2.c2 defined in unnamed module: " + e.getMessage()); |
211 "p2.c2 defined in unnamed module: " + e.getMessage()); |
212 } |
212 } |
213 } |
213 } |
214 |
214 |
215 public static void main(String args[]) throws Throwable { |
215 public static void main(String args[]) throws Throwable { |