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 | |
parent | fafae443719b26159ab2d7dac1c9b46b5e00b671 (diff) | |
download | aspectj-144143c2970a1e874d74cdbd0f8c622d4282a3c3.tar.gz aspectj-144143c2970a1e874d74cdbd0f8c622d4282a3c3.zip |
initial version
Diffstat (limited to 'tests/design')
27 files changed, 1670 insertions, 0 deletions
diff --git a/tests/design/around/FakeStackChecker.java b/tests/design/around/FakeStackChecker.java new file mode 100644 index 000000000..f66cab919 --- /dev/null +++ b/tests/design/around/FakeStackChecker.java @@ -0,0 +1,6 @@ +import org.aspectj.testing.Tester; + +public class FakeStackChecker { + public static void setBaseDepth() { } + public static void checkDepth(int expectedDepth, String message) { } +} diff --git a/tests/design/around/ReturnCastProceed.java b/tests/design/around/ReturnCastProceed.java new file mode 100644 index 000000000..4141a8e98 --- /dev/null +++ b/tests/design/around/ReturnCastProceed.java @@ -0,0 +1,83 @@ +import org.aspectj.testing.Tester; + + +public class ReturnCastProceed extends Helper { + + public static void main(String[] args) { + StackChecker.setBaseDepth(); + + Tester.checkEqual(mInt(), 3, "mInt is 3"); + Tester.checkAndClearEvents( new String[] { "advice" } ); + + mVoid(); + Tester.checkAndClearEvents(new String[] {"advice", "void body" }); + + Tester.checkEqual(mString(), "I'm a string", "mString is right"); + Tester.checkAndClearEvents(new String[] { "advice" }); + + Tester.checkEqual(mInteger().intValue(), 5555, "mInteger is boxed 5555"); + Tester.checkAndClearEvents(new String[] { "advice" }); + + Tester.check(mRunnable() instanceof Runnable, "mRunnable returns a Runnable"); + Tester.checkAndClearEvents(new String[] { "advice" }); + + Tester.check(mObject() == f, "mObject returns f"); + Tester.checkAndClearEvents(new String[] { "advice" }); + + } +} + +class Helper { + static Float f = new Float(37.8); + + static int mInt() { + //StackChecker.checkDepth(2, "mInt"); + return 3; + } + + static void mVoid() { + StackChecker.checkDepth(2, "mVoid"); + Tester.event("void body"); + } + + static String mString() { + StackChecker.checkDepth(2, "mString"); + return "I'm a string"; + } + static Integer mInteger() { + StackChecker.checkDepth(2, "mInteger"); + return new Integer(5555); + } + + static Runnable mRunnable() { + //StackChecker.checkDepth(2, "mRunnable"); + return new Runnable() { + public void run() { + Tester.event("i'm running"); // not used yet. + } + }; + } + + static Object mObject() { + StackChecker.checkDepth(2, "mObject"); + return f; + } +} + +aspect A { + Object around(): execution(static * Helper.*(..)) { + Tester.event("advice"); + return (Object) proceed(); + } + +// Object around(): execution(static * ReturnCastProceed.*(..)) { +// Object ret = proceed(); +// System.err.println("doing stuff"); +// return ret; +// } + +// Object around(): execution(static * ReturnCastProceed.*(..)) { +// return proceed(); +// } + +} diff --git a/tests/design/around/StackChecker.java b/tests/design/around/StackChecker.java new file mode 100644 index 000000000..e55c64055 --- /dev/null +++ b/tests/design/around/StackChecker.java @@ -0,0 +1,16 @@ +import org.aspectj.testing.Tester; + +public class StackChecker { + static int baseStackDepth = 0; + + public static void setBaseDepth() { + int depth = new Throwable().getStackTrace().length - 2; // XXX 1.4 only + baseStackDepth = depth; + } + + + public static void checkDepth(int expectedDepth, String message) { + int depth = new Throwable().getStackTrace().length - 1 - baseStackDepth; + Tester.checkEqual(depth, expectedDepth, message); + } +} diff --git a/tests/design/calls/Simple.java b/tests/design/calls/Simple.java new file mode 100644 index 000000000..f1277bde0 --- /dev/null +++ b/tests/design/calls/Simple.java @@ -0,0 +1,173 @@ +import org.aspectj.testing.Tester; + +import java.lang.reflect.*; + +public class Simple { + public static void main(String[] args) { + new Simple().fromInstanceMethod("a1", 1); + //Tester.printEvents(); + Tester.checkAndClearEvents(new String[] { + "this(simple)", + "call(C.*)", + "args(..,a1)", + "C.toStaticMethod(a1)", + + "this(simple)", + "call(SubC.*)", + "args(..,a1)", + "SubC.toStaticMethod(a1)", + + "this(simple)&&target(c)", + "target(c)", + "this(simple)", + "call(C.*)", + "args(..,a1)", + "C.toInstanceMethod(a1)", + + "this(simple)&&target(subc)", + "target(subc)", + "this(simple)", + "call(C.*)", + "call(C.*)&&target(SubC)", + "args(..,a1)", + "SubC.toInstanceMethod(a1)", + + "this(simple)&&target(subc)", + "target(subc)", + "this(simple)", + "call(C.*)", + "call(SubC.*)", + "call(SubC.*)&&target(C)", + "call(C.*)&&target(SubC)", + "args(..,a1)", + "SubC.toInstanceMethod(a1)", + + //??? these events are not expected because advice on call join points + // isn't run when the calls are made reflectively -- Change from 1.0 to 1.1 + //"call(C.*)", + //"call(C.*)&&target(SubC)", + //"args(..,a1)", + "SubC.toInstanceMethod(a1)", + }); + Simple.fromStaticMethod("a2"); + //Tester.printEvents(); + Tester.checkAndClearEvents(new String[] { + "call(C.*)", + "args(..,a2)", + "C.toStaticMethod(a2)", + + "call(SubC.*)", + "args(..,a2)", + "SubC.toStaticMethod(a2)", + + "target(c)", + "call(C.*)", + "args(..,a2)", + "C.toInstanceMethod(a2)", + + "target(subc)", + "call(C.*)", + "call(C.*)&&target(SubC)", + "args(..,a2)", + "SubC.toInstanceMethod(a2)", + + "target(subc)", + "call(C.*)", + "call(C.*)&&target(SubC)", + "SubC.toInstanceMethod(2)", + }); + } + + public static void fromStaticMethod(String s) { + C.toStaticMethod(s); + SubC.toStaticMethod(s); + C c = new C(); + c.toInstanceMethod(0, s); + c = new SubC(); + c.toInstanceMethod(0, s); + c.toInstanceMethod(0, new Integer(2)); + } + + public void fromInstanceMethod(String s, int v) { + C.toStaticMethod(s); + SubC.toStaticMethod(s); + C c = new C(); + c.toInstanceMethod(v, s); + c = new SubC(); + c.toInstanceMethod(v, s); + SubC subc = new SubC(); + subc.toInstanceMethod(v, s); + doReflectiveCall(subc, v, s); + } + + void doReflectiveCall(C c, int v, String s) { + try { + Method m = + C.class.getMethod("toInstanceMethod", + new Class[] {Integer.TYPE, Object.class}); + m.invoke(c, new Object[] {new Integer(v), s}); + } catch (Exception e) { + Tester.event("unexpected exception: " + e); + } + } + + public String toString() { return "simple"; } +} + + +class C { + public static void toStaticMethod(Object s) { + Tester.event("C.toStaticMethod("+s+")"); + } + + public void toInstanceMethod(int value, Object str) { + Tester.event("C.toInstanceMethod("+str+")"); + } + public String toString() { return "c"; } +} + +class SubC extends C { + public static void toStaticMethod(Object s) { + Tester.event("SubC.toStaticMethod("+s+")"); + } + + public void toInstanceMethod(int x, Object str) { + Tester.event("SubC.toInstanceMethod("+str+")"); + } + public String toString() { return "subc"; } +} + +aspect A { + before(C c, Simple s): this(s) && target(c) { + Tester.event("this("+s+")&&target("+c+")"); + } + + before(C c): target(c) && within(Simple) { + Tester.event("target("+c+")"); + } + + before(Simple s): this(s) && call(* C+.*(..)) { + Tester.event("this("+s+")"); + } + + before(): call(* C.*(..)) { + Tester.event("call(C.*)"); + } + + before(): call(* SubC.*(..)) { + Tester.event("call(SubC.*)"); + } + + before(): call(* SubC.*(..)) && target(C) { + Tester.event("call(SubC.*)&&target(C)"); + } + + before(): call(* C.*(..)) && target(SubC) { + Tester.event("call(C.*)&&target(SubC)"); + } + + before(String s): args(.., s) && call(* C+.*(..)) { + //System.out.println(thisJoinPoint); + Tester.event("args(..,"+s+")"); + } +} diff --git a/tests/design/calls/Tricky1.java b/tests/design/calls/Tricky1.java new file mode 100644 index 000000000..99f14f395 --- /dev/null +++ b/tests/design/calls/Tricky1.java @@ -0,0 +1,33 @@ +public class Tricky1 { + public static void main(String[] args) { + Product1 p1, p2, p3; + p1 = Product1.create(); + p2 = Product1.create(); + p3 = Product1.create(); + System.out.println("p1: " + p1 + ", p2: " + p2 + ", p3: " + p3); + } +} + + +class Product1 { + public static Product1 create() { + return new Product1(); + } +} + +aspect MakeProduct1Singleton { + private static Product1 singletonProduct1 = null; + + // all calls to the Product1.create() method + pointcut creationCut(): calls(Product1, new(..)); //Product1 create()); + + // all calls to the above that don't happen in this aspect + pointcut externalCreationCut(): !within(MakeProduct1Singleton) && creationCut(); + + static around () returns Product1: externalCreationCut() { + if (singletonProduct1 == null) { + singletonProduct1 = new Product1(); //Product1.create(); + } + return singletonProduct1; + } +} 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); + } +} diff --git a/tests/design/intro/Interfaces.java b/tests/design/intro/Interfaces.java new file mode 100644 index 000000000..67ca0f684 --- /dev/null +++ b/tests/design/intro/Interfaces.java @@ -0,0 +1,98 @@ +import org.aspectj.testing.Tester; + +import java.util.*; + +import org.aspectj.lang.reflect.*; + +public class Interfaces { + public static void main(String[] args) { + String v = I.staticField; + Tester.checkEqual(notes, "I.staticField", "static init of I"); + Tester.checkEqual(v, "I.staticField"); + clearNotes(); + + I i = (I)new C(); + Tester.checkEqual(notes, + "initialize-I instanceField-A* I.instanceField privateField-A* I.privateField-from-A privateField-A* I.privateField-from-B", + "inst init of I"); + Tester.checkEqual(i.instanceField, "I.instanceField"); + clearNotes(); + + v = SubI.staticField; + Tester.checkEqual(notes, "SubI.staticField", "static init of SubI"); + Tester.checkEqual(v, "SubI.staticField"); + clearNotes(); + + SubI si = (SubI)new SubC(); + Tester.checkEqual(notes, + "initialize-I instanceField-A* I.instanceField privateField-A* I.privateField-from-A privateField-A* I.privateField-from-B SubI.instanceField", + "inst init of SubI"); + Tester.checkEqual(si.instanceField, "SubI.instanceField"); + clearNotes(); + + + i.instanceField += "-XXX"; + Tester.checkEqual(notes, "I.instanceField-!I||A*", "I.instanceField set"); + Tester.checkEqual(i.instanceField, "I.instanceField-XXX"); + } + + private static List notes = new LinkedList(); + + public static void clearNotes() { + notes = new LinkedList(); + //System.out.println("***********************************************"); + } + + public static String note(String note) { + notes.add(note); + //System.out.println(note); + return note; + } +} + +class C implements I { + String instanceField = "C.instanceField"; +} + +class SubC extends C implements SubI { + String instanceField = "SubC.instanceField"; +} + +interface I { + // must follow standard Java rules + String staticField = Interfaces.note("I.staticField"); +} + +interface SubI extends I { + String staticField = Interfaces.note("SubI.staticField"); +} + + + +aspect A1 { + public String SubI.instanceField = Interfaces.note("SubI.instanceField"); + + public String I.instanceField = Interfaces.note("I.instanceField"); + private String I.privateField = Interfaces.note("I.privateField-from-A"); +} + +aspect A2 { + private String I.privateField = Interfaces.note("I.privateField-from-B"); +} + +aspect A3 { + before(I i): !within(I||A*) && set(String I.*) && target(i) { + Interfaces.note(thisJoinPoint.getSignature().toShortString()+"-!I||A*"); // +"::" + thisJoinPoint.getSourceLocation().getWithinType()); + } + + before(I i): within(I) && set(String I.*) && target(i) { + Interfaces.note(thisJoinPoint.getSignature().getName()+"-I"); //toShortString()); + } + before(I i): within(A*) && set(String I.*) && target(i) { + Interfaces.note(thisJoinPoint.getSignature().getName()+"-A*"); //toShortString()); + } + + before(I i): initialization(I.new(..)) && target(i) { + Interfaces.note("initialize-I"); + } +} diff --git a/tests/design/intro/MultiInheritCF.java b/tests/design/intro/MultiInheritCF.java new file mode 100644 index 000000000..cd9be4b91 --- /dev/null +++ b/tests/design/intro/MultiInheritCF.java @@ -0,0 +1,47 @@ +import org.aspectj.testing.Tester; + +public class MultiInheritCF { + public static void main(String[] args) { + C c = new C(); + Tester.checkEqual(c.fromRoot(), "Root"); + Tester.checkEqual(c.toString(), "I1"); + Tester.checkEqual(c.fromI2(), "I2"); + Tester.checkEqual(c.fromIRoot0(), "IRoot"); + Tester.checkEqual(c.fromIRoot1(), "I1"); + Tester.checkEqual(c.fromIRoot2(), "I2"); + } +} + +abstract class Root { + public String fromRoot() { return "Root"; } + public abstract String fromI2(); + public String toString() { return "Root"; } +} + +class C extends Root implements I1, I2 { +} + +interface IRoot { +} + +interface I1 extends IRoot { + public String fromRoot(); +} + +interface I2 extends IRoot { +} + + +aspect A { + public String IRoot.fromIRoot0() { return "IRoot"; } + public String IRoot.fromIRoot1() { return "IRoot"; } + public String IRoot.fromIRoot2() { return "IRoot"; } + + + public String I1.toString() { return "I1"; } //ERR: conflicts with Root + public abstract int I2.toString(); //ERR: conflicts with Root and I1 + String I2.fromI2() { return "I2"; } //ERR: weaker than Root + + public String I1.fromIRoot1() { return "I1"; } + public String I2.fromIRoot1() { return "I1"; } //ERR: conflicts with I1 +} diff --git a/tests/design/intro/MultiInheritCP.java b/tests/design/intro/MultiInheritCP.java new file mode 100644 index 000000000..704041086 --- /dev/null +++ b/tests/design/intro/MultiInheritCP.java @@ -0,0 +1,46 @@ +import org.aspectj.testing.Tester; + +public class MultiInheritCP { + public static void main(String[] args) { + C c = new C(); + Tester.checkEqual(c.fromRoot(), "Root"); + Tester.checkEqual(c.toString(), "I1"); + Tester.checkEqual(c.fromI2(), "I2"); + Tester.checkEqual(c.fromIRoot0(), "IRoot"); + Tester.checkEqual(c.fromIRoot1(), "I1"); + Tester.checkEqual(c.fromIRoot2(), "I2"); + } +} + +abstract class Root { + public String fromRoot() { return "Root"; } + public abstract String fromI2(); +} + +class C extends Root implements I1, I2 { +} + +interface IRoot { +} + +interface I1 extends IRoot { + public String fromRoot(); +} + +interface I2 extends IRoot { +} + + +aspect A { + public String IRoot.fromIRoot0() { return "IRoot"; } + public String IRoot.fromIRoot1() { return "IRoot"; } + public String IRoot.fromIRoot2() { return "IRoot"; } + + + public String I1.toString() { return "I1"; } + public abstract String I2.toString(); + public String I2.fromI2() { return "I2"; } + + public String I1.fromIRoot1() { return "I1"; } + public String I2.fromIRoot2() { return "I2"; } +} diff --git a/tests/design/intro/Overriding.java b/tests/design/intro/Overriding.java new file mode 100644 index 000000000..7e8412eac --- /dev/null +++ b/tests/design/intro/Overriding.java @@ -0,0 +1,82 @@ +import org.aspectj.testing.Tester; + +public class Overriding { + public static void main(String[] args) { + SuperC sc = new C(); + + Tester.checkEqual(sc.m(), "A2"); + Tester.checkEqual(A3.getMi(sc), "A3.I2"); + Tester.checkEqual(A4.getMi(sc), "A4.I2"); + Tester.checkEqual(A3.getM2(sc), "A3.Inner"); + Tester.checkEqual(A4.getM2(sc), "A4.Inner"); + } +} + +abstract class SuperC { } +class C extends SuperC {} +class SubC extends C {} + +aspect A1 { + abstract String SuperC.m(); +} + + +aspect A2 { + String C.m() { + return "A2"; + } +} + +class A3 { + static aspect I1 { + private abstract String SuperC.mi(); + } + + static aspect I2 { + private String C.mi() { + return "A3.I2"; + } + } + + public static String getMi(SuperC sc) { + return sc.mi(); + } + + + static aspect Inner { + private abstract String SuperC.m2(); + private String C.m2() { + return "A3.Inner"; + } + } + public static String getM2(SuperC sc) { + return sc.m2(); + } +} + +class A4 { + static aspect I1 { + private abstract String SuperC.mi(); + } + + static aspect I2 { + private String C.mi() { + return "A4.I2"; + } + } + + public static String getMi(SuperC sc) { + return sc.mi(); + } + + static aspect Inner { + private abstract String SuperC.m2(); + private String C.m2() { + return "A4.Inner"; + } + } + public static String getM2(SuperC sc) { + return sc.m2(); + } + +} diff --git a/tests/design/intro/Simple.java b/tests/design/intro/Simple.java new file mode 100644 index 000000000..cd995d993 --- /dev/null +++ b/tests/design/intro/Simple.java @@ -0,0 +1,80 @@ +import org.aspectj.testing.Tester; + +public class Simple { + public static void main(String[] args) { + C c = new C(); + I i = (I)new C("hi"); + Tester.checkEqual(c.foo(), "foo:bar"); + Tester.checkEqual(i.mumble(), "mumble:foo:bar"); + + Tester.checkEqual(A.Cm(), "from A"); + Tester.checkEqual(B.Cm(), "from B"); + + c.idata = "c-mumble"; + Tester.checkEqual(c.idata, "c-mumble"); + Tester.checkEqual(i.idata, "mumble"); + + Tester.check("new A.C"); + Tester.check("new B.C"); + } +} + +class C { + public C() { super(); } + public String bar() {return "bar"; } +} + +interface I { + String foo(); + String bar(); +} + + +aspect A { + private String C.data = "foo"; + private String C.data1 = this.data; + + public String I.idata = "mumble"; + + public String C.foo() { + String s = this.data; + Tester.checkEqual(s, data1); + return data + ":" + bar(); + } + + declare parents: C implements I; + + public String I.mumble() { + return idata + ":" + foo(); + } + + private String C.m() { + return "from A"; + } + + public static String Cm() { + return new C(2).m(); + } + + public C.new(String s) { } + + private C.new(int i) { + Tester.note("new A.C"); + } +} + +aspect B { + private String C.data = "B"; + + private String C.m() { + return "from " + data; + } + + public static String Cm() { + return new C(2).m(); + } + + private C.new(int i) { + Tester.note("new B.C"); + } +} diff --git a/tests/design/intro/Statics.java b/tests/design/intro/Statics.java new file mode 100644 index 000000000..183e96be9 --- /dev/null +++ b/tests/design/intro/Statics.java @@ -0,0 +1,56 @@ +import org.aspectj.testing.Tester; + +public class Statics { + public static void main(String[] args) { + new C(); + Tester.checkEqual(C.getCount(), 1, "C.getCount()"); + Tester.checkEqual(SubC.getCount(), 0, "SubC.getCount()"); + new SubC(); + Tester.checkEqual(C.getCount(), 1, "C.getCount()"); + Tester.checkEqual(SubC.getCount(), 1, "SubC.getCount()"); + new SubC(); + Tester.checkEqual(C.getCount(), 1, "C.getCount()"); + Tester.checkEqual(SubC.getCount(), 2, "SubC.getCount()"); + + I.count += 1; + Tester.checkEqual(I.getCount(), 1, "I.getCount()"); + + Tester.checkEqual(I1.count, 10, "I1.count"); + Tester.checkEqual(SubI.count, 20, "SubI.count"); + Tester.checkEqual(SubI.getCount(), 1, "SubI.getCount()"); + } +} + + +class C { } + +class SubC extends C {} + +interface I {} + +interface I1 { + int N = -1; + Object o = new Integer(2); +} + +interface SubI extends I, I1 { } + +aspect Counter { + private static int C.instanceCount = 0; + private void C.incCount() { instanceCount++; } + static int C.getCount() { return instanceCount; } + private static int SubC.instanceCount = 0; + private void SubC.incCount() { instanceCount++; } + static int SubC.getCount() { return instanceCount; } + + static int I.count = 0; + static int I.getCount() { return count; } + + static int I1.count = 10; + + static int SubI.count = 20; + + after() returning (C c) : call(C+.new(..)) { + c.incCount(); + } +} diff --git a/tests/design/intro/Within.java b/tests/design/intro/Within.java new file mode 100644 index 000000000..49021c1f0 --- /dev/null +++ b/tests/design/intro/Within.java @@ -0,0 +1,64 @@ +import org.aspectj.testing.Tester; + +public class Within { + public static void main(String[] args) { + C c = new C(); + c.mi(); + + Tester.check("I.mi within A1"); + Tester.check("I.mi instanceof C"); + Tester.check("I.mi instanceof I"); + + c.mc(); + } +} + + +class C { + void mc() {} +} + +interface I {} + +aspect A1 { + void I.mi() {} +} + +aspect A2 { + declare parents: C implements I; +} + + + +aspect Test { + before (): execution(* I.*(..)) && within(C) { + Tester.checkFailed(thisJoinPoint + " I.* within C"); + } + before (): execution(* I.*(..)) && within(I) { + Tester.checkFailed(thisJoinPoint + " I.* within I"); + } + before (): execution(* I.*(..)) && within(A1) { + Tester.checkEqual(thisJoinPoint.getSignature().getName(), "mi", + thisJoinPoint + " I.* within A1"); + Tester.note("I.mi within A1"); + } + before (): execution(* I.*(..)) && within(A2) { + } + + before (): execution(* I.*(..)) && this(C) { + Tester.checkEqual(thisJoinPoint.getSignature().getName(), "mi", + thisJoinPoint + " I.* instanceof C"); + Tester.note("I.mi instanceof C"); + } + before (): execution(* I.*(..)) && this(I) { + Tester.checkEqual(thisJoinPoint.getSignature().getName(), "mi", + thisJoinPoint + " I.* instanceof I"); + Tester.note("I.mi instanceof I"); + } + before (): execution(* I.*(..)) && this(A1) { + Tester.checkFailed(thisJoinPoint + " I.* instanceof A1"); + } + before (): execution(* I.*(..)) && this(A2) { + Tester.checkFailed(thisJoinPoint + " I.* instanceof A2"); + } +} diff --git a/tests/design/intro/p1/C1.java b/tests/design/intro/p1/C1.java new file mode 100644 index 000000000..f7d7bd081 --- /dev/null +++ b/tests/design/intro/p1/C1.java @@ -0,0 +1,11 @@ +package p1; + +import java.util.*; + +public class C1 { + Map map = new HashMap(); +} + +class Helper { + List l = new LinkedList(); +} diff --git a/tests/design/intro/p1/ScopeIssues.java b/tests/design/intro/p1/ScopeIssues.java new file mode 100644 index 000000000..2aa2add92 --- /dev/null +++ b/tests/design/intro/p1/ScopeIssues.java @@ -0,0 +1,16 @@ +package p1; + +import org.aspectj.testing.Tester; + +import java.math.*; + +public aspect ScopeIssues { + public static void main(String[] args) { + Tester.checkEqual(C1.bi, BigInteger.ONE); + + Tester.checkEqual(Helper.bi, BigInteger.ONE); + } + + private static BigInteger C1.bi = BigInteger.ONE; + private static BigInteger Helper.bi = BigInteger.ONE; +} diff --git a/tests/design/pcds/If.java b/tests/design/pcds/If.java new file mode 100644 index 000000000..8d843e36d --- /dev/null +++ b/tests/design/pcds/If.java @@ -0,0 +1,52 @@ +public class If { + public static void main(String[] args) { + C c = new C(); + c.m(true); + c.m(false); + c.m(true); + c.m(false); + } +} + +class C { + static boolean test() { return value; } + + static boolean value = false; + + void m(boolean b) { + value = b; + System.out.println("C.m(" + b + ")"); + } +} + +aspect A { + static boolean testA() { return true; } + boolean itestA() { return true; } + + boolean t = true; + + before(): call(void m(boolean)) && if(C.test()) { + System.out.println(thisJoinPoint); + } + before(boolean x): call(void m(boolean)) && args(x) && if(x) && if(testA()) && if(this.t) && + if(thisJoinPoint.getSignature().getName().equals("m")) && if(itestA()) { + System.out.println(x + ": " + thisJoinPoint); + } + + pointcut cut(boolean a): call(void m(boolean)) && args(a) && if(a) && if(this.itestA()) && if(t); + + before(boolean x): cut(x) { + System.out.println(x); + } + + before(Object t): target(t) && call(void m(boolean)) && if(t instanceof C) { + System.out.println(t); + } + before(Object t): target(t) && call(void m(boolean)) && if(t instanceof String) { + System.out.println(t); + } + + before(C c): target(c) && call(void m(boolean)) && if(c.value) { + System.out.println(thisJoinPoint); + } +} diff --git a/tests/design/pcds/Simple.java b/tests/design/pcds/Simple.java new file mode 100644 index 000000000..e6051799a --- /dev/null +++ b/tests/design/pcds/Simple.java @@ -0,0 +1,80 @@ +package pcds; + +import org.aspectj.testing.Tester; + +public class Simple { + public static void main(String[] args) { + C c = new C(); + c.m("hi"); + + C subc = new SubC(); + subc.m("hi"); + subc.m(new Integer(1)); + + SubC subc1 = new SubC(); + subc1.m("bye"); + + subc.hashCode(); + } +} + +class C { + void m(Object o) { + System.out.println("C.m(" + o + ")"); + } + + static pointcut meths(C c): call(void m(Object)) && target(c); +} + +class SubC extends C { + SubC(int x) { + System.out.println("x: " + x); + } + + SubC(String s) { + this(2*2); + System.out.println("s: " + s); + int x = 10; + } + + SubC() { + this("hi"); + System.out.println("no args"); + } +} + + +aspect A { + before(Object o): C.meths(o) { + System.out.println("static named pointcut"); + } + + before(): call(void m(..)) && target(SubC) && args(String) { + System.out.println("dmatches: " + thisJoinPoint); + } + before(): call(void SubC.m(String)) { + System.out.println("!smatches: " + thisJoinPoint); + } + before(Object o, String s): call(void C.m(Object)) && target(SubC) && args(s) && args(o) { + System.out.println("smatches: " + thisJoinPoint +", " + s +", " + o); + } + + before(): initialization(SubC.new(..)) { + System.out.println(thisJoinPoint + "new SubC"); + } + void around(): initialization(SubC.new(..)) { + proceed(); + } + + before(): execution(SubC.new(..)) { + System.out.println(thisJoinPoint + "new SubC"); + } + + before(): call(int Object.hashCode()) { + System.out.println("hashCode()"); + } + before(): call(int Object.hashCode()) && target(C) { + System.out.println("hashCode() on C"); + } +} + diff --git a/tests/design/privileged/Simple.java b/tests/design/privileged/Simple.java new file mode 100644 index 000000000..5311d32ed --- /dev/null +++ b/tests/design/privileged/Simple.java @@ -0,0 +1,31 @@ +import org.aspectj.testing.Tester; + +public class Simple { + public static void main(String[] args) { + new C().foo(); + } +} + +class C { + private int privateField = 1; + + private int privateMethod() { + return 42; + } + + public void foo() { + System.out.println("f: " + privateField); + } +} + +privileged aspect A { + static before (C c): instanceof(c) && receptions(void foo()) { + System.out.println("from A: " + c.privateField); + c.privateField += 1; + System.out.println("from A: " + c.privateField); + System.out.println("from A: " + ++c.privateField); + System.out.println("from A: " + c.privateField++); + System.out.println("from A: " + c.privateField); + System.out.println("from A: " + c.privateMethod()); + } +} diff --git a/tests/design/reflect/Coverage.java b/tests/design/reflect/Coverage.java new file mode 100644 index 000000000..7de997909 --- /dev/null +++ b/tests/design/reflect/Coverage.java @@ -0,0 +1,270 @@ +import org.aspectj.testing.Tester; +import org.aspectj.lang.*; +import org.aspectj.lang.reflect.*; +import java.util.*; + +public class Coverage extends Args { + public static void main(String[] args) { + Args.args = args; + Args.c = new C(); + Args.c.x += 1; + Args.c.mumble("foo"); + } +} + +class C { + public int x; + void mumble(String arg1) { + Args.arg1 = arg1; + try { + throw new Exception("test"); + } catch (Exception e) { + } + } +} + +class Args { + public static String[] args; + public static String arg1; + public static C c; +} + +class SubC { +} + +aspect JoinPoints extends Helper { + + before(): set(* C.*) { + JoinPoint jp = thisJoinPoint; + p("sets", jp); + a(jp.getKind(),"field-set"); + Integer integer = null; + Object o = null; + try { + o = jp.getTarget().getClass().getField + (jp.getSignature().getName()).get(jp.getTarget()); + } catch (Exception e) { a(e); return; } + + try { + integer = (Integer)o; + } catch (ClassCastException cce) { + a(ni(o, Integer.class, "set-1")); + return; + } + a(integer.intValue() == 0, "set value i != 0"); + o = jp.getArgs()[0]; + try { + integer = (Integer) o; + } catch (ClassCastException cce) { + a(ni(o, Integer.class, "set-2")); + return; + } + a(integer.intValue() == 1, "set newvalue i != 1"); + Object ex = jp.getThis(); + JoinPoint.StaticPart sp = jp.getStaticPart(); + FieldSignature fs = null; + Signature sig = jp.getSignature(); + try { + fs = (FieldSignature)sig; + } catch (ClassCastException cce) { + a(ni(sig, FieldSignature.class, "set-1")); + + } + } + + before(): get(* C.*) { + JoinPoint jp = thisJoinPoint; + p("gets", jp); + a(jp.getKind(),"field-get"); + Integer integer = null; + Object o = null; + try { + o = jp.getTarget().getClass().getField + (jp.getSignature().getName()).get(jp.getTarget()); + } catch (Exception e) { a(e); } + try { + integer = (Integer) o; + } catch (ClassCastException cce) { + a(ni(o, Integer.class, "get")); + return; + } + a(integer.intValue() == 0, "get value i != 0"); + Object ex = jp.getThis(); + JoinPoint.StaticPart sp = jp.getStaticPart(); + FieldSignature fs = null; + Signature sig = jp.getSignature(); + try { + fs = (FieldSignature)sig; + } catch (ClassCastException cce) { + a(ni(sig, FieldSignature.class, "get")); + return; + } + } + + before(): execution(* C.*(..)) { + JoinPoint jp = thisJoinPoint; + p("executions", jp); + a(jp.getKind(), "method-execution"); + Signature sig = jp.getSignature(); + CodeSignature cs = null; + try { + cs = (CodeSignature)sig; + } catch (ClassCastException cce) { + a(ni(sig, CodeSignature.class, "execution")); + } + String name = cs.getName(); + got(name + ".execution"); + if (name.equals("mumble")) { + Object[] params = jp.getArgs(); + Object target = jp.getThis(); + String arg1 = null; + try { + arg1 = (String) params[0]; + } catch (ArrayIndexOutOfBoundsException ae) { + a("params.size < 1"); + return; + } catch (ClassCastException cce) { + a(ni(params[0], String.class)); + return; + } + a(target == Args.c, target + " != " + Args.c + ", should be c - 2"); + } + } + before(): call(C.new(..)) { + JoinPoint jp = thisJoinPoint; + p("calls", jp); + a(jp.getKind(),"constructor-call"); + Signature sig = jp.getSignature(); + ConstructorSignature cs = null; + try { + cs = (ConstructorSignature)sig; + } catch (ClassCastException cce) { + a(ni(cs, ConstructorSignature.class)); + } + Object[] params = jp.getArgs(); + Object target = jp.getThis(); + String name = cs.getName(); + a(name, "<init>"); + a(new Integer(params.length), new Integer(0)); + a(target == Args.c, target + " != " + Args.c + ", should be c - 3"); + } + before(): execution(C.new(..)) { + JoinPoint jp = thisJoinPoint; + p("executions", jp); + a(jp.getKind(),"constructor-execution"); + Signature sig = jp.getSignature(); + ConstructorSignature cs = null; + try { + cs = (ConstructorSignature)sig; + } catch (ClassCastException cce) { + a(ni(sig, ConstructorSignature.class)); + } + Object[] params = jp.getArgs(); + Object target = jp.getThis(); + String name = cs.getName(); + a(name, "<init>"); + a(new Integer(params.length), new Integer(0)); + } + before(): handler(*) && !within(JoinPoints) { + JoinPoint jp = thisJoinPoint; + p("handle", jp); + a(jp.getKind(),"exception-handler"); + Signature sig = jp.getSignature(); + CatchClauseSignature ccs = null; + try { + ccs = (CatchClauseSignature)sig; + } catch (ClassCastException cce) { + a(ni(sig, CatchClauseSignature.class)); + } + Throwable t = null; + try { + t = (Throwable)jp.getArgs()[0]; + } catch (ArrayIndexOutOfBoundsException _) { + a("handlers out of bounds"); + } catch (ClassCastException __) { + a(ni(jp.getArgs()[0], Throwable.class, "handlers")); + } + a(t.getMessage(), "test"); + Object ex = jp.getThis(); + a(ex, Args.c); + JoinPoint.StaticPart sp = jp.getStaticPart(); + } +} + +abstract aspect Helper { + + final static String[] strings = { + "mumble.execution", + "calls", + "executions", + "gets", + "sets", + "executions", + "handle", + }; + + static Map hash = new HashMap(); + static List names = new Vector(); + static { + for (int i = 0; i < strings.length; i++) { + need(strings[i]); + } + } + + after(): execution(* main(..)) { + checkAll(); + } + + static void checkAll() { + Iterator iter = names.iterator(); + while (iter.hasNext()) { + Object method = iter.next(); + Object call = hash.get(method); + a(call != null, method + " was not called"); + } + } + + static void got(Object method) { + hash.put(method, method); + } + + static void need(Object method) { + names.add(method); + } + + static void a(Object o1, Object o2) { + String msg = "" + o1 + " != " + o2; + a(o1, o2, msg); + } + static void a(Object o1, Object o2, String msg) { + if (o1 != null) a(o1.equals(o2), msg); + else if (o2 != null) a(o2.equals(o1), msg); + } + static void a(boolean b, Object o) { + Tester.check(b, o + ""); + } + static void a(Object o) { + a(false, o); + } + static String ni(Object o, Class c, String s) { + return ni("(" + s + ") " + o, c); + } + static String ni(Object o, Class c) { + return o + + (o == null ? " is null " : " is a " + o.getClass()) + + " is not a " + c; + } + static boolean verbose = false; + static void p(Object o) { + if (verbose) System.out.println(o); + } + static void p(Object o, JoinPoint jp) { + got(o); + } + static void po(Object o) { + boolean ov = verbose; + verbose = true; + p(o); + verbose = ov; + } +} diff --git a/tests/design/reflect/Point.java b/tests/design/reflect/Point.java new file mode 100644 index 000000000..b5d4fc913 --- /dev/null +++ b/tests/design/reflect/Point.java @@ -0,0 +1,10 @@ +package org.aspectj.examples; + +public class Point { + public int x, y; + + public Point(int _x, int _y) { this.x = _x; this.y = _y; } + + public void move(int dx, int dy) { x += dx; y += dy; } +} + diff --git a/tests/design/reflect/Simple.java b/tests/design/reflect/Simple.java new file mode 100644 index 000000000..59a5b7ffe --- /dev/null +++ b/tests/design/reflect/Simple.java @@ -0,0 +1,28 @@ +import org.aspectj.lang.reflect.*; + +public class Simple { + public static void main(String[] args) { + new Simple().foo("hi"); + } + + void foo(String s) { + System.out.println("foo(" + s + ")"); + } + + char ch = 'a'; + int i = 0; +} + +aspect A { + before(): execution(* *.*(..)) { + System.out.println(thisJoinPoint+ ", " + thisEnclosingJoinPointStaticPart); + } + before(): call(* *.*(..)) && !within(A) { + System.out.println("call: " + thisJoinPoint.getThis()+ ", " + thisEnclosingJoinPointStaticPart); + } + + before(): set(* Simple.*) { + //Object old = ((FieldAccessJoinPoint)thisJoinPoint).getValue(); + System.out.println(thisJoinPoint +", " + thisJoinPoint.getArgs()); + } +} diff --git a/tests/design/reflect/SimpleAround.java b/tests/design/reflect/SimpleAround.java new file mode 100644 index 000000000..842a6e720 --- /dev/null +++ b/tests/design/reflect/SimpleAround.java @@ -0,0 +1,30 @@ +import org.aspectj.lang.reflect.*; + +public class SimpleAround { + public static void main(String[] args) { + new SimpleAround().foo("hi"); + } + + void foo(String s) { + System.out.println("foo(" + s + ")"); + } +} + +aspect A { + static around(String x) returns void: executions(void *.foo(x)) { + System.out.println("calling foo with: " + x +", " + thisStaticJoinPoint); + proceed(x); + System.out.println("returning from: " + thisJoinPoint); //((CallJoinPoint)thisJoinPoint).getParameters()[0]); + } + + static before(): executions(void *.foo(String)) { + System.out.println("entering: " + thisJoinPoint); + } + + //static around() returns void: calls(void *.foo(String)) { + //System.out.println(thisStaticJoinPoint); + //proceed(); + //} + + +} diff --git a/tests/design/reflect/SimpleAround1.java b/tests/design/reflect/SimpleAround1.java new file mode 100644 index 000000000..92234ac04 --- /dev/null +++ b/tests/design/reflect/SimpleAround1.java @@ -0,0 +1,39 @@ +import org.aspectj.testing.*; +import org.aspectj.lang.*; +import org.aspectj.lang.reflect.*; +import java.util.*; + +public class SimpleAround1 { + public static void main(String[] args) { + new SimpleAround1().go(); + Tester.checkEqual(A.ran, "foo:goo:boo:", "advice didn't run"); + } + void go() { + foo("1"); + goo("2"); + boo("3"); + + } + void foo(String s) { new Integer(2).toString(); } + + void goo(String s) { } + + void boo(String s) { } +} + +aspect A { + + void around(String s): execution(void *.*oo(String)) && args(s){ + proceed(s); + JoinPoint jp = thisJoinPoint; + ran += jp.getSignature().getName()+":"; + } + + static String ran = ""; + + // When this advice is here no joinpoint is constructed in foo(String) + before(): execution(void *.foo(String)) { } + before(): execution(void *.goo(String)) {thisJoinPoint.getThis(); } + + before(): call(* Integer.*(..)) {thisJoinPoint.getThis(); } +} diff --git a/tests/design/reflect/Strings.java b/tests/design/reflect/Strings.java new file mode 100644 index 000000000..fa8ec04d2 --- /dev/null +++ b/tests/design/reflect/Strings.java @@ -0,0 +1,32 @@ +import org.aspectj.testing.Tester; +import org.aspectj.lang.*; +import org.aspectj.lang.reflect.*; +import java.util.*; +import org.aspectj.examples.Point; + +public class Strings { + public static void main(String[] args) { + Point p = new Point(1,1); + p.x += 10; + p.move(20, 20); + + try { + throw new UnsupportedOperationException("test"); + } catch (UnsupportedOperationException e) { + } + + } +} + +aspect JoinPoints { + static before(): within(Strings) || instanceof(Point) && !receptions(new(..)) { + System.out.println("tjp-short : " + thisJoinPoint.toShortString()); + System.out.println("tjp-default: " + thisJoinPoint); + System.out.println("tjp-long : " + thisJoinPoint.toLongString()); + System.out.println(); + System.out.println("sig-short : " + thisJoinPoint.getSignature().toShortString()); + System.out.println("sig-default: " + thisJoinPoint.getSignature()); + System.out.println("sig-long : " + thisJoinPoint.getSignature().toLongString()); + System.out.println("--------------------------------------------------------------------------"); + } +} |