summaryrefslogtreecommitdiffstats
path: root/tests/java5/annotations/binding/AtThis2.aj
blob: f8ceac43d0c8f401a37733f40511db99aa42b075 (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
58
59
60
61
62
import java.lang.annotation.*;

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

@Retention(RetentionPolicy.RUNTIME)
@interface Material { String material(); }

@Colored(color="yellow") @Material(material="wood")
public class AtThis2 {
  public static void main(String[]argv) {
    new AtThis2().start();
    new SubA().start();
    new SubB().start();
    X.verify();
  }

  @Colored(color="red")
  public void m() {
    System.err.println("method running\n");
  }
  public void start() { m(); }
}

@Material(material="metal") @Colored(color="green")
class SubA extends AtThis2 { }

@Material(material="jelly") @Colored(color="magenta")
class SubB extends SubA { }

aspect X {
  static int count = 0;

  before(Colored c,Material m): call(* m(..)) && !within(X) && @this(c) && @this(m) {
    System.err.println("advice running ("+c.color()+","+m.material()+")");
    count++;
    
    if (count== 1) {
      if (!c.color().equals("yellow"))
        throw new RuntimeException("First advice execution, color should be yellow:"+c.color());
      if (!m.material().equals("wood"))
        throw new RuntimeException("First advice execution, material should be wood:"+m.material());
    }
    if (count == 2) {
      if (!c.color().equals("green"))
        throw new RuntimeException("Second advice execution, color should be green");
      if (!m.material().equals("metal"))
        throw new RuntimeException("Second advice execution, material should be metal");
    }
    if (count == 3) {
      if (!c.color().equals("magenta"))
        throw new RuntimeException("Third advice execution, color should be magenta");
      if (!m.material().equals("jelly"))
        throw new RuntimeException("Third advice execution, material should be jelly");
    }
  }

 public static void verify() {
    if (count!=3) throw new Error("Should be 3 runs: "+count);
  }
}