64 public class DiffCL_UmodUpkg { |
64 public class DiffCL_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 MyDiffClassLoader.loader1 = new MyDiffClassLoader(); |
91 MyDiffClassLoader.loader1 = new MyDiffClassLoader(); |
92 MyDiffClassLoader.loader2 = new MyDiffClassLoader(); |
92 MyDiffClassLoader.loader2 = new MyDiffClassLoader(); |
93 |
93 |
94 // map module m1 to class loader. |
94 // map module m1x to class loader. |
95 // class c2 will be loaded in an unnamed module/loader2 |
95 // class c2 will be loaded in an unnamed module/loader2 |
96 // to achieve differing class loaders. |
96 // to achieve differing class loaders. |
97 Map<String, ClassLoader> map = new HashMap<>(); |
97 Map<String, ClassLoader> map = new HashMap<>(); |
98 map.put("m1", MyDiffClassLoader.loader1); |
98 map.put("m1x", MyDiffClassLoader.loader1); |
99 |
99 |
100 // Create Layer that contains m1 |
100 // Create Layer that contains m1x |
101 Layer layer = Layer.boot().defineModules(cf, map::get); |
101 Layer layer = Layer.boot().defineModules(cf, map::get); |
102 |
102 |
103 assertTrue(layer.findLoader("m1") == MyDiffClassLoader.loader1); |
103 assertTrue(layer.findLoader("m1x") == MyDiffClassLoader.loader1); |
104 assertTrue(layer.findLoader("java.base") == null); |
104 assertTrue(layer.findLoader("java.base") == null); |
105 |
105 |
106 // now use the same loader to load class p3.c3 |
106 // now use the same loader to load class p3.c3 |
107 Class p3_c3_class = MyDiffClassLoader.loader1.loadClass("p3.c3"); |
107 Class p3_c3_class = MyDiffClassLoader.loader1.loadClass("p3.c3"); |
108 |
108 |
109 // Attempt access |
109 // Attempt access |
110 try { |
110 try { |
111 p3_c3_class.newInstance(); |
111 p3_c3_class.newInstance(); |
112 throw new RuntimeException("Test Failed, strict module m1 should not be able to access " + |
112 throw new RuntimeException("Test Failed, strict module m1x should not be able to access " + |
113 "public type c4 defined in unnamed module"); |
113 "public type c4 defined in unnamed module"); |
114 } catch (IllegalAccessError e) { |
114 } catch (IllegalAccessError e) { |
115 } |
115 } |
116 } |
116 } |
117 |
117 |
118 // Module m1 is a strict module and has established |
118 // Module m1x is a strict module and has established |
119 // readability to an unnamed module that c4 is defined in. |
119 // readability to an unnamed module that c4 is defined in. |
120 public void test_strictModuleUnnamedReadableLayer() throws Throwable { |
120 public void test_strictModuleUnnamedReadableLayer() throws Throwable { |
121 |
121 |
122 // Define module: m1 |
122 // Define module: m1x |
123 // Can read: java.base |
123 // Can read: java.base |
124 // Packages: p3 |
124 // Packages: p3 |
125 // Packages exported: p3 is exported unqualifiedly |
125 // Packages exported: p3 is exported unqualifiedly |
126 ModuleDescriptor descriptor_m1 = |
126 ModuleDescriptor descriptor_m1x = |
127 ModuleDescriptor.module("m1") |
127 ModuleDescriptor.newModule("m1x") |
128 .requires("java.base") |
128 .requires("java.base") |
129 .exports("p3") |
129 .exports("p3") |
130 .build(); |
130 .build(); |
131 |
131 |
132 // Set up a ModuleFinder containing all modules for this layer. |
132 // Set up a ModuleFinder containing all modules for this layer. |
133 ModuleFinder finder = ModuleLibrary.of(descriptor_m1); |
133 ModuleFinder finder = ModuleLibrary.of(descriptor_m1x); |
134 |
134 |
135 // Resolves "m1" |
135 // Resolves "m1x" |
136 Configuration cf = Layer.boot() |
136 Configuration cf = Layer.boot() |
137 .configuration() |
137 .configuration() |
138 .resolveRequires(finder, ModuleFinder.of(), Set.of("m1")); |
138 .resolve(finder, ModuleFinder.of(), Set.of("m1x")); |
139 |
139 |
140 MyDiffClassLoader.loader1 = new MyDiffClassLoader(); |
140 MyDiffClassLoader.loader1 = new MyDiffClassLoader(); |
141 MyDiffClassLoader.loader2 = new MyDiffClassLoader(); |
141 MyDiffClassLoader.loader2 = new MyDiffClassLoader(); |
142 |
142 |
143 // map module m1 to class loader. |
143 // map module m1x to class loader. |
144 // class c2 will be loaded in an unnamed module/loader2 |
144 // class c2 will be loaded in an unnamed module/loader2 |
145 // to achieve differing class loaders. |
145 // to achieve differing class loaders. |
146 Map<String, ClassLoader> map = new HashMap<>(); |
146 Map<String, ClassLoader> map = new HashMap<>(); |
147 map.put("m1", MyDiffClassLoader.loader1); |
147 map.put("m1x", MyDiffClassLoader.loader1); |
148 |
148 |
149 // Create Layer that contains m1 |
149 // Create Layer that contains m1x |
150 Layer layer = Layer.boot().defineModules(cf, map::get); |
150 Layer layer = Layer.boot().defineModules(cf, map::get); |
151 |
151 |
152 assertTrue(layer.findLoader("m1") == MyDiffClassLoader.loader1); |
152 assertTrue(layer.findLoader("m1x") == MyDiffClassLoader.loader1); |
153 assertTrue(layer.findLoader("java.base") == null); |
153 assertTrue(layer.findLoader("java.base") == null); |
154 |
154 |
155 // now use the same loader to load class p3.c3ReadEdgeDiffLoader |
155 // now use the same loader to load class p3.c3ReadEdgeDiffLoader |
156 Class p3_c3_class = MyDiffClassLoader.loader1.loadClass("p3.c3ReadEdgeDiffLoader"); |
156 Class p3_c3_class = MyDiffClassLoader.loader1.loadClass("p3.c3ReadEdgeDiffLoader"); |
157 |
157 |
158 try { |
158 try { |
159 // Read edge between m1 and the unnamed module that loads c4 is established in |
159 // Read edge between m1x and the unnamed module that loads c4 is established in |
160 // C3ReadEdgeDiffLoader's ctor before attempting access. |
160 // C3ReadEdgeDiffLoader's ctor before attempting access. |
161 p3_c3_class.newInstance(); |
161 p3_c3_class.newInstance(); |
162 } catch (IllegalAccessError e) { |
162 } catch (IllegalAccessError e) { |
163 throw new RuntimeException("Test Failed, module m1 has established readability to " + |
163 throw new RuntimeException("Test Failed, module m1x has established readability to " + |
164 "c4 loader's unnamed module, access should be allowed: " + e.getMessage()); |
164 "c4 loader's unnamed module, access should be allowed: " + e.getMessage()); |
165 } |
165 } |
166 } |
166 } |
167 |
167 |
168 public static void main(String args[]) throws Throwable { |
168 public static void main(String args[]) throws Throwable { |