summaryrefslogtreecommitdiffstats
path: root/tests/java5/annotations/binding/WithinBinding1.aj
blob: 68b617c0d4bfa5d8c571503fdcb2187d3fc60826 (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
import java.lang.annotation.*;

@Retention(RetentionPolicy.RUNTIME)
@interface Colored { String color();}

@Colored(color="red")
public class WithinBinding1 {

  void mRed() {System.out.println("red"); }

  void mBlue() {System.out.println("blue");}

  void mGreen() {System.out.println("green");}

  WithinBinding1() {
  	System.out.println("yellow");
  }

  public static void main(String[]argv) {
    WithinBinding1 instance = new WithinBinding1();
    instance.mRed();
    instance.mBlue();
    instance.mGreen();
    X.verifyRun();
  }
}

aspect X {

	static int maxruns = 21; // there are 21 join points in a run of WithinBinding1.main()
    
    static int i = 0; // Count of advice executions
    
    before(Colored c): @within(c) {
      System.err.println(thisJoinPoint+" color="+c.color());
    	if (!c.color().equals("red")) throw new RuntimeException("not red? "+c.color());
    	i++;
    }
    
    public static void verifyRun() {
    	if (X.i != maxruns)
    		throw new RuntimeException("Expected "+maxruns+" advice runs but did "+X.i);
    }
}