blob: 87dcfc0eedd6fac126d91f3fcbcdcbb4962a1d28 (
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
|
import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME) @interface Colored { String color(); }
public class AtTarget3 {
public static void main(String[]argv) {
new A().m();
new B().m();
new C().m();
}
}
@Colored(color="yellow")
class A {
public void m() { System.err.println("method"); }
}
class B extends A { }
@Colored(color="blue") class C extends B { }
aspect X {
int adviceexecutions = 0;
before(Colored c): call(* *(..)) && !within(X) && @target(c) {
System.err.println(c.color());
adviceexecutions++;
if (adviceexecutions == 1 && !c.color().equals("yellow"))
throw new RuntimeException("First advice execution, color should be yellow");
// The pointcut does 'match' on call to B.m() but at runtime the annotation check fails so the advice isn't run
if (adviceexecutions == 2 && !c.color().equals("blue"))
throw new RuntimeException("Second advice execution, color should be blue");
if (adviceexecutions > 2)
throw new RuntimeException("Advice should only run twice");
}
}
|