Ian Zerny | 9aafbd5 | 2022-04-29 13:15:17 +0200 | [diff] [blame] | 1 | // Copyright (c) 2022, the R8 project authors. Please see the AUTHORS file |
| 2 | // for details. All rights reserved. Use of this source code is governed by a |
| 3 | // BSD-style license that can be found in the LICENSE file. |
| 4 | package twraddsuppressed; |
| 5 | |
| 6 | import java.io.Closeable; |
| 7 | |
| 8 | public class TestClass { |
| 9 | |
| 10 | public static class MyClosable implements Closeable { |
| 11 | |
| 12 | @Override |
| 13 | public void close() { |
| 14 | throw new RuntimeException("CLOSE"); |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | public static void foo() { |
| 19 | throw new RuntimeException("FOO"); |
| 20 | } |
| 21 | |
| 22 | public static void bar() { |
Søren Gjesse | 2fb9d7f | 2024-10-02 08:59:50 +0200 | [diff] [blame] | 23 | // NOTE: The $closeResource helper is _only_ generated by the JDK-9 compiler. |
| 24 | // |
Ian Zerny | 9aafbd5 | 2022-04-29 13:15:17 +0200 | [diff] [blame] | 25 | // Use twr twice to have javac generate a shared $closeResource helper. |
| 26 | try (MyClosable closable = new MyClosable()) { |
| 27 | foo(); |
| 28 | } |
| 29 | try (MyClosable closable = new MyClosable()) { |
| 30 | foo(); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | public static void main(String[] args) { |
| 35 | try { |
| 36 | bar(); |
| 37 | } catch (Exception e) { |
| 38 | Throwable[] suppressed = e.getSuppressed(); |
| 39 | if (suppressed.length == 0) { |
| 40 | System.out.println("NONE"); |
| 41 | } else { |
| 42 | for (Throwable throwable : suppressed) { |
| 43 | System.out.println(throwable.getMessage()); |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | } |