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 } | 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 } |