langtools/test/tools/javac/TryWithResources/TwrSuppression.java
author darcy
Mon, 02 Aug 2010 13:35:39 -0700
changeset 6161 ab857bfb695a
child 7213 912fdd70fd2a
permissions -rw-r--r--
6971877: Project Coin: improve semantics of suppressed exceptions in try-with-resources Reviewed-by: jjb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6161
ab857bfb695a 6971877: Project Coin: improve semantics of suppressed exceptions in try-with-resources
darcy
parents:
diff changeset
     1
/*
ab857bfb695a 6971877: Project Coin: improve semantics of suppressed exceptions in try-with-resources
darcy
parents:
diff changeset
     2
 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
ab857bfb695a 6971877: Project Coin: improve semantics of suppressed exceptions in try-with-resources
darcy
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ab857bfb695a 6971877: Project Coin: improve semantics of suppressed exceptions in try-with-resources
darcy
parents:
diff changeset
     4
 *
ab857bfb695a 6971877: Project Coin: improve semantics of suppressed exceptions in try-with-resources
darcy
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
ab857bfb695a 6971877: Project Coin: improve semantics of suppressed exceptions in try-with-resources
darcy
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
ab857bfb695a 6971877: Project Coin: improve semantics of suppressed exceptions in try-with-resources
darcy
parents:
diff changeset
     7
 * published by the Free Software Foundation.
ab857bfb695a 6971877: Project Coin: improve semantics of suppressed exceptions in try-with-resources
darcy
parents:
diff changeset
     8
 *
ab857bfb695a 6971877: Project Coin: improve semantics of suppressed exceptions in try-with-resources
darcy
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
ab857bfb695a 6971877: Project Coin: improve semantics of suppressed exceptions in try-with-resources
darcy
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ab857bfb695a 6971877: Project Coin: improve semantics of suppressed exceptions in try-with-resources
darcy
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
ab857bfb695a 6971877: Project Coin: improve semantics of suppressed exceptions in try-with-resources
darcy
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
ab857bfb695a 6971877: Project Coin: improve semantics of suppressed exceptions in try-with-resources
darcy
parents:
diff changeset
    13
 * accompanied this code).
ab857bfb695a 6971877: Project Coin: improve semantics of suppressed exceptions in try-with-resources
darcy
parents:
diff changeset
    14
 *
ab857bfb695a 6971877: Project Coin: improve semantics of suppressed exceptions in try-with-resources
darcy
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
ab857bfb695a 6971877: Project Coin: improve semantics of suppressed exceptions in try-with-resources
darcy
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
ab857bfb695a 6971877: Project Coin: improve semantics of suppressed exceptions in try-with-resources
darcy
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ab857bfb695a 6971877: Project Coin: improve semantics of suppressed exceptions in try-with-resources
darcy
parents:
diff changeset
    18
 *
ab857bfb695a 6971877: Project Coin: improve semantics of suppressed exceptions in try-with-resources
darcy
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ab857bfb695a 6971877: Project Coin: improve semantics of suppressed exceptions in try-with-resources
darcy
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
ab857bfb695a 6971877: Project Coin: improve semantics of suppressed exceptions in try-with-resources
darcy
parents:
diff changeset
    21
 * questions.
ab857bfb695a 6971877: Project Coin: improve semantics of suppressed exceptions in try-with-resources
darcy
parents:
diff changeset
    22
 */
ab857bfb695a 6971877: Project Coin: improve semantics of suppressed exceptions in try-with-resources
darcy
parents:
diff changeset
    23
ab857bfb695a 6971877: Project Coin: improve semantics of suppressed exceptions in try-with-resources
darcy
parents:
diff changeset
    24
/*
ab857bfb695a 6971877: Project Coin: improve semantics of suppressed exceptions in try-with-resources
darcy
parents:
diff changeset
    25
 * @test
ab857bfb695a 6971877: Project Coin: improve semantics of suppressed exceptions in try-with-resources
darcy
parents:
diff changeset
    26
 * @bug 6971877
ab857bfb695a 6971877: Project Coin: improve semantics of suppressed exceptions in try-with-resources
darcy
parents:
diff changeset
    27
 * @author Joseph D. Darcy
ab857bfb695a 6971877: Project Coin: improve semantics of suppressed exceptions in try-with-resources
darcy
parents:
diff changeset
    28
 * @summary Verify a primary exception suppresses all throwables
ab857bfb695a 6971877: Project Coin: improve semantics of suppressed exceptions in try-with-resources
darcy
parents:
diff changeset
    29
 */
ab857bfb695a 6971877: Project Coin: improve semantics of suppressed exceptions in try-with-resources
darcy
parents:
diff changeset
    30
ab857bfb695a 6971877: Project Coin: improve semantics of suppressed exceptions in try-with-resources
darcy
parents:
diff changeset
    31
public class TwrSuppression implements AutoCloseable {
ab857bfb695a 6971877: Project Coin: improve semantics of suppressed exceptions in try-with-resources
darcy
parents:
diff changeset
    32
    public static void main(String... args) throws Throwable {
ab857bfb695a 6971877: Project Coin: improve semantics of suppressed exceptions in try-with-resources
darcy
parents:
diff changeset
    33
        try {
ab857bfb695a 6971877: Project Coin: improve semantics of suppressed exceptions in try-with-resources
darcy
parents:
diff changeset
    34
            try (TwrSuppression r1 = new TwrSuppression(false);
ab857bfb695a 6971877: Project Coin: improve semantics of suppressed exceptions in try-with-resources
darcy
parents:
diff changeset
    35
                 TwrSuppression r2 = new TwrSuppression(true)) {
ab857bfb695a 6971877: Project Coin: improve semantics of suppressed exceptions in try-with-resources
darcy
parents:
diff changeset
    36
                throw new RuntimeException();
ab857bfb695a 6971877: Project Coin: improve semantics of suppressed exceptions in try-with-resources
darcy
parents:
diff changeset
    37
            }
ab857bfb695a 6971877: Project Coin: improve semantics of suppressed exceptions in try-with-resources
darcy
parents:
diff changeset
    38
        } catch(RuntimeException e) {
ab857bfb695a 6971877: Project Coin: improve semantics of suppressed exceptions in try-with-resources
darcy
parents:
diff changeset
    39
            Throwable[] suppressedExceptions = e.getSuppressedExceptions();
ab857bfb695a 6971877: Project Coin: improve semantics of suppressed exceptions in try-with-resources
darcy
parents:
diff changeset
    40
            int length = suppressedExceptions.length;
ab857bfb695a 6971877: Project Coin: improve semantics of suppressed exceptions in try-with-resources
darcy
parents:
diff changeset
    41
            if (length != 2)
ab857bfb695a 6971877: Project Coin: improve semantics of suppressed exceptions in try-with-resources
darcy
parents:
diff changeset
    42
                throw new RuntimeException("Unexpected length " + length);
ab857bfb695a 6971877: Project Coin: improve semantics of suppressed exceptions in try-with-resources
darcy
parents:
diff changeset
    43
ab857bfb695a 6971877: Project Coin: improve semantics of suppressed exceptions in try-with-resources
darcy
parents:
diff changeset
    44
            if (suppressedExceptions[0].getClass() != Error.class ||
ab857bfb695a 6971877: Project Coin: improve semantics of suppressed exceptions in try-with-resources
darcy
parents:
diff changeset
    45
                suppressedExceptions[1].getClass() != Exception.class) {
ab857bfb695a 6971877: Project Coin: improve semantics of suppressed exceptions in try-with-resources
darcy
parents:
diff changeset
    46
                System.err.println("Unexpected suppressed types!");
ab857bfb695a 6971877: Project Coin: improve semantics of suppressed exceptions in try-with-resources
darcy
parents:
diff changeset
    47
                e.printStackTrace();
ab857bfb695a 6971877: Project Coin: improve semantics of suppressed exceptions in try-with-resources
darcy
parents:
diff changeset
    48
                throw new RuntimeException(e);
ab857bfb695a 6971877: Project Coin: improve semantics of suppressed exceptions in try-with-resources
darcy
parents:
diff changeset
    49
            }
ab857bfb695a 6971877: Project Coin: improve semantics of suppressed exceptions in try-with-resources
darcy
parents:
diff changeset
    50
        }
ab857bfb695a 6971877: Project Coin: improve semantics of suppressed exceptions in try-with-resources
darcy
parents:
diff changeset
    51
    }
ab857bfb695a 6971877: Project Coin: improve semantics of suppressed exceptions in try-with-resources
darcy
parents:
diff changeset
    52
ab857bfb695a 6971877: Project Coin: improve semantics of suppressed exceptions in try-with-resources
darcy
parents:
diff changeset
    53
    private boolean throwError;
ab857bfb695a 6971877: Project Coin: improve semantics of suppressed exceptions in try-with-resources
darcy
parents:
diff changeset
    54
ab857bfb695a 6971877: Project Coin: improve semantics of suppressed exceptions in try-with-resources
darcy
parents:
diff changeset
    55
    private TwrSuppression(boolean throwError) {
ab857bfb695a 6971877: Project Coin: improve semantics of suppressed exceptions in try-with-resources
darcy
parents:
diff changeset
    56
        this.throwError = throwError;
ab857bfb695a 6971877: Project Coin: improve semantics of suppressed exceptions in try-with-resources
darcy
parents:
diff changeset
    57
    }
ab857bfb695a 6971877: Project Coin: improve semantics of suppressed exceptions in try-with-resources
darcy
parents:
diff changeset
    58
ab857bfb695a 6971877: Project Coin: improve semantics of suppressed exceptions in try-with-resources
darcy
parents:
diff changeset
    59
    @Override
ab857bfb695a 6971877: Project Coin: improve semantics of suppressed exceptions in try-with-resources
darcy
parents:
diff changeset
    60
    public void close() throws Exception {
ab857bfb695a 6971877: Project Coin: improve semantics of suppressed exceptions in try-with-resources
darcy
parents:
diff changeset
    61
        if (throwError) {
ab857bfb695a 6971877: Project Coin: improve semantics of suppressed exceptions in try-with-resources
darcy
parents:
diff changeset
    62
            throw new Error();
ab857bfb695a 6971877: Project Coin: improve semantics of suppressed exceptions in try-with-resources
darcy
parents:
diff changeset
    63
        } else {
ab857bfb695a 6971877: Project Coin: improve semantics of suppressed exceptions in try-with-resources
darcy
parents:
diff changeset
    64
            throw new Exception();
ab857bfb695a 6971877: Project Coin: improve semantics of suppressed exceptions in try-with-resources
darcy
parents:
diff changeset
    65
        }
ab857bfb695a 6971877: Project Coin: improve semantics of suppressed exceptions in try-with-resources
darcy
parents:
diff changeset
    66
    }
ab857bfb695a 6971877: Project Coin: improve semantics of suppressed exceptions in try-with-resources
darcy
parents:
diff changeset
    67
}