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 invalidOriginalInnerToDerived(); 52 invalidTimeToDerived(); 53 invalidHeadersToDerived(); 54 // Should display version 55 invalidClassToString(); 56 // Should display customer class loader 57 invalidClassToStringCustomLoader(); 58 } 59 60 public static void invalidObjectToDerived() { 61 java.lang.Object instance = new java.lang.Object(); 62 int left = 23; 63 int right = 42; 64 try { 65 for (int i = 0; i < 1; i += 1) { 66 left = ((Derived) instance).method(left, right); 67 } 68 throw new RuntimeException("ClassCastException wasn't thrown, test failed."); 69 } catch (ClassCastException cce) { 70 System.out.println(cce.toString()); 71 if (!cce.getMessage().contains("class java.lang.Object cannot be cast to class Derived (java.lang.Object is in module java.base of loader 'bootstrap'; Derived is in unnamed module of loader 'app')")) { 72 throw new RuntimeException("Wrong message: " + cce.getMessage()); 73 } 74 } 75 } 76 77 public static void invalidOriginalInnerToDerived() { 78 OriginalInner instance = new OriginalInner(); 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.toString()); 88 if (!cce.getMessage().contains("class OriginalInner cannot be cast to class Derived (OriginalInner and Derived are in unnamed module of loader 'app')")) { 89 throw new RuntimeException("Wrong message: " + cce.getMessage()); 90 } 91 } 92 } 93 94 // Test with a non-upgradeable 'java.' module other than java.base. 95 public static void invalidTimeToDerived() { 96 java.sql.Time instance = new java.sql.Time(10000); 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.toString()); 106 if (!cce.getMessage().contains("class java.sql.Time cannot be cast to class Derived (java.sql.Time is in module java.sql of loader 'platform'; Derived is in unnamed module of loader 'app')")) { 107 throw new RuntimeException("Wrong message: " + cce.getMessage()); 108 } 109 } 110 } 111 112 // Test with a non-upgradeable 'jdk.' module. 113 public static void invalidHeadersToDerived() { 114 com.sun.net.httpserver.Headers instance = new com.sun.net.httpserver.Headers(); 115 int left = 23; 116 int right = 42; 117 try { 118 for (int i = 0; i < 1; i += 1) { 119 left = ((Derived) (java.lang.Object)instance).method(left, right); 120 } 121 throw new RuntimeException("ClassCastException wasn't thrown, test failed."); 122 } catch (ClassCastException cce) { 123 System.out.println(cce.toString()); 124 if (!cce.getMessage().contains("class com.sun.net.httpserver.Headers cannot be cast to class Derived (com.sun.net.httpserver.Headers is in module jdk.httpserver of loader 'platform'; Derived is in unnamed module of loader 'app')")) { 125 throw new RuntimeException("Wrong message: " + cce.getMessage()); 126 } 127 } 128 } 129 130 public static void invalidClassToString() throws Throwable { 131 // Get the java.lang.Module object for module java.base. 132 Class jlObject = Class.forName("java.lang.Object"); 133 Object jlObject_jlM = jlObject.getModule(); 134 assertNotNull(jlObject_jlM, "jlModule object of java.lang.Object should not be null"); 135 136 // Get the class loader for CCE_module_msg and assume it's also used to 137 // load classes p1.c1 and p2.c2. 138 ClassLoader this_cldr = CCE_module_msg.class.getClassLoader(); 139 140 // Define a module for p2. 141 Object m2x = ModuleHelper.ModuleObject("module_two", this_cldr, new String[] { "p2" }); 142 assertNotNull(m2x, "Module should not be null"); 143 ModuleHelper.DefineModule(m2x, false, "9.0", "m2x/there", new String[] { "p2" }); 144 ModuleHelper.AddReadsModule(m2x, jlObject_jlM); 145 146 try { 147 ModuleHelper.AddModuleExportsToAll(m2x, "p2"); 148 Object p2Obj = new p2.c2(); 149 System.out.println((String)p2Obj); 150 throw new RuntimeException("ClassCastException wasn't thrown, test failed."); 151 } catch (ClassCastException cce) { 152 String exception = cce.getMessage(); 153 System.out.println(cce.toString()); 154 if (!exception.contains("class p2.c2 cannot be cast to class java.lang.String (p2.c2 is in module module_two@") || 155 !exception.contains(" of loader 'app'; java.lang.String is in module java.base of loader 'bootstrap')")) { 156 throw new RuntimeException("Wrong message: " + exception); 157 } 158 } 159 } 160 161 public static void invalidClassToStringCustomLoader() throws Throwable { 162 // Get the java.lang.Module object for module java.base. 163 Class jlObject = Class.forName("java.lang.Object"); 164 Object jlObject_jlM = jlObject.getModule(); 165 assertNotNull(jlObject_jlM, "jlModule object of java.lang.Object should not be null"); 166 167 // Create a customer class loader to load class p4/c4. 168 URL[] urls = new URL[] { CLASSES_DIR.toUri().toURL() }; 169 ClassLoader parent = ClassLoader.getSystemClassLoader(); 170 MyURLClassLoader myCldr = new MyURLClassLoader("MyClassLoader", urls, parent); 171 172 try { 173 // Class p4.c4 should be defined to the unnamed module of myCldr 174 Class p4_c4_class = myCldr.loadClass("p4.c4"); 175 Object c4Obj = p4_c4_class.newInstance(); 176 System.out.println((String)c4Obj); 177 throw new RuntimeException("ClassCastException wasn't thrown, test failed."); 178 } catch (ClassCastException cce) { 179 String exception = cce.getMessage(); 180 System.out.println(cce.toString()); 181 if (!exception.contains("class p4.c4 cannot be cast to class java.lang.String (p4.c4 is in unnamed module of loader 'MyClassLoader' @") || 182 !exception.contains("; java.lang.String is in module java.base of loader 'bootstrap')")) { 183 throw new RuntimeException("Wrong message: " + exception); 184 } 185 } 186 } 187 } 188 189 class OriginalInner extends java.lang.Object { 190 public int method(int left, int right) { 191 return right; 192 } 193 } 194 195 class Derived extends java.lang.Object { 196 public int method(int left, int right) { 197 return right; 198 } 199 } 200 201 class MyURLClassLoader extends URLClassLoader { 202 public MyURLClassLoader(String name, 203 URL[] urls, 204 ClassLoader parent) { 205 super(name, urls, parent); 206 } 207 208 public Class loadClass(String name) throws ClassNotFoundException { 209 if (!name.equals("p4.c4")) { 210 return super.loadClass(name); 211 } 212 byte[] data = getClassData(name); 213 return defineClass(name, data, 0, data.length); 214 } 215 216 byte[] getClassData(String name) { 217 try { 218 String TempName = name.replaceAll("\\.", "/"); 219 String currentDir = System.getProperty("test.classes"); 220 String filename = currentDir + File.separator + TempName + ".class"; 221 FileInputStream fis = new FileInputStream(filename); 222 byte[] b = new byte[5000]; 223 int cnt = fis.read(b, 0, 5000); 224 byte[] c = new byte[cnt]; 225 for (int i=0; i<cnt; i++) c[i] = b[i]; 226 return c; 227 } catch (IOException e) { 228 return null; 229 } 230 } 231 }