mcimadamore@550: /* ohair@554: * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. mcimadamore@550: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. mcimadamore@550: * mcimadamore@550: * This code is free software; you can redistribute it and/or modify it mcimadamore@550: * under the terms of the GNU General Public License version 2 only, as mcimadamore@550: * published by the Free Software Foundation. mcimadamore@550: * mcimadamore@550: * This code is distributed in the hope that it will be useful, but WITHOUT mcimadamore@550: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or mcimadamore@550: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License mcimadamore@550: * version 2 for more details (a copy is included in the LICENSE file that mcimadamore@550: * accompanied this code). mcimadamore@550: * mcimadamore@550: * You should have received a copy of the GNU General Public License version mcimadamore@550: * 2 along with this work; if not, write to the Free Software Foundation, mcimadamore@550: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. mcimadamore@550: * ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@554: * or visit www.oracle.com if you need additional information or have any ohair@554: * questions. mcimadamore@550: */ mcimadamore@550: mcimadamore@550: /* mcimadamore@550: * @test mcimadamore@550: * @bug 6943289 mcimadamore@550: * @summary Project Coin: Improved Exception Handling for Java (aka 'multicatch') mcimadamore@550: */ mcimadamore@550: mcimadamore@550: import java.lang.annotation.*; mcimadamore@550: mcimadamore@550: public class Pos04 { mcimadamore@550: mcimadamore@550: enum ExceptionKind { mcimadamore@550: A(1), mcimadamore@550: B(2), mcimadamore@550: C(1); mcimadamore@550: mcimadamore@550: int expectedValue; mcimadamore@550: mcimadamore@550: ExceptionKind(int expectedValue) { mcimadamore@550: this.expectedValue = expectedValue; mcimadamore@550: } mcimadamore@550: } mcimadamore@550: mcimadamore@550: @Retention(RetentionPolicy.RUNTIME) mcimadamore@550: @interface CatchNumber { mcimadamore@550: int value(); mcimadamore@550: } mcimadamore@550: mcimadamore@550: @CatchNumber(1) mcimadamore@550: static class A extends Exception { } mcimadamore@550: mcimadamore@550: @CatchNumber(2) mcimadamore@550: static class B extends Exception { } mcimadamore@550: mcimadamore@550: @CatchNumber(1) mcimadamore@550: static class C extends Exception { } mcimadamore@550: mcimadamore@550: static int sum = 0; mcimadamore@550: mcimadamore@550: public static void main(String[] args) { mcimadamore@550: for (ExceptionKind ekind : ExceptionKind.values()) { mcimadamore@550: test(ekind); mcimadamore@550: } mcimadamore@550: if (sum != 4) { mcimadamore@550: throw new Error("bad checksum - expected:4, found:" + sum); mcimadamore@550: } mcimadamore@550: } mcimadamore@550: mcimadamore@550: public static void test(ExceptionKind ekind) { mcimadamore@550: try { mcimadamore@550: switch(ekind) { mcimadamore@550: case A: throw new A(); mcimadamore@550: case B: throw new B(); mcimadamore@550: case C: throw new C(); mcimadamore@550: } mcimadamore@550: } catch(final A | C ex) {// Catch number 1 mcimadamore@550: CatchNumber catchNumber = ex.getClass().getAnnotation(CatchNumber.class); mcimadamore@550: if (catchNumber == null || catchNumber.value() != ekind.expectedValue) { mcimadamore@550: throw new Error("was expecting 1 - got " + catchNumber); mcimadamore@550: } mcimadamore@550: sum += catchNumber.value(); mcimadamore@550: } catch (final B ex) { // Catch number 2 mcimadamore@550: CatchNumber catchNumber = ex.getClass().getAnnotation(CatchNumber.class); mcimadamore@550: if (catchNumber == null || catchNumber.value() != ekind.expectedValue) { mcimadamore@550: throw new Error("was expecting 2 - got " + catchNumber); mcimadamore@550: } mcimadamore@550: sum += catchNumber.value(); mcimadamore@550: } mcimadamore@550: } mcimadamore@550: }