author | jlahoda |
Tue, 12 Nov 2019 06:32:13 +0000 | |
changeset 59021 | cfc7bb9a5a92 |
parent 55306 | ea43db53de91 |
permissions | -rw-r--r-- |
52936 | 1 |
/* |
53878
4584d0331318
8219254: Update explicit uses of latest source/target in langtools tests to a property
darcy
parents:
53023
diff
changeset
|
2 |
* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved. |
52936 | 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 |
* @bug 8214114 |
|
27 |
* @summary Verify try-catch inside a switch expression works properly. |
|
59021 | 28 |
* @compile TryCatch.java |
29 |
* @run main TryCatch |
|
52936 | 30 |
*/ |
31 |
public class TryCatch { |
|
32 |
public static void main(String[] args) { |
|
33 |
{ |
|
34 |
int val = 3; |
|
35 |
for (int p : new int[] {0, 1, 2}) { |
|
36 |
int res = 1 + new TryCatch().id(switch(p) { |
|
37 |
case 0 -> switch (p + 1) { |
|
38 |
case 1: |
|
39 |
try { |
|
40 |
new TryCatch().throwException(); |
|
55306 | 41 |
yield -1; |
52936 | 42 |
} catch(Throwable ex) { |
55306 | 43 |
yield val; |
52936 | 44 |
} |
55306 | 45 |
default: yield -1; |
52936 | 46 |
}; |
47 |
case 1 -> { |
|
48 |
try { |
|
55306 | 49 |
yield new TryCatch().id(switch (p + 1) { |
52936 | 50 |
case 2: |
51 |
try { |
|
52 |
new TryCatch().throwException(); |
|
55306 | 53 |
yield -1; |
52936 | 54 |
} catch(Throwable ex) { |
55 |
throw ex; |
|
56 |
} |
|
55306 | 57 |
default: yield -1; |
52936 | 58 |
}); |
59 |
} catch(Throwable ex) { |
|
55306 | 60 |
yield val; |
52936 | 61 |
} |
62 |
} |
|
63 |
default -> { |
|
64 |
try { |
|
65 |
new TryCatch().throwException(); |
|
55306 | 66 |
yield -1; |
52936 | 67 |
} catch(Throwable ex) { |
55306 | 68 |
yield val; |
52936 | 69 |
} |
70 |
} |
|
71 |
} - 1); |
|
72 |
if (res != 3) { |
|
73 |
throw new AssertionError("Unexpected result: " + res); |
|
74 |
} |
|
75 |
} |
|
76 |
} |
|
77 |
{ |
|
78 |
int val = 3; |
|
79 |
for (int p : new int[] {0, 1, 2}) { |
|
80 |
int x; |
|
81 |
int res = new TryCatch().id(val == 3 && switch(p) { |
|
82 |
case 0 -> switch (p + 1) { |
|
83 |
case 1: |
|
84 |
try { |
|
85 |
new TryCatch().throwException(); |
|
55306 | 86 |
yield false; |
52936 | 87 |
} catch(Throwable ex) { |
55306 | 88 |
yield true; |
52936 | 89 |
} |
55306 | 90 |
default: yield false; |
52936 | 91 |
}; |
92 |
case 1 -> { |
|
93 |
try { |
|
55306 | 94 |
yield new TryCatch().id(switch (p + 1) { |
52936 | 95 |
case 2: |
96 |
try { |
|
97 |
new TryCatch().throwException(); |
|
55306 | 98 |
yield false; |
52936 | 99 |
} catch(Throwable ex) { |
100 |
throw ex; |
|
101 |
} |
|
55306 | 102 |
default: yield false; |
52936 | 103 |
}); |
104 |
} catch(Throwable ex) { |
|
55306 | 105 |
yield true; |
52936 | 106 |
} |
107 |
} |
|
108 |
default -> { |
|
109 |
try { |
|
110 |
new TryCatch().throwException(); |
|
55306 | 111 |
yield false; |
52936 | 112 |
} catch(Throwable ex) { |
55306 | 113 |
yield true; |
52936 | 114 |
} |
115 |
} |
|
116 |
} && (x = 1) == 1 && x == 1 ? val : -1); |
|
117 |
if (res != 3) { |
|
118 |
throw new AssertionError("Unexpected result: " + res); |
|
119 |
} |
|
120 |
} |
|
121 |
} |
|
122 |
{ |
|
123 |
int val = 3; |
|
124 |
for (E e : new E[] {E.A, E.B, E.C}) { |
|
125 |
int res = 1 + new TryCatch().id(switch(e) { |
|
126 |
case A -> switch (e.next()) { |
|
127 |
case B: |
|
128 |
try { |
|
129 |
new TryCatch().throwException(); |
|
55306 | 130 |
yield -1; |
52936 | 131 |
} catch(Throwable ex) { |
55306 | 132 |
yield val; |
52936 | 133 |
} |
55306 | 134 |
default: yield -1; |
52936 | 135 |
}; |
136 |
case B -> { |
|
137 |
try { |
|
55306 | 138 |
yield new TryCatch().id(switch (e.next()) { |
52936 | 139 |
case C: |
140 |
try { |
|
141 |
new TryCatch().throwException(); |
|
55306 | 142 |
yield -1; |
52936 | 143 |
} catch(Throwable ex) { |
144 |
throw ex; |
|
145 |
} |
|
55306 | 146 |
default: yield -1; |
52936 | 147 |
}); |
148 |
} catch(Throwable ex) { |
|
55306 | 149 |
yield val; |
52936 | 150 |
} |
151 |
} |
|
152 |
default -> { |
|
153 |
try { |
|
154 |
new TryCatch().throwException(); |
|
55306 | 155 |
yield -1; |
52936 | 156 |
} catch(Throwable ex) { |
55306 | 157 |
yield val; |
52936 | 158 |
} |
159 |
} |
|
160 |
} - 1); |
|
161 |
if (res != 3) { |
|
162 |
throw new AssertionError("Unexpected result: " + res); |
|
163 |
} |
|
164 |
} |
|
165 |
} |
|
166 |
{ |
|
167 |
int val = 3; |
|
168 |
for (E e : new E[] {E.A, E.B, E.C}) { |
|
169 |
int x; |
|
170 |
int res = new TryCatch().id(val == 3 && switch(e) { |
|
171 |
case A -> switch (e.next()) { |
|
172 |
case B: |
|
173 |
try { |
|
174 |
new TryCatch().throwException(); |
|
55306 | 175 |
yield false; |
52936 | 176 |
} catch(Throwable ex) { |
55306 | 177 |
yield true; |
52936 | 178 |
} |
55306 | 179 |
default: yield false; |
52936 | 180 |
}; |
181 |
case B -> { |
|
182 |
try { |
|
55306 | 183 |
yield new TryCatch().id(switch (e.next()) { |
52936 | 184 |
case C: |
185 |
try { |
|
186 |
new TryCatch().throwException(); |
|
55306 | 187 |
yield false; |
52936 | 188 |
} catch(Throwable ex) { |
189 |
throw ex; |
|
190 |
} |
|
55306 | 191 |
default: yield false; |
52936 | 192 |
}); |
193 |
} catch(Throwable ex) { |
|
55306 | 194 |
yield true; |
52936 | 195 |
} |
196 |
} |
|
197 |
default -> { |
|
198 |
try { |
|
199 |
new TryCatch().throwException(); |
|
55306 | 200 |
yield false; |
52936 | 201 |
} catch(Throwable ex) { |
55306 | 202 |
yield true; |
52936 | 203 |
} |
204 |
} |
|
205 |
} && (x = 1) == 1 && x == 1 ? val : -1); |
|
206 |
if (res != 3) { |
|
207 |
throw new AssertionError("Unexpected result: " + res); |
|
208 |
} |
|
209 |
} |
|
210 |
} |
|
211 |
{ |
|
212 |
int val = 3; |
|
213 |
for (String s : new String[] {"", "a", "b"}) { |
|
214 |
int res = 1 + new TryCatch().id(switch(s) { |
|
215 |
case "" -> switch (s + "c") { |
|
216 |
case "c": |
|
217 |
try { |
|
218 |
new TryCatch().throwException(); |
|
55306 | 219 |
yield -1; |
52936 | 220 |
} catch(Throwable ex) { |
55306 | 221 |
yield val; |
52936 | 222 |
} |
55306 | 223 |
default: yield -1; |
52936 | 224 |
}; |
225 |
case "a" -> { |
|
226 |
try { |
|
55306 | 227 |
yield new TryCatch().id(switch (s + "c") { |
52936 | 228 |
case "ac": |
229 |
try { |
|
230 |
new TryCatch().throwException(); |
|
55306 | 231 |
yield -1; |
52936 | 232 |
} catch(Throwable ex) { |
233 |
throw ex; |
|
234 |
} |
|
55306 | 235 |
default: yield -1; |
52936 | 236 |
}); |
237 |
} catch(Throwable ex) { |
|
55306 | 238 |
yield val; |
52936 | 239 |
} |
240 |
} |
|
241 |
default -> { |
|
242 |
try { |
|
243 |
new TryCatch().throwException(); |
|
55306 | 244 |
yield -1; |
52936 | 245 |
} catch(Throwable ex) { |
55306 | 246 |
yield val; |
52936 | 247 |
} |
248 |
} |
|
249 |
} - 1); |
|
250 |
if (res != 3) { |
|
251 |
throw new AssertionError("Unexpected result: " + res); |
|
252 |
} |
|
253 |
} |
|
254 |
} |
|
255 |
{ |
|
256 |
int val = 3; |
|
257 |
for (String s : new String[] {"", "a", "b"}) { |
|
258 |
int x; |
|
259 |
int res = new TryCatch().id(val == 3 && switch(s) { |
|
260 |
case "" -> switch (s + "c") { |
|
261 |
case "c": |
|
262 |
try { |
|
263 |
new TryCatch().throwException(); |
|
55306 | 264 |
yield false; |
52936 | 265 |
} catch(Throwable ex) { |
55306 | 266 |
yield true; |
52936 | 267 |
} |
55306 | 268 |
default: yield false; |
52936 | 269 |
}; |
270 |
case "a" -> { |
|
271 |
try { |
|
55306 | 272 |
yield new TryCatch().id(switch (s + "c") { |
52936 | 273 |
case "ac": |
274 |
try { |
|
275 |
new TryCatch().throwException(); |
|
55306 | 276 |
yield false; |
52936 | 277 |
} catch(Throwable ex) { |
278 |
throw ex; |
|
279 |
} |
|
55306 | 280 |
default: yield false; |
52936 | 281 |
}); |
282 |
} catch(Throwable ex) { |
|
55306 | 283 |
yield true; |
52936 | 284 |
} |
285 |
} |
|
286 |
default -> { |
|
287 |
try { |
|
288 |
new TryCatch().throwException(); |
|
55306 | 289 |
yield false; |
52936 | 290 |
} catch(Throwable ex) { |
55306 | 291 |
yield true; |
52936 | 292 |
} |
293 |
} |
|
294 |
} && (x = 1) == 1 && x == 1 ? val : -1); |
|
295 |
if (res != 3) { |
|
296 |
throw new AssertionError("Unexpected result: " + res); |
|
297 |
} |
|
298 |
} |
|
299 |
} |
|
300 |
||
301 |
{ |
|
302 |
int res = new FieldHolder().intTest; |
|
303 |
||
304 |
if (res != 3) { |
|
305 |
throw new AssertionError("Unexpected result: " + res); |
|
306 |
} |
|
307 |
} |
|
308 |
{ |
|
309 |
int res = FieldHolder.intStaticTest; |
|
310 |
||
311 |
if (res != 3) { |
|
312 |
throw new AssertionError("Unexpected result: " + res); |
|
313 |
} |
|
314 |
} |
|
315 |
{ |
|
316 |
boolean res = new FieldHolder().booleanTest; |
|
317 |
||
318 |
if (!res) { |
|
319 |
throw new AssertionError("Unexpected result: " + res); |
|
320 |
} |
|
321 |
} |
|
322 |
{ |
|
323 |
boolean res = FieldHolder.booleanStaticTest; |
|
324 |
||
325 |
if (!res) { |
|
326 |
throw new AssertionError("Unexpected result: " + res); |
|
327 |
} |
|
328 |
} |
|
329 |
} |
|
330 |
||
331 |
static class FieldHolder { |
|
332 |
private final int intTest = switch (0) { |
|
55306 | 333 |
case -1: yield -1; |
52936 | 334 |
default: |
335 |
try { |
|
55306 | 336 |
yield new TryCatch().id(switch (2) { |
52936 | 337 |
case 2: |
338 |
try { |
|
339 |
new TryCatch().throwException(); |
|
55306 | 340 |
yield -1; |
52936 | 341 |
} catch(Throwable ex) { |
342 |
throw ex; |
|
343 |
} |
|
55306 | 344 |
default: yield -1; |
52936 | 345 |
}); |
346 |
} catch(Throwable ex) { |
|
55306 | 347 |
yield 3; |
52936 | 348 |
} |
349 |
}; |
|
350 |
private static final int intStaticTest = switch (0) { |
|
55306 | 351 |
case -1: yield -1; |
52936 | 352 |
default: |
353 |
try { |
|
55306 | 354 |
yield new TryCatch().id(switch (2) { |
52936 | 355 |
case 2: |
356 |
try { |
|
357 |
new TryCatch().throwException(); |
|
55306 | 358 |
yield -1; |
52936 | 359 |
} catch(Throwable ex) { |
360 |
throw ex; |
|
361 |
} |
|
55306 | 362 |
default: yield -1; |
52936 | 363 |
}); |
364 |
} catch(Throwable ex) { |
|
55306 | 365 |
yield 3; |
52936 | 366 |
} |
367 |
}; |
|
368 |
private final boolean booleanTest = switch (0) { |
|
55306 | 369 |
case -1: yield false; |
52936 | 370 |
default: |
371 |
try { |
|
55306 | 372 |
yield new TryCatch().id(switch (2) { |
52936 | 373 |
case 2: |
374 |
try { |
|
375 |
new TryCatch().throwException(); |
|
55306 | 376 |
yield false; |
52936 | 377 |
} catch(Throwable ex) { |
378 |
throw ex; |
|
379 |
} |
|
55306 | 380 |
default: yield false; |
52936 | 381 |
}); |
382 |
} catch(Throwable ex) { |
|
55306 | 383 |
yield true; |
52936 | 384 |
} |
385 |
}; |
|
386 |
private static final boolean booleanStaticTest = switch (0) { |
|
55306 | 387 |
case -1: yield false; |
52936 | 388 |
default: |
389 |
try { |
|
55306 | 390 |
yield new TryCatch().id(switch (2) { |
52936 | 391 |
case 2: |
392 |
try { |
|
393 |
new TryCatch().throwException(); |
|
55306 | 394 |
yield false; |
52936 | 395 |
} catch(Throwable ex) { |
396 |
throw ex; |
|
397 |
} |
|
55306 | 398 |
default: yield false; |
52936 | 399 |
}); |
400 |
} catch(Throwable ex) { |
|
55306 | 401 |
yield true; |
52936 | 402 |
} |
403 |
}; |
|
404 |
} |
|
405 |
||
406 |
private int id(int i) { |
|
407 |
return i; |
|
408 |
} |
|
409 |
||
410 |
private boolean id(boolean b) { |
|
411 |
return b; |
|
412 |
} |
|
413 |
||
414 |
private void throwException() { |
|
415 |
throw new RuntimeException(); |
|
416 |
} |
|
417 |
enum E { |
|
418 |
A, B, C; |
|
419 |
public E next() { |
|
420 |
return values()[(ordinal() + 1) % values().length]; |
|
421 |
} |
|
422 |
} |
|
423 |
} |