Wed, 27 Apr 2011 17:03:24 -0700
7039822: Project Coin: add explicit tests for the lub of an exception parameter
Reviewed-by: mcimadamore, jjg
mcimadamore@550 | 1 | /* |
ohair@554 | 2 | * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. |
mcimadamore@550 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
mcimadamore@550 | 4 | * |
mcimadamore@550 | 5 | * This code is free software; you can redistribute it and/or modify it |
mcimadamore@550 | 6 | * under the terms of the GNU General Public License version 2 only, as |
mcimadamore@550 | 7 | * published by the Free Software Foundation. |
mcimadamore@550 | 8 | * |
mcimadamore@550 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
mcimadamore@550 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
mcimadamore@550 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
mcimadamore@550 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
mcimadamore@550 | 13 | * accompanied this code). |
mcimadamore@550 | 14 | * |
mcimadamore@550 | 15 | * You should have received a copy of the GNU General Public License version |
mcimadamore@550 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
mcimadamore@550 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
mcimadamore@550 | 18 | * |
ohair@554 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ohair@554 | 20 | * or visit www.oracle.com if you need additional information or have any |
ohair@554 | 21 | * questions. |
mcimadamore@550 | 22 | */ |
mcimadamore@550 | 23 | |
mcimadamore@550 | 24 | /* |
mcimadamore@550 | 25 | * @test |
mcimadamore@550 | 26 | * @bug 6943289 |
mcimadamore@550 | 27 | * @summary Project Coin: Improved Exception Handling for Java (aka 'multicatch') |
mcimadamore@550 | 28 | */ |
mcimadamore@550 | 29 | |
mcimadamore@550 | 30 | import java.lang.annotation.*; |
mcimadamore@550 | 31 | |
mcimadamore@550 | 32 | public class Pos04 { |
mcimadamore@550 | 33 | |
mcimadamore@550 | 34 | enum ExceptionKind { |
mcimadamore@550 | 35 | A(1), |
mcimadamore@550 | 36 | B(2), |
mcimadamore@550 | 37 | C(1); |
mcimadamore@550 | 38 | |
mcimadamore@550 | 39 | int expectedValue; |
mcimadamore@550 | 40 | |
mcimadamore@550 | 41 | ExceptionKind(int expectedValue) { |
mcimadamore@550 | 42 | this.expectedValue = expectedValue; |
mcimadamore@550 | 43 | } |
mcimadamore@550 | 44 | } |
mcimadamore@550 | 45 | |
mcimadamore@550 | 46 | @Retention(RetentionPolicy.RUNTIME) |
mcimadamore@550 | 47 | @interface CatchNumber { |
mcimadamore@550 | 48 | int value(); |
mcimadamore@550 | 49 | } |
mcimadamore@550 | 50 | |
mcimadamore@550 | 51 | @CatchNumber(1) |
mcimadamore@550 | 52 | static class A extends Exception { } |
mcimadamore@550 | 53 | |
mcimadamore@550 | 54 | @CatchNumber(2) |
mcimadamore@550 | 55 | static class B extends Exception { } |
mcimadamore@550 | 56 | |
mcimadamore@550 | 57 | @CatchNumber(1) |
mcimadamore@550 | 58 | static class C extends Exception { } |
mcimadamore@550 | 59 | |
mcimadamore@550 | 60 | static int sum = 0; |
mcimadamore@550 | 61 | |
mcimadamore@550 | 62 | public static void main(String[] args) { |
mcimadamore@550 | 63 | for (ExceptionKind ekind : ExceptionKind.values()) { |
mcimadamore@550 | 64 | test(ekind); |
mcimadamore@550 | 65 | } |
mcimadamore@550 | 66 | if (sum != 4) { |
mcimadamore@550 | 67 | throw new Error("bad checksum - expected:4, found:" + sum); |
mcimadamore@550 | 68 | } |
mcimadamore@550 | 69 | } |
mcimadamore@550 | 70 | |
mcimadamore@550 | 71 | public static void test(ExceptionKind ekind) { |
mcimadamore@550 | 72 | try { |
mcimadamore@550 | 73 | switch(ekind) { |
mcimadamore@550 | 74 | case A: throw new A(); |
mcimadamore@550 | 75 | case B: throw new B(); |
mcimadamore@550 | 76 | case C: throw new C(); |
mcimadamore@550 | 77 | } |
mcimadamore@550 | 78 | } catch(final A | C ex) {// Catch number 1 |
mcimadamore@550 | 79 | CatchNumber catchNumber = ex.getClass().getAnnotation(CatchNumber.class); |
mcimadamore@550 | 80 | if (catchNumber == null || catchNumber.value() != ekind.expectedValue) { |
mcimadamore@550 | 81 | throw new Error("was expecting 1 - got " + catchNumber); |
mcimadamore@550 | 82 | } |
mcimadamore@550 | 83 | sum += catchNumber.value(); |
mcimadamore@550 | 84 | } catch (final B ex) { // Catch number 2 |
mcimadamore@550 | 85 | CatchNumber catchNumber = ex.getClass().getAnnotation(CatchNumber.class); |
mcimadamore@550 | 86 | if (catchNumber == null || catchNumber.value() != ekind.expectedValue) { |
mcimadamore@550 | 87 | throw new Error("was expecting 2 - got " + catchNumber); |
mcimadamore@550 | 88 | } |
mcimadamore@550 | 89 | sum += catchNumber.value(); |
mcimadamore@550 | 90 | } |
mcimadamore@550 | 91 | } |
mcimadamore@550 | 92 | } |