64 public class UmodUPkg { |
64 public class UmodUPkg { |
65 |
65 |
66 // Create Layers over the boot layer to test different |
66 // Create Layers over the boot layer to test different |
67 // accessing scenarios of a named module to an unnamed module. |
67 // accessing scenarios of a named module to an unnamed module. |
68 |
68 |
69 // Module m1 is a strict module and has not established |
69 // Module m1x is a strict module and has not established |
70 // readability to an unnamed module that c4 is defined in. |
70 // readability to an unnamed module that c4 is defined in. |
71 public void test_strictModuleLayer() throws Throwable { |
71 public void test_strictModuleLayer() throws Throwable { |
72 |
72 |
73 // Define module: m1 |
73 // Define module: m1x |
74 // Can read: java.base |
74 // Can read: java.base |
75 // Packages: p3 |
75 // Packages: p3 |
76 // Packages exported: p3 is exported unqualifiedly |
76 // Packages exported: p3 is exported unqualifiedly |
77 ModuleDescriptor descriptor_m1 = |
77 ModuleDescriptor descriptor_m1x = |
78 ModuleDescriptor.module("m1") |
78 ModuleDescriptor.newModule("m1x") |
79 .requires("java.base") |
79 .requires("java.base") |
80 .exports("p3") |
80 .exports("p3") |
81 .build(); |
81 .build(); |
82 |
82 |
83 // Set up a ModuleFinder containing all modules for this layer. |
83 // Set up a ModuleFinder containing all modules for this layer. |
84 ModuleFinder finder = ModuleLibrary.of(descriptor_m1); |
84 ModuleFinder finder = ModuleLibrary.of(descriptor_m1x); |
85 |
85 |
86 // Resolves "m1" |
86 // Resolves "m1x" |
87 Configuration cf = Layer.boot() |
87 Configuration cf = Layer.boot() |
88 .configuration() |
88 .configuration() |
89 .resolveRequires(finder, ModuleFinder.of(), Set.of("m1")); |
89 .resolve(finder, ModuleFinder.of(), Set.of("m1x")); |
90 |
90 |
91 // map module m1 to class loader. |
91 // map module m1x to class loader. |
92 // class c4 will be loaded in an unnamed module/loader. |
92 // class c4 will be loaded in an unnamed module/loader. |
93 MySameClassLoader loader = new MySameClassLoader(); |
93 MySameClassLoader loader = new MySameClassLoader(); |
94 Map<String, ClassLoader> map = new HashMap<>(); |
94 Map<String, ClassLoader> map = new HashMap<>(); |
95 map.put("m1", loader); |
95 map.put("m1x", loader); |
96 |
96 |
97 // Create Layer that contains m1 |
97 // Create Layer that contains m1x |
98 Layer layer = Layer.boot().defineModules(cf, map::get); |
98 Layer layer = Layer.boot().defineModules(cf, map::get); |
99 |
99 |
100 assertTrue(layer.findLoader("m1") == loader); |
100 assertTrue(layer.findLoader("m1x") == loader); |
101 assertTrue(layer.findLoader("java.base") == null); |
101 assertTrue(layer.findLoader("java.base") == null); |
102 |
102 |
103 // now use the same loader to load class p3.c3 |
103 // now use the same loader to load class p3.c3 |
104 Class p3_c3_class = loader.loadClass("p3.c3"); |
104 Class p3_c3_class = loader.loadClass("p3.c3"); |
105 |
105 |
106 // Attempt access |
106 // Attempt access |
107 try { |
107 try { |
108 p3_c3_class.newInstance(); |
108 p3_c3_class.newInstance(); |
109 throw new RuntimeException("Test Failed, strict module m1, type p3.c3, should not be able to access " + |
109 throw new RuntimeException("Test Failed, strict module m1x, type p3.c3, should not be able to access " + |
110 "public type c4 defined in unnamed module"); |
110 "public type c4 defined in unnamed module"); |
111 } catch (IllegalAccessError e) { |
111 } catch (IllegalAccessError e) { |
112 } |
112 } |
113 } |
113 } |
114 |
114 |
115 // Module m1 is a strict module and has established |
115 // Module m1x is a strict module and has established |
116 // readability to an unnamed module that c4 is defined in. |
116 // readability to an unnamed module that c4 is defined in. |
117 public void test_strictModuleUnnamedReadableLayer() throws Throwable { |
117 public void test_strictModuleUnnamedReadableLayer() throws Throwable { |
118 |
118 |
119 // Define module: m1 |
119 // Define module: m1x |
120 // Can read: java.base |
120 // Can read: java.base |
121 // Packages: p3 |
121 // Packages: p3 |
122 // Packages exported: p3 is exported unqualifiedly |
122 // Packages exported: p3 is exported unqualifiedly |
123 ModuleDescriptor descriptor_m1 = |
123 ModuleDescriptor descriptor_m1x = |
124 ModuleDescriptor.module("m1") |
124 ModuleDescriptor.newModule("m1x") |
125 .requires("java.base") |
125 .requires("java.base") |
126 .exports("p3") |
126 .exports("p3") |
127 .build(); |
127 .build(); |
128 |
128 |
129 // Set up a ModuleFinder containing all modules for this layer. |
129 // Set up a ModuleFinder containing all modules for this layer. |
130 ModuleFinder finder = ModuleLibrary.of(descriptor_m1); |
130 ModuleFinder finder = ModuleLibrary.of(descriptor_m1x); |
131 |
131 |
132 // Resolves "m1" |
132 // Resolves "m1x" |
133 Configuration cf = Layer.boot() |
133 Configuration cf = Layer.boot() |
134 .configuration() |
134 .configuration() |
135 .resolveRequires(finder, ModuleFinder.of(), Set.of("m1")); |
135 .resolve(finder, ModuleFinder.of(), Set.of("m1x")); |
136 |
136 |
137 MySameClassLoader loader = new MySameClassLoader(); |
137 MySameClassLoader loader = new MySameClassLoader(); |
138 // map module m1 to class loader. |
138 // map module m1x to class loader. |
139 // class c4 will be loaded in an unnamed module/loader. |
139 // class c4 will be loaded in an unnamed module/loader. |
140 Map<String, ClassLoader> map = new HashMap<>(); |
140 Map<String, ClassLoader> map = new HashMap<>(); |
141 map.put("m1", loader); |
141 map.put("m1x", loader); |
142 |
142 |
143 // Create Layer that contains m1 |
143 // Create Layer that contains m1x |
144 Layer layer = Layer.boot().defineModules(cf, map::get); |
144 Layer layer = Layer.boot().defineModules(cf, map::get); |
145 |
145 |
146 assertTrue(layer.findLoader("m1") == loader); |
146 assertTrue(layer.findLoader("m1x") == loader); |
147 assertTrue(layer.findLoader("java.base") == null); |
147 assertTrue(layer.findLoader("java.base") == null); |
148 |
148 |
149 // now use the same loader to load class p3.c3ReadEdge |
149 // now use the same loader to load class p3.c3ReadEdge |
150 Class p3_c3_class = loader.loadClass("p3.c3ReadEdge"); |
150 Class p3_c3_class = loader.loadClass("p3.c3ReadEdge"); |
151 |
151 |
152 try { |
152 try { |
153 // Read edge between m1 and the unnamed module that loads c4 is established in |
153 // Read edge between m1x and the unnamed module that loads c4 is established in |
154 // c3ReadEdge's ctor before attempting access. |
154 // c3ReadEdge's ctor before attempting access. |
155 p3_c3_class.newInstance(); |
155 p3_c3_class.newInstance(); |
156 } catch (IllegalAccessError e) { |
156 } catch (IllegalAccessError e) { |
157 throw new RuntimeException("Test Failed, module m1, type p3.c3ReadEdge, has established readability to " + |
157 throw new RuntimeException("Test Failed, module m1x, type p3.c3ReadEdge, has established readability to " + |
158 "c4 loader's unnamed module, access should be allowed: " + e.getMessage()); |
158 "c4 loader's unnamed module, access should be allowed: " + e.getMessage()); |
159 } |
159 } |
160 } |
160 } |
161 |
161 |
162 public static void main(String args[]) throws Throwable { |
162 public static void main(String args[]) throws Throwable { |