1 /* 2 * Copyright (c) 2016, 2018, 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 * @compile p2/c2.java 29 * @compile p4/c4.java 30 * @build sun.hotspot.WhiteBox 31 * @compile/module=java.base java/lang/ModuleHelper.java 32 * @run driver ClassFileInstaller sun.hotspot.WhiteBox 33 * sun.hotspot.WhiteBox$WhiteBoxPermission 34 * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI CCE_module_msg 35 */ 36 37 import java.io.*; 38 import java.net.URL; 39 import java.net.URLClassLoader; 40 import java.nio.file.Path; 41 import java.nio.file.Paths; 42 import static jdk.test.lib.Asserts.*; 43 44 // Test that the message in a runtime ClassCastException contains module info. 45 public class CCE_module_msg { 46 private static final Path CLASSES_DIR = Paths.get("classes"); 47 48 public static void main(String[] args) throws Throwable { 49 // Should not display version 50 invalidObjectToDerived(); 51 invalidTimeToDerived(); 52 invalidHeadersToDerived(); 53 // Should display version 54 invalidClassToString(); 55 // Should display customer class loader 56 invalidClassToStringCustomLoader(); 57 } 58 59 public static void invalidObjectToDerived() { 60 java.lang.Object instance = new java.lang.Object(); 61 int left = 23; 62 int right = 42; 63 try { 64 for (int i = 0; i < 1; i += 1) { 65 left = ((Derived) instance).method(left, right); 66 } 67 throw new RuntimeException("ClassCastException wasn't thrown, test failed."); 68 } catch (ClassCastException cce) { 69 System.out.println(cce.getMessage()); 70 if (!cce.getMessage().contains("java.base/java.lang.Object cannot be cast to Derived")) { 71 throw new RuntimeException("Wrong message: " + cce.getMessage()); 72 } 73 } 74 } 75 76 // Test with a non-upgradeable 'java.' module other than java.base. 77 public static void invalidTimeToDerived() { 78 java.sql.Time instance = new java.sql.Time(10000); 79 int left = 23; 80 int right = 42; 81 try { 82 for (int i = 0; i < 1; i += 1) { 83 left = ((Derived) (java.lang.Object)instance).method(left, right); 84 } 85 throw new RuntimeException("ClassCastException wasn't thrown, test failed."); 86 } catch (ClassCastException cce) { 87 System.out.println(cce.getMessage()); 88 if (!cce.getMessage().contains("java.sql/java.sql.Time cannot be cast to Derived")) { 89 throw new RuntimeException("Wrong message: " + cce.getMessage()); 90 } 91 } 92 } 93 94 // Test with a non-upgradeable 'jdk.' module. 95 public static void invalidHeadersToDerived() { 96 com.sun.net.httpserver.Headers instance = new com.sun.net.httpserver.Headers(); 97 int left = 23; 98 int right = 42; 99 try { 100 for (int i = 0; i < 1; i += 1) { 101 left = ((Derived) (java.lang.Object)instance).method(left, right); 102 } 103 throw new RuntimeException("ClassCastException wasn't thrown, test failed."); 104 } catch (ClassCastException cce) { 105 System.out.println(cce.getMessage()); 106 if (!cce.getMessage().contains("jdk.httpserver/com.sun.net.httpserver.Headers cannot be cast to Derived")) { 107 throw new RuntimeException("Wrong message: " + cce.getMessage()); 108 } 109 } 110 } 111 112 public static void invalidClassToString() throws Throwable { 113 // Get the java.lang.Module object for module java.base. 114 Class jlObject = Class.forName("java.lang.Object"); 115 Object jlObject_jlM = jlObject.getModule(); 116 assertNotNull(jlObject_jlM, "jlModule object of java.lang.Object should not be null"); 117 118 // Get the class loader for CCE_module_msg and assume it's also used to 119 // load classes p1.c1 and p2.c2. 120 ClassLoader this_cldr = CCE_module_msg.class.getClassLoader(); 121 122 // Define a module for p2. 123 Object m2x = ModuleHelper.ModuleObject("module_two", this_cldr, new String[] { "p2" }); 124 assertNotNull(m2x, "Module should not be null"); 125 ModuleHelper.DefineModule(m2x, false, "9.0", "m2x/there", new String[] { "p2" }); 126 ModuleHelper.AddReadsModule(m2x, jlObject_jlM); 127 128 try { 129 ModuleHelper.AddModuleExportsToAll(m2x, "p2"); 130 Object p2Obj = new p2.c2(); 131 System.out.println((String)p2Obj); 132 throw new RuntimeException("ClassCastException wasn't thrown, test failed."); 133 } catch (ClassCastException cce) { 134 String exception = cce.getMessage(); 135 System.out.println(exception); 136 if (exception.contains("module_two/p2.c2") || 137 !(exception.contains("module_two@") && 138 exception.contains("/p2.c2 cannot be cast to java.base/java.lang.String"))) { 139 throw new RuntimeException("Wrong message: " + exception); 140 } 141 } 142 } 143 144 public static void invalidClassToStringCustomLoader() throws Throwable { 145 // Get the java.lang.Module object for module java.base. 146 Class jlObject = Class.forName("java.lang.Object"); 147 Object jlObject_jlM = jlObject.getModule(); 148 assertNotNull(jlObject_jlM, "jlModule object of java.lang.Object should not be null"); 149 150 // Create a customer class loader to load class p4/c4. 151 URL[] urls = new URL[] { CLASSES_DIR.toUri().toURL() }; 152 ClassLoader parent = ClassLoader.getSystemClassLoader(); 153 MyURLClassLoader myCldr = new MyURLClassLoader("MyClassLoader", urls, parent); 154 155 try { 156 // Class p4.c4 should be defined to the unnamed module of myCldr 157 Class p4_c4_class = myCldr.loadClass("p4.c4"); 158 Object c4Obj = p4_c4_class.newInstance(); 159 System.out.println((String)c4Obj); 160 throw new RuntimeException("ClassCastException wasn't thrown, test failed."); 161 } catch (ClassCastException cce) { 162 String exception = cce.getMessage(); 163 System.out.println(exception); 164 if (!exception.contains("MyClassLoader//p4.c4 cannot be cast to java.base/java.lang.String")) { 165 throw new RuntimeException("Wrong message: " + exception); 166 } 167 } 168 } 169 } 170 171 class Derived extends java.lang.Object { 172 public int method(int left, int right) { 173 return right; 174 } 175 } 176 177 class MyURLClassLoader extends URLClassLoader { 178 public MyURLClassLoader(String name, 179 URL[] urls, 180 ClassLoader parent) { 181 super(name, urls, parent); 182 } 183 184 public Class loadClass(String name) throws ClassNotFoundException { 185 if (!name.equals("p4.c4")) { 186 return super.loadClass(name); 187 } 188 byte[] data = getClassData(name); 189 return defineClass(name, data, 0, data.length); 190 } 191 192 byte[] getClassData(String name) { 193 try { 194 String TempName = name.replaceAll("\\.", "/"); 195 String currentDir = System.getProperty("test.classes"); 196 String filename = currentDir + File.separator + TempName + ".class"; 197 FileInputStream fis = new FileInputStream(filename); 198 byte[] b = new byte[5000]; 199 int cnt = fis.read(b, 0, 5000); 200 byte[] c = new byte[cnt]; 201 for (int i=0; i<cnt; i++) c[i] = b[i]; 202 return c; 203 } catch (IOException e) { 204 return null; 205 } 206 } 207 }