blob: 07ba6b0407463997605fa6721f8033adaf7ca2f6 (
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
|
import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME) @Inherited @interface Colored { String color(); }
public class AtThis4 {
public static void main(String[]argv) {
new A().start();
new B().start();
new C().start();
X.verify();
}
}
@Colored(color="yellow")
class A {
public void start() { m();} // Can't match @this() on calls from static method,
// hence this start() method...
public void m() { System.err.println("method"); }
}
class B extends A { } // inherits yellow color :)
@Colored(color="blue") class C extends B { }
aspect X {
static int count = 0;
before(Colored c): call(* m(..)) && !within(X) && @this(c) {
System.err.println(c.color() + thisJoinPoint);
count++;
if (count == 1 && !c.color().equals("yellow"))
throw new RuntimeException("First advice execution, color should be yellow");
if (count == 2 && !c.color().equals("yellow"))
throw new RuntimeException("Second advice execution, color should be yellow");
if (count == 3 && !c.color().equals("blue"))
throw new RuntimeException("Third advice execution, color should be blue");
}
public static void verify() {
if (count!=3) throw new Error("Should be 3 runs: "+count);
}
}
|