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