blob: 315cf4ae6638c01371df21f032f2eb95c11822a0 (
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
|
import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
@interface Colored { String color(); }
public class InitBinding {
public static void main(String[]argv) {
new A();
new B();
new C();
X.verifyRun();
}
}
class A {
@Colored(color="orange") public A() {}
}
class B {
@Colored(color="yellow") public B() {}
}
class C {
@Colored(color="brown") public C() {}
}
aspect X {
// Expected color order
static String exp[] = new String[]{"orange","yellow","brown"};
static int i = 0; // Count of advice executions
before(Colored c): preinitialization(new(..)) && !within(X) && @annotation(c) {
System.err.println(thisJoinPoint+" color="+c.color());
if (!c.color().equals(exp[i])) throw new RuntimeException("not "+exp[i]+"? "+c.color());
i++;
}
public static void verifyRun() {
if (X.i != exp.length)
throw new RuntimeException("Expected "+exp.length+" advice runs but did "+X.i);
}
}
//aspect X {
// int i = 0;
//
// before(Colored c): preinitialization(new(..)) && !within(X) && @annotation(c) {
// i++;
// System.err.println(thisJoinPoint+" color="+c.color());
// if (i==1 && !c.color().equals("orange")) throw new RuntimeException("First time through should be red, but is "+c.color());
// if (i==2 && !c.color().equals("yellow")) throw new RuntimeException("Second time through should be green, but is "+c.color());
// if (i==3 && !c.color().equals("brown")) throw new RuntimeException("Third time through should be blue, but is "+c.color());
// System.err.println(c.color());
// }
//}
//
|