aboutsummaryrefslogtreecommitdiffstats
path: root/tests/new/AfterThrowing.java
blob: 69e5306a76932fe0ef75234e7ce422222d9b20e2 (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
91
92
import org.aspectj.testing.Tester;

import java.util.*;

public class AfterThrowing {
    public static void main(String[] args) { test(); }

    public static void test() {
        throwException(true);
        Tester.check("Throwable:throwException");
        Tester.check("Exception:throwException");

        throwRuntimeException(true);
        Tester.check("Throwable:throwRuntimeException");
        Tester.check("RuntimeException:throwRuntimeException");

        throwError(true);
        Tester.check("Throwable:throwError");

        throwMyException(true);
        Tester.check("Throwable:throwMyException");
        Tester.check("Exception:throwMyException");
        Tester.check("MyException:throwMyException");
    }

    static void throwNothing(boolean b) { }

    static void throwException(boolean b) throws Exception {
        if (b) throw new Exception();
        throwError(false);
    }

    static void throwRuntimeException(boolean b) {
        if (b) throw new RuntimeException();
    }

    static String throwError(boolean b) {
        int[] i = new int[10];
        // this line is to make sure ajc doesn't think it needs to worry about a 
        // CloneNotSupportedException when arrays are cloned
        i = (int[])i.clone();

        if (b) throw new Error();
        return "foo";
    }

    static Object throwMyException(boolean b) throws MyException {
        if (b) throw new MyException();
        return new Integer(10);
    }

    public static class MyException extends Exception { }
}


aspect A {
    pointcut throwerCut(): within(AfterThrowing) && execution(* *(boolean));


    after () throwing (Throwable t): throwerCut() { 
        Tester.note("Throwable:" + thisJoinPointStaticPart.getSignature().getName());
    }

    after () throwing (Exception t): throwerCut() { 
        Tester.note("Exception:" + thisJoinPoint.getSignature().getName());
    }

    after () throwing (RuntimeException t): throwerCut() { 
        Tester.note("RuntimeException:" + thisJoinPointStaticPart.getSignature().getName());
    }

    after () throwing (AfterThrowing.MyException t): throwerCut() { 
        Tester.note("MyException:" + thisJoinPoint.getSignature().getName());
    }

    pointcut catchThrowsFrom(): 
        within(AfterThrowing) && call(* AfterThrowing.*(boolean));


    declare soft: Throwable: catchThrowsFrom();

    Object around(): catchThrowsFrom() {
        try {
            return proceed();
        } catch (Throwable t) {
            //System.out.println("caught " + t);
            return null;
        }
    }
}