aboutsummaryrefslogtreecommitdiffstats
path: root/tests/new/TryAndProceed.java
blob: 42cebaeee65b5a8b5cc0ec632153e0441bd1da56 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import org.aspectj.testing.Tester;

public class TryAndProceed {
    public static void main(String[] args) {
        new C().mNoThrows();
        Tester.checkEqual(C.buf.toString(), "beforeAll:aroundAll:mNoThrows:");
        C.buf = new StringBuffer();

        A.aspectOf().allowThrowingAround = true;
        try {
            new C().mThrowsCheckedExc();
            Tester.checkFailed("should have thrown RuntimeExc");
        } catch (CheckedExc ce) {
            Tester.checkFailed("should have thrown RuntimeExc not " + ce);
        } catch (RuntimeException re) {
            //System.out.println("caught " + re);
        }
        Tester.checkEqual(C.buf.toString(), 
           "beforeAll:aroundCheckedNoThrow:aroundAll:aroundCheckedThrow:aroundCaughtCE:");
        C.buf = new StringBuffer();

        A.aspectOf().allowThrowingBefore = true;
        try {
            new C().mThrowsCheckedExc();
            Tester.checkFailed("should have thrown CheckedExc");
        } catch (CheckedExc ce) {
            //System.out.println("caught " + ce);
        } catch (RuntimeException re) {
            Tester.checkFailed("should have thrown CheckedExc not RuntimeExc");
        }
        Tester.checkEqual(C.buf.toString(), "beforeChecked:");

    }
}


class C {
    public static StringBuffer buf = new StringBuffer();

    public void mThrowsCheckedExc() throws CheckedExc {
        C.buf.append("mThrowsCheckedExc:");
    }

    public void mNoThrows() {
        C.buf.append("mNoThrows:");
    }
}

aspect A {
    pointcut checkedCut(): call(void C.mThrowsCheckedExc());
    pointcut uncheckedCut(): call(void C.mNoThrows());
    pointcut allCut(): checkedCut() || uncheckedCut();

    public static boolean allowThrowingBefore = false;
    public static boolean allowThrowingAround = false;

    before() throws CheckedExc: checkedCut() && if(allowThrowingBefore) {
        C.buf.append("beforeChecked:");
        throw new CheckedExc("from before");
    }

    before(): allCut() {
        C.buf.append("beforeAll:");
    }

    Object around(): checkedCut() {
        C.buf.append("aroundCheckedNoThrow:");
        return proceed();
    }

    Object around(): allCut() {
        C.buf.append("aroundAll:");
        try {
            return proceed();
        } catch (CheckedExc ce) {
            C.buf.append("aroundCaughtCE:");

            throw new RuntimeException("hand-softening CheckedExc");
        }
    }

    Object around() throws CheckedExc: checkedCut() && if(allowThrowingAround) {
        C.buf.append("aroundCheckedThrow:");
        throw new CheckedExc("from around");
    }
}

class CheckedExc extends Exception {
    public CheckedExc(String m) { super(m); }
}