21 * questions. |
21 * questions. |
22 */ |
22 */ |
23 |
23 |
24 /** |
24 /** |
25 * @test |
25 * @test |
26 * @run main/othervm CCE_module_msg |
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 |
27 */ |
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.*; |
28 |
44 |
29 // Test that the message in a runtime ClassCastException contains module info. |
45 // Test that the message in a runtime ClassCastException contains module info. |
30 public class CCE_module_msg { |
46 public class CCE_module_msg { |
|
47 private static final Path CLASSES_DIR = Paths.get("classes"); |
31 |
48 |
32 public static void main(String[] args) { |
49 public static void main(String[] args) throws Throwable { |
33 invalidCastTest(); |
50 // Should not display version |
|
51 invalidObjectToDerived(); |
|
52 // Should display version |
|
53 invalidClassToString(); |
|
54 // Should display customer class loader |
|
55 invalidClassToStringCustomLoader(); |
34 } |
56 } |
35 |
57 |
36 public static void invalidCastTest() { |
58 public static void invalidObjectToDerived() { |
37 java.lang.Object instance = new java.lang.Object(); |
59 java.lang.Object instance = new java.lang.Object(); |
38 int left = 23; |
60 int left = 23; |
39 int right = 42; |
61 int right = 42; |
40 try { |
62 try { |
41 for (int i = 0; i < 1; i += 1) { |
63 for (int i = 0; i < 1; i += 1) { |
42 left = ((Derived) instance).method(left, right); |
64 left = ((Derived) instance).method(left, right); |
43 } |
65 } |
44 throw new RuntimeException("ClassCastException wasn't thrown, test failed."); |
66 throw new RuntimeException("ClassCastException wasn't thrown, test failed."); |
45 } catch (ClassCastException cce) { |
67 } catch (ClassCastException cce) { |
46 System.out.println(cce.getMessage()); |
68 System.out.println(cce.getMessage()); |
47 if (!cce.getMessage().contains("java.lang.Object (in module: java.base) cannot be cast")) { |
69 if (!cce.getMessage().contains("java.base/java.lang.Object cannot be cast to Derived")) { |
48 throw new RuntimeException("Wrong message: " + cce.getMessage()); |
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); |
49 } |
129 } |
50 } |
130 } |
51 } |
131 } |
52 } |
132 } |
53 |
133 |
54 class Derived extends java.lang.Object { |
134 class Derived extends java.lang.Object { |
55 public int method(int left, int right) { |
135 public int method(int left, int right) { |
56 return right; |
136 return right; |
57 } |
137 } |
58 } |
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 } |