diff options
author | wisberg <wisberg> | 2002-12-16 18:51:06 +0000 |
---|---|---|
committer | wisberg <wisberg> | 2002-12-16 18:51:06 +0000 |
commit | 144143c2970a1e874d74cdbd0f8c622d4282a3c3 (patch) | |
tree | b12383d3d9e76c7e1f25f7fbec83051ef17f81fb /tests/design/eachobject | |
parent | fafae443719b26159ab2d7dac1c9b46b5e00b671 (diff) | |
download | aspectj-144143c2970a1e874d74cdbd0f8c622d4282a3c3.tar.gz aspectj-144143c2970a1e874d74cdbd0f8c622d4282a3c3.zip |
initial version
Diffstat (limited to 'tests/design/eachobject')
-rw-r--r-- | tests/design/eachobject/Simple.java | 83 | ||||
-rw-r--r-- | tests/design/eachobject/Tricky1.java | 95 | ||||
-rw-r--r-- | tests/design/eachobject/Tricky2.java | 77 | ||||
-rw-r--r-- | tests/design/eachobject/Tricky3.java | 32 |
4 files changed, 287 insertions, 0 deletions
diff --git a/tests/design/eachobject/Simple.java b/tests/design/eachobject/Simple.java new file mode 100644 index 000000000..8ffe3e934 --- /dev/null +++ b/tests/design/eachobject/Simple.java @@ -0,0 +1,83 @@ +import org.aspectj.testing.Tester; +import org.aspectj.lang.NoAspectBoundException; + +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(), "SubA2.before, A.before, A.before, C.m, ", "SubC2"); + + + Tester.check(SubA1.aspectOf(subc2) instanceof SubA1, "SubA1.aspectOf(subc2) instanceof SubA1"); + + // retrieving the SubA aspect using its own aspectOf method + Tester.check(SubA2.aspectOf(subc2) instanceof SubA2, "Sub2A.aspectOf(subc2) instanceof SubA2"); + + try { + // trying to retrieve a SubA aspect where only an A aspect is present + SubA2 subA = SubA2.aspectOf(c); + Tester.checkFailed("SubA2.aspectOf(c) should throw an exception"); + } catch (NoAspectBoundException 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 pertarget(targets()) { + abstract pointcut targets(); + StringBuffer history = null; + after (C c) returning (): target(c) && initialization(new(..)) { + //System.out.println(thisJoinPoint); + history = c.history; + } + + before (): call(void m()) { + history.append("A.before, "); + } +} + +aspect SubA1 extends A { + pointcut targets(): target(C); +} + + +aspect SubA2 extends A { + pointcut targets(): target(SubC2); + before (): call(void m()) { + history.append("SubA2.before, "); + } +} diff --git a/tests/design/eachobject/Tricky1.java b/tests/design/eachobject/Tricky1.java new file mode 100644 index 000000000..7359ceaa3 --- /dev/null +++ b/tests/design/eachobject/Tricky1.java @@ -0,0 +1,95 @@ +import org.aspectj.testing.Tester; + +/** + * test time of creation and existence of aspect of eachobject(P) + * where P is not instanceof + */ +public class Tricky1 { + public static void main(String[] args) { + // the ReceptionAspect isn't created until just before the m message is received + C c = new C(); + c.foo(); + c.m(); + Tester.checkEqual(c.history.toString(), "C.foo, SubReceptionAspect1.before, C.m, "); + Tester.check(!SubReceptionAspect2.hasAspect(c), "!Sub2.hasAspect"); + + // if m1 is called first, then a single SubReceptionAspect is created instead + // no matter how many times m1 is called + c = new C(); + c.m1(); + c.m1(); + Tester.checkEqual(c.history.toString(), + "SubReceptionAspect2.before, C.m1, SubReceptionAspect2.before, C.m1, "); + Tester.check(!SubReceptionAspect1.hasAspect(c), "!Sub1.hasAspect"); + + + // calling both m and m1 should prduce one of each aspect + c = new C(); + c.m(); + c.m1(); + Tester.check(SubReceptionAspect1.hasAspect(c), "Sub1.hasAspect"); + Tester.check(SubReceptionAspect2.hasAspect(c), "Sub2.hasAspect"); + + + // be sure nothing unexpected happens on subtypes + c = new SubC(); + c.foo(); + c.m(); + Tester.checkEqual(c.history.toString(), "C.foo, SubReceptionAspect1.before, SubC.m, "); + + } +} + +class C { + // records what methods are called and advice are run + public StringBuffer history = new StringBuffer(); + + public void m() { + history.append("C.m, "); + } + public void m1() { + history.append("C.m1, "); + } + + public void foo() { + history.append("C.foo, "); + } +} + +class SubC extends C { + public void m() { + history.append("SubC.m, "); + } +} + + +abstract aspect ReceptionAspect perthis(targets()) { + abstract pointcut targets(); + + protected void noteBefore(C c) { + c.history.append("ReceptionAspect.before, "); + } + + before (C c): target(c) && execution(* *(..)) { + noteBefore(c); + } +} + + +aspect SubReceptionAspect1 extends ReceptionAspect { + pointcut targets(): execution(void m()); + + protected void noteBefore(C c) { + c.history.append("SubReceptionAspect1.before, "); + } +} + + + +aspect SubReceptionAspect2 extends ReceptionAspect { + pointcut targets(): execution(void m1()); + + protected void noteBefore(C c) { + c.history.append("SubReceptionAspect2.before, "); + } +} diff --git a/tests/design/eachobject/Tricky2.java b/tests/design/eachobject/Tricky2.java new file mode 100644 index 000000000..1f7ca9a36 --- /dev/null +++ b/tests/design/eachobject/Tricky2.java @@ -0,0 +1,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()); +} diff --git a/tests/design/eachobject/Tricky3.java b/tests/design/eachobject/Tricky3.java new file mode 100644 index 000000000..9f03a0369 --- /dev/null +++ b/tests/design/eachobject/Tricky3.java @@ -0,0 +1,32 @@ +import org.aspectj.testing.Tester; + +public class Tricky3 { + public static void main(String[] args) { + C c = new SubC(); + } +} + +class C { +} + +class SubC extends C { + void m() { } +} + +aspect A1 pertarget(target(SubC)) { + after() returning (SubC sub): call(new(..)) { + System.out.println("new " + sub); + } +} + +aspect A2 pertarget(call(void SubC.*())) { + after() returning (SubC sub): call(new(..)) { + System.out.println("new " + sub); + } +} + +aspect A3 pertarget(call(void m())) { + after() returning (SubC sub): call(new(..)) { + System.out.println("new " + sub); + } +} |