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