summaryrefslogtreecommitdiffstats
path: root/tests/base/test121/Driver.java
blob: ff9fffb28579bc8ef6b0eaf1e8bbe04bc150c5c9 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import org.aspectj.testing.Tester;
public class Driver {
  
  public static void main(String[] args) { test(); }

  public static void test() {
    Foo.staticMethod();
    Foo.introducedStaticMethod();

    Foo foo = new Foo(10);

    foo.nonStaticMethod();
    foo.introducedNonStaticMethod();

      Tester.checkEqual(A.fooStaticCounter, 1, "A.fooStaticCounter");
      Tester.checkEqual(A.fooCounter, 1, "A.fooCounter");
      Tester.checkEqual(A.aStaticCounter, 1, "A.aStaticCounter");
      Tester.checkEqual(A.aCounter, 1, "A.aCounter");
      // these is only one constructor call, for Foo
      Tester.checkEqual(A.constructorCounter, 1, "constructor calls");
      // one for Foo, one for A
      Tester.checkEqual(A.initializationCounter, 2, "initializations");
      Tester.check(A.ranIntroducedConstructor, 
                    "no overriding of the real thing");
  }
}

class Foo { 
  
  static void staticMethod() { }
         void nonStaticMethod() { }
}

aspect A0_8beta1 {
    after() returning(): /*target(*) &&*/ call(new(int)) {
        A.constructorCounter++;
    }
    after() returning(): /*target(*) &&*/ initialization(new(..)) && !within(A0_8beta1) {
    	System.out.println("init at " + thisJoinPoint);
        A.initializationCounter++;
    }
    
    before(): within(Foo) && execution(static * Foo.*(..)) {
        A.fooStaticCounter++;
    }
    
    before(): within(A) && execution(static * Foo.*(..)) {
        A.aStaticCounter++;
    }
    
    before(): within(A) && execution(!static * Foo.*(..)) {
        A.aCounter++;
        System.out.println("external before advise on " + thisJoinPoint);
    }
}

aspect A pertarget(target(Foo)){
  
    static int constructorCounter = 0;
    static int initializationCounter = 0;
    static int aStaticCounter = 0;
    static int aCounter = 0;
    static int fooStaticCounter = 0;
    static int fooCounter = 0;

    static boolean ranIntroducedConstructor = false;

    //introduction Foo {
    static void Foo.introducedStaticMethod() {
        // System.out.println(thisJoinPoint.className +"."+ 
        // thisJoinPoint.methodName);
    }
    void Foo.introducedNonStaticMethod() {
        // System.out.println(thisJoinPoint.className +"."+ 
        // thisJoinPoint.methodName);
    }
    Foo.new(int n) { ranIntroducedConstructor = true; }

	// make sure advice doesn't go on the toString() method
	// this would result in an infinite recursion
	before(): within(Foo) && execution(!static * Foo.*(..)) {
	    fooCounter++;
	    //System.out.println("before advise on " + 
	    //thisJoinPoint.className +"."+ thisJoinPoint.methodName);
	}
	
	public A() { System.err.println("creating: " + this); }

    //XXX moved to other aspect, need to think about this...
    //before(): within(A) && executions(!static * Foo.*(..)) {
    //aCounter++;
    //System.out.println("before advise on " + thisJoinPoint);
    //}
}