1 /* 2 * Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved. 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. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @modules java.base/jdk.internal.misc 27 * @library /test/lib .. 28 * @build sun.hotspot.WhiteBox 29 * @compile/module=java.base java/lang/reflect/ModuleHelper.java 30 * @run main ClassFileInstaller sun.hotspot.WhiteBox 31 * sun.hotspot.WhiteBox$WhiteBoxPermission 32 * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI JVMDefineModule 33 */ 34 35 import static jdk.test.lib.Asserts.*; 36 import java.sql.Time; 37 38 public class JVMDefineModule { 39 40 public static void main(String args[]) throws Throwable { 41 MyClassLoader cl = new MyClassLoader(); 42 Object m; 43 44 // NULL classloader argument, expect success 45 m = ModuleHelper.ModuleObject("mymodule", null, new String[] { "mypackage" }); 46 assertNotNull(m, "Module should not be null"); 47 ModuleHelper.DefineModule(m, "9.0", "mymodule/here", new String[] { "mypackage" }); 48 49 /* Invalid test, won't compile. 50 // Invalid classloader argument, expect an IAE 51 try { 52 m = ModuleHelper.ModuleObject("mymodule_one", new Object(), new String[] { "mypackage1" }); 53 ModuleHelper.DefineModule(m, "9.0", "mymodule/here", new String[] { "mypackage1" }); 54 throw new RuntimeException("Failed to get expected IAE for bad loader"); 55 } catch(IllegalArgumentException e) { 56 // Expected 57 } 58 */ 59 60 // NULL package argument, should not throw an exception 61 m = ModuleHelper.ModuleObject("mymoduleTwo", cl, new String[] { "nullpkg" }); 62 assertNotNull(m, "Module should not be null"); 63 ModuleHelper.DefineModule(m, "9.0", "mymoduleTwo/here", null); 64 65 // Null module argument, expect an NPE 66 try { 67 ModuleHelper.DefineModule(null, "9.0", "mymodule/here", new String[] { "mypackage1" }); 68 throw new RuntimeException("Failed to get expected NPE for null module"); 69 } catch(NullPointerException e) { 70 if (!e.getMessage().contains("Null module object")) { 71 throw new RuntimeException("Failed to get expected IAE message for null module: " + e.getMessage()); 72 } 73 // Expected 74 } 75 76 // Invalid module argument, expect an IAE 77 try { 78 ModuleHelper.DefineModule(new Object(), "9.0", "mymodule/here", new String[] { "mypackage1" }); 79 throw new RuntimeException("Failed to get expected IAE or NPE for bad module"); 80 } catch(IllegalArgumentException e) { 81 if (!e.getMessage().contains("module is not an instance of type java.lang.reflect.Module")) { 82 throw new RuntimeException("Failed to get expected IAE message for bad module: " + e.getMessage()); 83 } 84 } 85 86 // NULL module name, expect an IAE or NPE 87 try { 88 m = ModuleHelper.ModuleObject(null, cl, new String[] { "mypackage2" }); 89 ModuleHelper.DefineModule(m, "9.0", "mymodule/here", new String[] { "mypackage2" }); 90 throw new RuntimeException("Failed to get expected NPE for NULL module"); 91 } catch(IllegalArgumentException e) { 92 // Expected 93 } catch(NullPointerException e) { 94 // Expected 95 } 96 97 // module name is java.base, expect an IAE 98 m = ModuleHelper.ModuleObject("java.base", cl, new String[] { "mypackage3" }); 99 try { 100 ModuleHelper.DefineModule(m, "9.0", "mymodule/here", new String[] { "mypackage3" }); 101 throw new RuntimeException("Failed to get expected IAE for java.base, not defined with boot class loader"); 102 } catch(IllegalArgumentException e) { 103 if (!e.getMessage().contains("Class loader must be the boot class loader")) { 104 throw new RuntimeException("Failed to get expected IAE message for java.base: " + e.getMessage()); 105 } 106 } 107 108 // Duplicates in package list, expect an IAE 109 m = ModuleHelper.ModuleObject("module.x", cl, new String[] { "mypackage4", "mypackage5" }); 110 try { 111 ModuleHelper.DefineModule(m, "9.0", "mymodule/here", new String[] { "mypackage4", "mypackage5", "mypackage4" }); 112 throw new RuntimeException("Failed to get IAE for duplicate packages"); 113 } catch(IllegalArgumentException e) { 114 if (!e.getMessage().contains("Duplicate package name")) { 115 throw new RuntimeException("Failed to get expected IAE message for duplicate package: " + e.getMessage()); 116 } 117 } 118 119 // Empty entry in package list, expect an IAE 120 m = ModuleHelper.ModuleObject("module.y", cl, new String[] { "mypackageX", "mypackageY" }); 121 try { 122 ModuleHelper.DefineModule(m, "9.0", "mymodule/here", new String[] { "mypackageX", "", "mypackageY" }); 123 throw new RuntimeException("Failed to get IAE for empty package"); 124 } catch(IllegalArgumentException e) { 125 if (!e.getMessage().contains("Invalid package name")) { 126 throw new RuntimeException("Failed to get expected IAE message empty package entry: " + e.getMessage()); 127 } 128 } 129 130 // Duplicate module name, expect an ISE 131 m = ModuleHelper.ModuleObject("Module_A", cl, new String[] { "mypackage6" }); 132 assertNotNull(m, "Module should not be null"); 133 ModuleHelper.DefineModule(m, "9.0", "module.name/here", new String[] { "mypackage6" }); 134 try { 135 ModuleHelper.DefineModule(m, "9.0", "module.name/here", new String[] { "mypackage6a" }); 136 throw new RuntimeException("Failed to get ISE for duplicate module"); 137 } catch(IllegalStateException e) { 138 if (!e.getMessage().contains("Module Module_A is already defined")) { 139 throw new RuntimeException("Failed to get expected ISE message for duplicate module: " + e.getMessage()); 140 } 141 } 142 143 // Package is already defined for class loader, expect an ISE 144 m = ModuleHelper.ModuleObject("dupl.pkg.module", cl, new String[] { "mypackage6b" }); 145 try { 146 ModuleHelper.DefineModule(m, "9.0", "module.name/here", new String[] { "mypackage6" }); 147 throw new RuntimeException("Failed to get ISE for existing package"); 148 } catch(IllegalStateException e) { 149 if (!e.getMessage().contains("Package mypackage6 for module dupl.pkg.module is already in another module, Module_A, defined to the class loader")) { 150 throw new RuntimeException("Failed to get expected ISE message for duplicate package: " + e.getMessage()); 151 } 152 } 153 154 // Empty module name, expect an IAE 155 try { 156 m = ModuleHelper.ModuleObject("", cl, new String[] { "mypackage8" }); 157 ModuleHelper.DefineModule(m, "9.0", "module.name/here", new String[] { "mypackage8" }); 158 throw new RuntimeException("Failed to get expected IAE for empty module name"); 159 } catch(IllegalArgumentException e) { 160 // Expected 161 } 162 163 // Module name with ';', not allowed in java source 164 try { 165 m = ModuleHelper.ModuleObject("bad;name", cl, new String[] { "mypackage9" }); 166 ModuleHelper.DefineModule(m, "9.0", "module.name/here", new String[] { "mypackage9" }); 167 throw new RuntimeException("Failed to get expected IAE for bad;name"); 168 } catch(IllegalArgumentException e) { 169 // Expected 170 } 171 172 // Module name with leading dot, not allowed in java source 173 try { 174 m = ModuleHelper.ModuleObject(".leadingdot", cl, new String[] { "mypackage9a" }); 175 ModuleHelper.DefineModule(m, "9.0", "module.name/here", new String[] { "mypackage9a" }); 176 throw new RuntimeException("Failed to get expected IAE for .leadingdot"); 177 } catch(IllegalArgumentException e) { 178 // Expected 179 } 180 181 // Module name with trailing dot, not allowed in java source 182 try { 183 m = ModuleHelper.ModuleObject("trailingdot.", cl, new String[] { "mypackage9b" }); 184 ModuleHelper.DefineModule(m, "9.0", "module.name/here", new String[] { "mypackage9b" }); 185 throw new RuntimeException("Failed to get expected IAE for trailingdot."); 186 } catch(IllegalArgumentException e) { 187 // Expected 188 } 189 190 // Module name with consecutive dots, not allowed in java source 191 try { 192 m = ModuleHelper.ModuleObject("trailingdot.", cl, new String[] { "mypackage9b" }); 193 ModuleHelper.DefineModule(m, "9.0", "module.name/here", new String[] { "mypackage9b" }); 194 throw new RuntimeException("Failed to get expected IAE for trailingdot."); 195 } catch(IllegalArgumentException e) { 196 // Expected 197 } 198 199 // module name with multiple dots, should be okay 200 m = ModuleHelper.ModuleObject("more.than.one.dat", cl, new String[] { "mypackage9d" }); 201 assertNotNull(m, "Module should not be null"); 202 ModuleHelper.DefineModule(m, "9.0", "module.name/here", new String[] { "mypackage9d" }); 203 204 // Zero length package list, should be okay 205 m = ModuleHelper.ModuleObject("zero.packages", cl, new String[] { }); 206 assertNotNull(m, "Module should not be null"); 207 ModuleHelper.DefineModule(m, "9.0", "module.name/here", new String[] { }); 208 209 // Invalid package name, expect an IAE 210 m = ModuleHelper.ModuleObject("moduleFive", cl, new String[] { "your.package" }); 211 try { 212 ModuleHelper.DefineModule(m, "9.0", "module.name/here", new String[] { "your.package" }); 213 throw new RuntimeException("Failed to get expected IAE for your.package"); 214 } catch(IllegalArgumentException e) { 215 if (!e.getMessage().contains("Invalid package name")) { 216 throw new RuntimeException("Failed to get expected IAE message for bad package name: " + e.getMessage()); 217 } 218 } 219 220 // Invalid package name, expect an IAE 221 m = ModuleHelper.ModuleObject("moduleSix", cl, new String[] { "foo" }); // Name irrelevant 222 try { 223 ModuleHelper.DefineModule(m, "9.0", "module.name/here", new String[] { ";your/package" }); 224 throw new RuntimeException("Failed to get expected IAE for ;your.package"); 225 } catch(IllegalArgumentException e) { 226 if (!e.getMessage().contains("Invalid package name")) { 227 throw new RuntimeException("Failed to get expected IAE message for bad package name: " + e.getMessage()); 228 } 229 } 230 231 // Invalid package name, expect an IAE 232 m = ModuleHelper.ModuleObject("moduleSeven", cl, new String[] { "foo" }); // Name irrelevant 233 try { 234 ModuleHelper.DefineModule(m, "9.0", "module.name/here", new String[] { "7[743" }); 235 throw new RuntimeException("Failed to get expected IAE for package 7[743"); 236 } catch(IllegalArgumentException e) { 237 if (!e.getMessage().contains("Invalid package name")) { 238 throw new RuntimeException("Failed to get expected IAE message for bad package name: " + e.getMessage()); 239 } 240 } 241 242 // Package named "java" defined to a class loader other than the boot or platform class loader, expect an IAE 243 m = ModuleHelper.ModuleObject("modulejavapkgOne", cl, new String[] { "java/foo" }); 244 try { 245 // module m is defined to an instance of MyClassLoader class loader 246 ModuleHelper.DefineModule(m, "9.0", "modulejavapkgOne", new String[] { "java/foo" }); 247 throw new RuntimeException("Failed to get expected IAE for package java/foo"); 248 } catch(IllegalArgumentException e) { 249 if (!e.getMessage().contains("prohibited package name")) { 250 throw new RuntimeException("Failed to get expected IAE message for prohibited package name: " + e.getMessage()); 251 } 252 } 253 254 // Package named "javabar" defined to a class loader other than the boot or platform class loader, should be ok 255 m = ModuleHelper.ModuleObject("modulejavapkgTwo", cl, new String[] { "javabar" }); 256 assertNotNull(m, "Module should not be null"); 257 ModuleHelper.DefineModule(m, "9.0", "modulejavapkgTwo", new String[] { "javabar" }); 258 259 // Package named "java" defined to the boot class loader, should be ok 260 // m's type is a java.lang.Object, module is java.base 261 // java.base module is defined to the boot loader 262 ClassLoader boot_loader = m.getClass().getClassLoader(); 263 m = ModuleHelper.ModuleObject("modulejavapkgThree", boot_loader, new String[] { "java/foo" }); 264 assertNotNull(m, "Module should not be null"); 265 ModuleHelper.DefineModule(m, "9.0", "modulejavapkgThree", new String[] { "java/foo" }); 266 267 // Package named "java" defined to the platform class loader, should be ok 268 // java.sql module defined to the platform class loader. 269 java.sql.Time jst = new java.sql.Time(45 * 1000); 270 ClassLoader platform_loader = jst.getClass().getClassLoader(); 271 m = ModuleHelper.ModuleObject("modulejavapkgFour", platform_loader, new String[] { "java/foo" }); 272 assertNotNull(m, "Module should not be null"); 273 ModuleHelper.DefineModule(m, "9.0", "modulejavapkgFour", new String[] { "java/foo" }); 274 275 // module version that is null, should be okay 276 m = ModuleHelper.ModuleObject("moduleEight", cl, new String[] { "a_package_8" }); 277 assertNotNull(m, "Module should not be null"); 278 ModuleHelper.DefineModule(m, null, "moduleEight/here", new String[] { "a_package_8" }); 279 280 // module version that is "", should be okay 281 m = ModuleHelper.ModuleObject("moduleNine", cl, new String[] { "a_package_9" }); 282 assertNotNull(m, "Module should not be null"); 283 ModuleHelper.DefineModule(m, "", "moduleNine/here", new String[] { "a_package_9" }); 284 285 // module location that is null, should be okay 286 m = ModuleHelper.ModuleObject("moduleTen", cl, new String[] { "a_package_10" }); 287 assertNotNull(m, "Module should not be null"); 288 ModuleHelper.DefineModule(m, "9.0", null, new String[] { "a_package_10" }); 289 290 // module location that is "", should be okay 291 m = ModuleHelper.ModuleObject("moduleEleven", cl, new String[] { "a_package_11" }); 292 assertNotNull(m, "Module should not be null"); 293 ModuleHelper.DefineModule(m, "9.0", "", new String[] { "a_package_11" }); 294 } 295 296 static class MyClassLoader extends ClassLoader { } 297 }