aboutsummaryrefslogtreecommitdiffstats
path: root/tests/new/CatchAdvice.java
blob: 8d0b247f733926940b8135b0da661289482c2d80 (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
import org.aspectj.testing.Tester;

import org.aspectj.lang.reflect.*;

public aspect CatchAdvice {
    public static void main(String[] args) { test(); }

    public static void test() {
	new C().foo();
	Tester.check(ranAdvice1, "advice on *'s");
	Tester.check(ranAdvice2, "advice on C");
	Tester.check(!ranAdvice3, "advice on C and Error");
    }

    private static boolean ranAdvice1 = false;
    before(Throwable t): handler(Throwable+) && args(t){
	//int n = thisJoinPoint.parameters.length;
	//System.out.println("caught "+t+" "+n+" params");
	Tester.check(((CatchClauseSignature)thisJoinPoint.getSignature()).
                     getParameterType() == ArithmeticException.class,
                     "right catch clause");
	ranAdvice1 = true;
   }
    after(Throwable t): handler(Throwable+) && args(t) {
	//System.out.println("after catch");
    }

    static boolean ranAdvice2 = false;
    after(ArithmeticException t):
        this(C) && handler(ArithmeticException) && args(t) {
	//System.out.println("(in C) caught "+t);
	ranAdvice2 = true;
    }

    static boolean ranAdvice3 = false;
    after(Error e):
        within(C) && handler(Error+) && args(e){
	//System.out.println("(in C) caught "+e);
	ranAdvice3 = true;
    }
}

class C {
    public void foo() {
	int x=0;
	try {
	    int y = 1/x;
	} catch (ArithmeticException e) {
	    //System.out.println("caught arithmetic exception");
	}
    }
}