Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/langtools/jdk/jshell/ExceptionsTest.java
40931 views
1
/*
2
* Copyright (c) 2015, 2020, 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
* @summary Tests for exceptions
27
* @bug 8198801 8212167 8210527
28
* @modules jdk.compiler/com.sun.tools.javac.api
29
* jdk.compiler/com.sun.tools.javac.main
30
* jdk.jdeps/com.sun.tools.javap
31
* @library /tools/lib
32
* @build toolbox.ToolBox toolbox.JarTask toolbox.JavacTask
33
* @build KullaTesting TestingInputStream Compiler
34
* @run testng ExceptionsTest
35
*/
36
37
import java.io.IOException;
38
import java.io.PrintWriter;
39
import java.io.StringWriter;
40
import jdk.jshell.EvalException;
41
import jdk.jshell.JShellException;
42
import jdk.jshell.Snippet;
43
import jdk.jshell.SnippetEvent;
44
import jdk.jshell.UnresolvedReferenceException;
45
46
import java.nio.file.Path;
47
import java.nio.file.Paths;
48
49
import org.testng.annotations.Test;
50
51
import static org.testng.Assert.*;
52
53
@Test
54
public class ExceptionsTest extends KullaTesting {
55
56
private final Compiler compiler = new Compiler();
57
private final Path outDir = Paths.get("test_class_path");
58
59
public void throwUncheckedException() {
60
String message = "error_message";
61
SnippetEvent cr = assertEvalException("throw new RuntimeException(\"" + message + "\");");
62
assertExceptionMatch(cr,
63
new ExceptionInfo(RuntimeException.class, message,
64
newStackTraceElement("", "", cr.snippet(), 1)));
65
}
66
67
public void throwCheckedException() {
68
String message = "error_message";
69
SnippetEvent cr = assertEvalException("throw new Exception(\"" + message + "\");");
70
assertExceptionMatch(cr,
71
new ExceptionInfo(Exception.class, message,
72
newStackTraceElement("", "", cr.snippet(), 1)));
73
}
74
75
public void throwFromStaticMethodOfClass() {
76
String message = "error_message";
77
Snippet s1 = methodKey(assertEval("void f() { throw new RuntimeException(\"" + message + "\"); }"));
78
Snippet s2 = classKey(assertEval("class A { static void g() { f(); } }"));
79
SnippetEvent cr3 = assertEvalException("A.g();");
80
assertExceptionMatch(cr3,
81
new ExceptionInfo(RuntimeException.class, message,
82
newStackTraceElement("", "f", s1, 1),
83
newStackTraceElement("A", "g", s2, 1),
84
newStackTraceElement("", "", cr3.snippet(), 1)));
85
}
86
87
public void throwFromStaticMethodOfInterface() {
88
String message = "error_message";
89
Snippet s1 = methodKey(assertEval("void f() { throw new RuntimeException(\"" + message + "\"); }"));
90
Snippet s2 = classKey(assertEval("interface A { static void g() { f(); } }"));
91
SnippetEvent cr3 = assertEvalException("A.g();");
92
assertExceptionMatch(cr3,
93
new ExceptionInfo(RuntimeException.class, message,
94
newStackTraceElement("", "f", s1, 1),
95
newStackTraceElement("A", "g", s2, 1),
96
newStackTraceElement("", "", cr3.snippet(), 1)));
97
}
98
99
public void throwChained() {
100
String message1 = "error_message1";
101
String message2 = "error_message2";
102
Snippet s1 = methodKey(assertEval("void p() throws Exception { ((String) null).toString(); }"));
103
Snippet s2 = methodKey(assertEval("void n() throws Exception { try { p(); } catch (Exception ex) { throw new java.io.IOException(\"" + message2 + "\", ex); }}"));
104
Snippet s3 = methodKey(assertEval("void m() {\n"
105
+ "try { n(); }\n"
106
+ "catch (Exception ex) {\n"
107
+ " throw new RuntimeException(\"" + message1 + "\", ex);\n"
108
+ "}}"));
109
SnippetEvent cr4 = assertEvalException("m();");
110
assertExceptionMatch(cr4,
111
new ExceptionInfo(RuntimeException.class, message1,
112
new ExceptionInfo(IOException.class, message2,
113
new ExceptionInfo(NullPointerException.class, null,
114
newStackTraceElement("", "p", s1, 1),
115
newStackTraceElement("", "n", s2, 1),
116
newStackTraceElement("", "m", s3, 2),
117
newStackTraceElement("", "", cr4.snippet(), 1)),
118
newStackTraceElement("", "n", s2, 1),
119
newStackTraceElement("", "m", s3, 2),
120
newStackTraceElement("", "", cr4.snippet(), 1)),
121
newStackTraceElement("", "m", s3, 4),
122
newStackTraceElement("", "", cr4.snippet(), 1)));
123
}
124
125
public void throwChainedUnresolved() {
126
String message1 = "error_message1";
127
String message2 = "error_message2";
128
Snippet s1 = methodKey(assertEval("void p() throws Exception { ((String) null).toString(); }"));
129
Snippet s2 = methodKey(assertEval("void n() throws Exception { try { p(); } catch (Exception ex) { throw new java.io.IOException(\"" + message2 + "\", ex); }}"));
130
Snippet s3 = methodKey(assertEval("void m() {\n"
131
+ "try { n(); }\n"
132
+ "catch (Exception ex) {\n"
133
+ " throw new RuntimeException(\"" + message1 + "\", ex);\n"
134
+ "}}"));
135
getState().drop(s1);
136
SnippetEvent cr4 = assertEvalException("m();");
137
assertExceptionMatch(cr4,
138
new ExceptionInfo(RuntimeException.class, message1,
139
new UnresolvedExceptionInfo(s2,
140
newStackTraceElement("", "n", s2, 1),
141
newStackTraceElement("", "m", s3, 2),
142
newStackTraceElement("", "", cr4.snippet(), 1)),
143
newStackTraceElement("", "m", s3, 4),
144
newStackTraceElement("", "", cr4.snippet(), 1)));
145
}
146
147
public void throwFromConstructor() {
148
String message = "error_message";
149
Snippet s1 = methodKey(assertEval("void f() { throw new RuntimeException(\"" + message + "\"); }"));
150
Snippet s2 = classKey(assertEval("class A { A() { f(); } }"));
151
SnippetEvent cr3 = assertEvalException("new A();");
152
assertExceptionMatch(cr3,
153
new ExceptionInfo(RuntimeException.class, message,
154
newStackTraceElement("", "f", s1, 1),
155
newStackTraceElement("A", "<init>", s2, 1),
156
newStackTraceElement("", "", cr3.snippet(), 1)));
157
}
158
159
public void throwFromDefaultMethodOfInterface() {
160
String message = "error_message";
161
Snippet s1 = methodKey(assertEval("void f() { throw new RuntimeException(\"" + message + "\"); }"));
162
Snippet s2 = classKey(assertEval("interface A { default void g() { f(); } }"));
163
SnippetEvent cr3 = assertEvalException("new A() { }.g();");
164
assertExceptionMatch(cr3,
165
new ExceptionInfo(RuntimeException.class, message,
166
newStackTraceElement("", "f", s1, 1),
167
newStackTraceElement("A", "g", s2, 1),
168
newStackTraceElement("", "", cr3.snippet(), 1)));
169
}
170
171
public void throwFromLambda() {
172
String message = "lambda";
173
Snippet s1 = varKey(assertEval(
174
"Runnable run = () -> {\n" +
175
" throw new RuntimeException(\"" + message + "\");\n" +
176
"};"
177
));
178
SnippetEvent cr2 = assertEvalException("run.run();");
179
assertExceptionMatch(cr2,
180
new ExceptionInfo(RuntimeException.class, message,
181
newStackTraceElement("", "lambda$", s1, 2),
182
newStackTraceElement("", "", cr2.snippet(), 1)));
183
}
184
185
public void throwFromAnonymousClass() {
186
String message = "anonymous";
187
Snippet s1 = varKey(assertEval(
188
"Runnable run = new Runnable() {\n" +
189
" public void run() {\n"+
190
" throw new RuntimeException(\"" + message + "\");\n" +
191
" }\n" +
192
"};"
193
));
194
SnippetEvent cr2 = assertEvalException("run.run();");
195
assertExceptionMatch(cr2,
196
new ExceptionInfo(RuntimeException.class, message,
197
newStackTraceElement("1", "run", s1, 3),
198
newStackTraceElement("", "", cr2.snippet(), 1)));
199
}
200
201
public void throwFromLocalClass() {
202
String message = "local";
203
Snippet s1 = methodKey(assertEval(
204
"void f() {\n" +
205
" class A {\n" +
206
" void f() {\n"+
207
" throw new RuntimeException(\"" + message + "\");\n" +
208
" }\n" +
209
" }\n" +
210
" new A().f();\n" +
211
"}"
212
));
213
SnippetEvent cr2 = assertEvalException("f();");
214
assertExceptionMatch(cr2,
215
new ExceptionInfo(RuntimeException.class, message,
216
newStackTraceElement("1A", "f", s1, 4),
217
newStackTraceElement("", "f", s1, 7),
218
newStackTraceElement("", "", cr2.snippet(), 1)));
219
}
220
221
// test 8210527
222
public void throwFromWithoutSource() {
223
String message = "show this";
224
SnippetEvent se = assertEvalException("java.lang.reflect.Proxy.newProxyInstance(" +
225
"Thread.currentThread().getContextClassLoader(), new Class[] {}," +
226
"(p, m, a) -> { throw new IllegalStateException(\"" + message + "\"); }).hashCode()");
227
assertExceptionMatch(se,
228
new ExceptionInfo(IllegalStateException.class, message,
229
newStackTraceElement("", "lambda$do_it$$0", se.snippet(), 1),
230
new StackTraceElement("jdk.proxy1.$Proxy0", "hashCode", null, -1),
231
newStackTraceElement("", "", se.snippet(), 1)));
232
}
233
234
// test 8210527
235
public void throwFromNoSource() {
236
Path path = outDir.resolve("fail");
237
compiler.compile(path,
238
"package fail;\n" +
239
"public class Fail {\n" +
240
" static { int x = 1 / 0; }\n" +
241
"}\n");
242
addToClasspath(compiler.getPath(path));
243
SnippetEvent se = assertEvalException("Class.forName(\"fail.Fail\")");
244
assertExceptionMatch(se,
245
new ExceptionInfo(ExceptionInInitializerError.class, null,
246
new StackTraceElement("java.lang.Class", "forName0", "Class.java", -2),
247
new StackTraceElement("java.lang.Class", "forName", "Class.java", -2),
248
newStackTraceElement("", "", se.snippet(), 1)));
249
}
250
251
// test 8212167
252
public void throwLineFormat1() {
253
SnippetEvent se = assertEvalException(
254
"if (true) { \n" +
255
" int x = 10; \n" +
256
" int y = 10 / 0;}"
257
);
258
assertExceptionMatch(se,
259
new ExceptionInfo(ArithmeticException.class, "/ by zero",
260
newStackTraceElement("", "", se.snippet(), 3)));
261
}
262
263
public void throwLineFormat3() {
264
Snippet sp = methodKey(assertEval(
265
"int p() \n" +
266
" { return 4/0; }"));
267
Snippet sm = methodKey(assertEval(
268
"int m(int x)\n" +
269
" \n" +
270
" {\n" +
271
" return p() + x; \n" +
272
" }"));
273
Snippet sn = methodKey(assertEval(
274
"int n(int x) {\n" +
275
" try {\n" +
276
" return m(x);\n" +
277
" }\n" +
278
" catch (Throwable ex) {\n" +
279
" throw new IllegalArgumentException( \"GOT:\", ex);\n" +
280
" }\n" +
281
" }"));
282
SnippetEvent se = assertEvalException("n(33);");
283
assertExceptionMatch(se,
284
new ExceptionInfo(IllegalArgumentException.class, null,
285
new ExceptionInfo(ArithmeticException.class, "/ by zero",
286
newStackTraceElement("", "p", sp, 2),
287
newStackTraceElement("", "m", sm, 4),
288
newStackTraceElement("", "n", sn, 3),
289
newStackTraceElement("", "", se.snippet(), 1)),
290
newStackTraceElement("", "n", sn, 6),
291
newStackTraceElement("", "", se.snippet(), 1)));
292
}
293
294
@Test(enabled = false) // TODO 8129427
295
public void outOfMemory() {
296
assertEval("import java.util.*;");
297
assertEval("List<byte[]> list = new ArrayList<>();");
298
assertExecuteException("while (true) { list.add(new byte[10000]); }", OutOfMemoryError.class);
299
}
300
301
public void stackOverflow() {
302
assertEval("void f() { f(); }");
303
assertExecuteException("f();", StackOverflowError.class);
304
}
305
306
private StackTraceElement newStackTraceElement(String className, String methodName, Snippet key, int lineNumber) {
307
return new StackTraceElement(className, methodName, "#" + key.id(), lineNumber);
308
}
309
310
private static class AnyExceptionInfo {
311
312
public final StackTraceElement[] stackTraceElements;
313
314
public AnyExceptionInfo(StackTraceElement... stackTraceElements) {
315
this.stackTraceElements = stackTraceElements.length == 0 ? null : stackTraceElements;
316
}
317
}
318
319
private static class UnresolvedExceptionInfo extends AnyExceptionInfo {
320
321
public final Snippet sn;
322
323
public UnresolvedExceptionInfo(Snippet sn, StackTraceElement... stackTraceElements) {
324
super(stackTraceElements);
325
this.sn = sn;
326
}
327
}
328
329
private static class ExceptionInfo extends AnyExceptionInfo {
330
331
public final Class<? extends Throwable> exception;
332
public final String message;
333
public final AnyExceptionInfo cause;
334
335
public ExceptionInfo(Class<? extends Throwable> exception, String message,
336
StackTraceElement... stackTraceElements) {
337
this(exception, message, null, stackTraceElements);
338
}
339
340
public ExceptionInfo(Class<? extends Throwable> exception, String message,
341
AnyExceptionInfo cause, StackTraceElement... stackTraceElements) {
342
super(stackTraceElements);
343
this.exception = exception;
344
this.message = message;
345
this.cause = cause;
346
}
347
}
348
349
private void assertExecuteException(String input, Class<? extends Throwable> exception) {
350
assertExceptionMatch(assertEvalException(input), new ExceptionInfo(exception, null));
351
}
352
353
private void assertExceptionMatch(SnippetEvent cr, ExceptionInfo exceptionInfo) {
354
assertExceptionMatch(cr.exception(), cr.snippet().source(), exceptionInfo);
355
}
356
357
private void assertExceptionMatch(Throwable exception, String source, ExceptionInfo exceptionInfo) {
358
assertNotNull(exception, "Expected exception was not thrown: " + exceptionInfo.exception);
359
if (exception instanceof EvalException) {
360
EvalException ex = (EvalException) exception;
361
String actualException = ex.getExceptionClassName();
362
String expectedException = exceptionInfo.exception.getCanonicalName();
363
assertEquals(actualException, expectedException,
364
String.format("Given \"%s\" expected exception: %s, got: %s%nStack trace:%n%s",
365
source, expectedException, actualException, getStackTrace(ex)));
366
if (exceptionInfo.message != null) {
367
assertEquals(ex.getMessage(), exceptionInfo.message,
368
String.format("Given \"%s\" expected message: %s, got: %s",
369
source, exceptionInfo.message, ex.getMessage()));
370
}
371
assertStackMatch(ex, source, exceptionInfo);
372
if (exceptionInfo.cause != null) {
373
assertAnyExceptionMatch(exception.getCause(), exceptionInfo.cause);
374
}
375
} else {
376
fail("Unexpected exception: " + exception + " or exceptionInfo: " + exceptionInfo);
377
}
378
}
379
380
private void assertStackMatch(JShellException exception, String source, AnyExceptionInfo exceptionInfo) {
381
if (exceptionInfo.stackTraceElements != null) {
382
assertStackTrace(exception.getStackTrace(), exceptionInfo.stackTraceElements,
383
String.format("Given \"%s\"%nStack trace:%n%s%n",
384
source, getStackTrace(exception)));
385
}
386
}
387
388
private void assertAnyExceptionMatch(Throwable exception, AnyExceptionInfo exceptionInfo) {
389
if (exceptionInfo instanceof ExceptionInfo) {
390
assertExceptionMatch(exception, "", (ExceptionInfo) exceptionInfo);
391
} else {
392
assertTrue(exceptionInfo instanceof UnresolvedExceptionInfo, "Bad exceptionInfo: " + exceptionInfo);
393
assertTrue(exception instanceof UnresolvedReferenceException,
394
"Expected UnresolvedReferenceException: " + exception);
395
UnresolvedExceptionInfo uei = (UnresolvedExceptionInfo) exceptionInfo;
396
UnresolvedReferenceException ure = (UnresolvedReferenceException) exception;
397
assertEquals(ure.getSnippet(), uei.sn);
398
assertStackMatch(ure, "", exceptionInfo);
399
}
400
}
401
402
private void assertStackTrace(StackTraceElement[] actual, StackTraceElement[] expected, String message) {
403
if (actual != expected) {
404
if (actual == null || expected == null) {
405
fail(message);
406
} else {
407
assertEquals(actual.length, expected.length, message + " : arrays do not have the same size");
408
for (int i = 0; i < actual.length; ++i) {
409
StackTraceElement actualElement = actual[i];
410
StackTraceElement expectedElement = expected[i];
411
assertEquals(actualElement.getClassName(), expectedElement.getClassName(), message + " : class names [" + i + "]");
412
String expectedMethodName = expectedElement.getMethodName();
413
if (expectedMethodName.startsWith("lambda$")) {
414
assertTrue(actualElement.getMethodName().startsWith("lambda$"), message + " : method names");
415
} else {
416
assertEquals(actualElement.getMethodName(), expectedElement.getMethodName(), message + " : method names [" + i + "]");
417
}
418
assertEquals(actualElement.getFileName(), expectedElement.getFileName(), message + " : file names [" + i + "]");
419
if (expectedElement.getLineNumber() >= 0) {
420
assertEquals(actualElement.getLineNumber(), expectedElement.getLineNumber(), message + " : line numbers [" + i + "]"
421
+ " -- actual: " + actualElement.getLineNumber() + ", expected: " + expectedElement.getLineNumber() +
422
" -- in: " + actualElement.getClassName());
423
}
424
}
425
}
426
}
427
}
428
429
private String getStackTrace(Throwable ex) {
430
StringWriter st = new StringWriter();
431
ex.printStackTrace(new PrintWriter(st));
432
return st.toString();
433
}
434
}
435
436