1 /* 2 * Copyright (c) 2015, 2016, 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 * @modules java.base/jdk.internal.loader 28 * java.desktop 29 * @library /testlibrary 30 * @run main/othervm GetSysPkgTest 31 */ 32 33 import java.io.File; 34 import java.lang.reflect.InvocationTargetException; 35 import java.lang.reflect.Method; 36 import jdk.test.lib.*; 37 38 // Test that JVM get_system_package() returns the module location for defined packages. 39 public class GetSysPkgTest { 40 41 private static Object invoke(Method m, Object obj, Object... args) throws Throwable { 42 try { 43 return m.invoke(obj, args); 44 } catch (InvocationTargetException e) { 45 throw e.getCause(); 46 } 47 } 48 49 private static Method findMethod(String name) { 50 for (Method m : jdk.internal.loader.BootLoader.class.getDeclaredMethods()) { 51 if (m.getName().equals(name)) { 52 m.setAccessible(true); 53 return m; 54 } 55 } 56 throw new RuntimeException("Failed to find method " + name + " in java.lang.reflect.Module"); 57 } 58 59 // Throw RuntimeException if getSystemPackageLocation() does not return 60 // the expected location. 61 static void getPkg(String name, String expected_loc) throws Throwable { 62 String loc = (String)invoke(findMethod("getSystemPackageLocation"), null, name); 63 if (loc == null) { 64 if (expected_loc == null) return; 65 System.out.println("Expected location: " + expected_loc + 66 ", for package: " + name + ", got: null"); 67 } else if (expected_loc == null) { 68 System.out.println("Expected location: null, for package: " + 69 name + ", got: " + loc); 70 } else if (!loc.equals(expected_loc)) { 71 System.out.println("Expected location: " + 72 expected_loc + ", for package: " + name + ", got: " + loc); 73 } else { 74 return; 75 } 76 throw new RuntimeException(); 77 } 78 79 public static void main(String args[]) throws Throwable { 80 if (args.length == 0 || !args[0].equals("do_tests")) { 81 82 // Create a package found via -Xbootclasspath/a 83 String source = "package BootLdr_package; " + 84 "public class BootLdrPkg { " + 85 " public int mth() { return 4; } " + 86 "}"; 87 byte[] klassbuf = 88 InMemoryJavaCompiler.compile("BootLdr_package.BootLdrPkg", source); 89 ClassFileInstaller.writeClassToDisk("BootLdr_package/BootLdrPkg", klassbuf, "bl_dir"); 90 91 // Create a package found via -cp. 92 source = "package GetSysPkg_package; " + 93 "public class GetSysClass { " + 94 " public int mth() { return 4; } " + 95 "}"; 96 klassbuf = 97 InMemoryJavaCompiler.compile("GetSysPkg_package.GetSysClass", source); 98 ClassFileInstaller.writeClassToDisk("GetSysPkg_package/GetSysClass", klassbuf); 99 100 ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-Xbootclasspath/a:bl_dir", 101 "-XaddExports:java.base/jdk.internal.loader=ALL-UNNAMED", "-cp", "." + File.pathSeparator + 102 System.getProperty("test.classes"), "GetSysPkgTest", "do_tests"); 103 OutputAnalyzer output = new OutputAnalyzer(pb.start()); 104 output.shouldHaveExitValue(0); 105 return; 106 } 107 108 getPkg("java/lang", "jrt:/java.base"); 109 getPkg("javax/script", null); // Package not defined 110 111 // Test a package that does not yet have any referenced classes. 112 // Note: if another class in com/sun/crypto/provider/ happens to get 113 // loaded or if class PrivateKeyInfo disappears from this package 114 // then this test will fail. 115 getPkg("com/sun/crypto/provider", null); 116 // Now make sure a class in the package is referenced. 117 Class newClass = Class.forName("com.sun.crypto.provider.PrivateKeyInfo"); 118 getPkg("com/sun/crypto/provider", "jrt:/java.base"); 119 120 getPkg("java/nio/charset", "jrt:/java.base"); 121 122 // Test a package in a module not owned by boot loader. 123 Class clss = Class.forName("jdk.nio.zipfs.ZipPath"); 124 if (clss == null) 125 throw new RuntimeException("Could not find class jdk.nio.zipfs.ZipPath"); 126 getPkg("javax/activation", null); // Not owned by boot loader 127 128 // Test a package not in jimage file. 129 clss = Class.forName("GetSysPkg_package.GetSysClass"); 130 if (clss == null) 131 throw new RuntimeException("Could not find class GetSysPkg_package.GetSysClass"); 132 getPkg("GetSysPkg_package", null); 133 134 // Access a class with a package in a boot loader module other than java.base 135 clss = Class.forName("java.awt.Button"); 136 if (clss == null) 137 throw new RuntimeException("Could not find class java.awt.Button"); 138 getPkg("java/awt", "jrt:/java.desktop"); 139 140 // Test getting the package location from a class found via -Xbootclasspath/a 141 clss = Class.forName("BootLdr_package.BootLdrPkg"); 142 if (clss == null) 143 throw new RuntimeException("Could not find class BootLdr_package.BootLdrPkg"); 144 String bootldrPkg = (String)invoke(findMethod("getSystemPackageLocation"), null, "BootLdr_package"); 145 if (bootldrPkg == null) { 146 throw new RuntimeException("Expected BootLdr_package to return non-null value"); 147 } 148 if (!bootldrPkg.equals("bl_dir")) { 149 throw new RuntimeException("Expected BootLdr_package to return bl_dir, got: " + bootldrPkg); 150 } 151 152 // Test when package's class reference is an array. 153 // Note: if another class in javax/crypto happens to get loaded 154 // or if class AEADBadTagException disappears from this package 155 // then this test will fail. 156 getPkg("javax/crypto", null); 157 javax.crypto.AEADBadTagException[] blah = new javax.crypto.AEADBadTagException[3]; 158 getPkg("javax/crypto", "jrt:/java.base"); 159 160 } 161 }