summaryrefslogtreecommitdiffstats
path: root/tests/design/eachobject/Tricky2.java
blob: 1f7ca9a362ea5a4b19c50cd83447bc6827a05876 (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
import org.aspectj.testing.Tester;
import org.aspectj.runtime.NoAspectException;

public class Simple {
    public static void main(String[] args) {
	C c = new C();
	c.m();
	// aspect A is bound to the base class C
	Tester.checkEqual(c.history.toString(), "A.before, C.m, ", "C");

	SubC1 subc1 = new SubC1();
	subc1.m();
	// aspect A is also bound to the subclass SubC1
	//Tester.checkEqual(subc1.history.toString(), "A.before, SubC1.m, C.m, ", "SubC1");

	SubC2 subc2 = new SubC2();
	subc2.m();
	// aspect A is overriden by SubA on the class SubC2
	//Tester.checkEqual(subc2.history.toString(), "SubA.before, A.before, C.m, ", "SubC2");


	// retrieving the SubA aspect using its super's aspectOf static method
	//Tester.check(A.aspectOf(subc2) instanceof SubA, "A.aspectOf(subc2) instanceof SubA");

	// retrieving the SubA aspect using its own aspectOf method
	//Tester.check(SubA.aspectOf(subc2) instanceof SubA, "SubA.aspectOf(subc2) instanceof SubA");

	try {
	    // trying to retrieve a SubA aspect where only an A aspect is present
	    SubA subA = SubA.aspectOf(c);
	    Tester.checkFailed("SubA.aspectOf(c) should throw an exception");
	} catch (NoAspectException nae) {
	    Tester.note("NoAspectException caught");
	}

	Tester.check("NoAspectException caught");
    }
}


class C {
    // records what methods are called and advice are run
    public StringBuffer history = new StringBuffer();

    public void m() {
	history.append("C.m, ");
    }
}

class SubC1 extends C {
    public void m() {
	history.append("SubC1.m, ");
	super.m();
    }
}

class SubC2 extends C {
}


abstract aspect A {
    //StringBuffer history = null;
    after (Object object) returning (): instanceof(object) && receptions(new(..)) {
	//history = c.history;
	System.out.println("created a: " + object + " on a " + this);
    }

    abstract pointcut targets();

    before (): targets() {
       System.out.println("A.before, ");
    }
}

aspect SubA extends A of eachobject(instanceof(C)) {
    pointcut targets(): receptions(void m());
}