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