1/*
2 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
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 6943289
27 * @summary Project Coin: Improved Exception Handling for Java (aka 'multicatch')
28 */
29
30import java.lang.annotation.*;
31
32public class Pos04 {
33
34    enum ExceptionKind {
35        A(1),
36        B(2),
37        C(1);
38
39        int expectedValue;
40
41        ExceptionKind(int expectedValue) {
42            this.expectedValue = expectedValue;
43        }
44    }
45
46    @Retention(RetentionPolicy.RUNTIME)
47    @interface CatchNumber {
48       int value();
49    }
50
51    @CatchNumber(1)
52    static class A extends Exception { }
53
54    @CatchNumber(2)
55    static class B extends Exception { }
56
57    @CatchNumber(1)
58    static class C extends Exception { }
59
60    static int sum = 0;
61
62    public static void main(String[] args) {
63        for (ExceptionKind ekind : ExceptionKind.values()) {
64            test(ekind);
65        }
66        if (sum != 4) {
67            throw new Error("bad checksum - expected:4, found:" + sum);
68        }
69    }
70
71    public static void test(ExceptionKind ekind) {
72        try {
73            switch(ekind) {
74                case A: throw new A();
75                case B: throw new B();
76                case C: throw new C();
77            }
78        } catch(final A | C ex) {// Catch number 1
79            CatchNumber catchNumber = ex.getClass().getAnnotation(CatchNumber.class);
80            if (catchNumber == null || catchNumber.value() != ekind.expectedValue) {
81                throw new Error("was expecting 1 - got " + catchNumber);
82            }
83            sum += catchNumber.value();
84        } catch (final B ex) { // Catch number 2
85            CatchNumber catchNumber = ex.getClass().getAnnotation(CatchNumber.class);
86            if (catchNumber == null || catchNumber.value() != ekind.expectedValue) {
87                throw new Error("was expecting 2 - got " + catchNumber);
88            }
89            sum += catchNumber.value();
90        }
91    }
92}
93