From: aclement Date: Thu, 3 Feb 2005 11:04:59 +0000 (+0000) Subject: Annotation Binding: testcases for @this/@args and one for using named pointcuts with... X-Git-Tag: V1_5_0M2~189 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=0dde9d61b534fd84462d2b6a52b6d21d624d3077;p=aspectj.git Annotation Binding: testcases for @this/@args and one for using named pointcuts with annotations. --- diff --git a/tests/java5/annotations/binding/AtArgs1.aj b/tests/java5/annotations/binding/AtArgs1.aj new file mode 100644 index 000000000..c967f878f --- /dev/null +++ b/tests/java5/annotations/binding/AtArgs1.aj @@ -0,0 +1,41 @@ +import java.lang.annotation.*; + +@Retention(RetentionPolicy.RUNTIME) @interface Colored { String color(); } +@Retention(RetentionPolicy.RUNTIME) @interface Fruit { String value(); } + +@Colored(color="yellow") @Fruit("banana") class YB {} +@Colored(color="green") @Fruit("apple") class GA {} +@Colored(color="red") @Fruit("tomato") class RT {} + +public class AtArgs1 { + public static void main(String[]argv) { + m(new YB(),new GA(),new RT()); + + X.verify(); + } + + public static void m(Object a,Object b,Object c) { } + +} + +aspect X { + + static int count = 0; + + before(Colored c1,Colored c2,Colored c3): call(* m(..)) && !within(X) && @args(c1,c2,c3) { + System.err.println("Colors are "+c1.color()+","+c2.color()+","+c3.color()); + count++; + if (!c1.color().equals("yellow")) + throw new RuntimeException("Color1 should be yellow"); + if (!c2.color().equals("green")) + throw new RuntimeException("Color2 should be green"); + if (!c3.color().equals("red")) + throw new RuntimeException("Color3 should be red"); + + } + + public static void verify() { + if (count!=1) throw new Error("Should be 1 run: "+count); + } +} + diff --git a/tests/java5/annotations/binding/AtArgs2.aj b/tests/java5/annotations/binding/AtArgs2.aj new file mode 100644 index 000000000..ed46d6186 --- /dev/null +++ b/tests/java5/annotations/binding/AtArgs2.aj @@ -0,0 +1,28 @@ +import java.lang.annotation.*; + +@Retention(RetentionPolicy.RUNTIME) @interface Colored { String color(); } +@Retention(RetentionPolicy.RUNTIME) @interface Fruit { String value(); } + +@Colored(color="yellow") @Fruit("banana") class YB {} + +public class AtArgs2 { + public static void main(String[]argv) { + m(new YB()); + if (!X.run) throw new Error("Advice should have run"); + } + + public static void m(Object a) {} + +} + +aspect X { + public static boolean run = false; + before(Colored c): call(* m(..)) && !within(X) && @args(c) { + System.err.println("Color is "+c.color()); + run = true; + if (!c.color().equals("yellow")) + throw new RuntimeException("Color should be yellow: "+c.color()); + } + +} + diff --git a/tests/java5/annotations/binding/AtArgs3.aj b/tests/java5/annotations/binding/AtArgs3.aj new file mode 100644 index 000000000..ee2fbccc1 --- /dev/null +++ b/tests/java5/annotations/binding/AtArgs3.aj @@ -0,0 +1,28 @@ +import java.lang.annotation.*; + +@Retention(RetentionPolicy.RUNTIME) @interface Colored { String color(); } +@Retention(RetentionPolicy.RUNTIME) @interface Fruit { String value(); } + +@Colored(color="yellow") @Fruit("banana") class YB {} + +public class AtArgs3 { + public static void main(String[]argv) { + m(new YB()); + if (!X.b) + throw new Error("Advice should have run"); + } + + public static void m(Object a) {} + +} + +aspect X { + + static boolean b = false; + + before(): call(* m(..)) && !within(X) && @args(@Colored) { + b=true; + } + +} + diff --git a/tests/java5/annotations/binding/AtArgs4.aj b/tests/java5/annotations/binding/AtArgs4.aj new file mode 100644 index 000000000..9c129b5ce --- /dev/null +++ b/tests/java5/annotations/binding/AtArgs4.aj @@ -0,0 +1,40 @@ +import java.lang.annotation.*; + +@Retention(RetentionPolicy.RUNTIME) @interface Colored { String color(); } +@Retention(RetentionPolicy.RUNTIME) @interface Fruit { String value(); } + +@Colored(color="yellow") @Fruit("banana") class YB {} +@Colored(color="green") @Fruit("apple") class GA {} +@Colored(color="red") @Fruit("tomato") class RT {} + +public class AtArgs4 { + public static void main(String[]argv) { + m(new YB(),new GA(),new RT()); + X.verify(); + } + + public static void m(Object a,Object b,Object c) { } + +} + +aspect X { + static int count = 0; + + before(Colored c1,Fruit f,Colored c2): call(* m(..)) && !within(X) && @args(c1,f,c2) { + System.err.println("Two colors:"+c1.color()+","+c2.color()); + System.err.println("Fruit is:"+f.value()); + count++; + if (!c1.color().equals("yellow")) + throw new RuntimeException("Color1 should be yellow"); + if (!f.value().equals("apple")) + throw new RuntimeException("Fruit should be apple"); + if (!c2.color().equals("red")) + throw new RuntimeException("Color2 should be red"); + } + + public static void verify() { + if (count!=1) throw new Error("Should be 1 run: "+count); + } + +} + diff --git a/tests/java5/annotations/binding/AtArgs5.aj b/tests/java5/annotations/binding/AtArgs5.aj new file mode 100644 index 000000000..b2b51885a --- /dev/null +++ b/tests/java5/annotations/binding/AtArgs5.aj @@ -0,0 +1,39 @@ +import java.lang.annotation.*; + +@Retention(RetentionPolicy.RUNTIME) @interface Colored { String color(); } +@Retention(RetentionPolicy.RUNTIME) @interface Fruit { String value(); } + +@Colored(color="yellow") @Fruit("banana") class YB {} +@Colored(color="green") @Fruit("apple") class GA {} +@Colored(color="red") @Fruit("tomato") class RT {} + +public class AtArgs5 { + public static void main(String[]argv) { + m(new YB(),new GA(),new RT()); + } + + public static void m(Object a,Object b,Object c) { } + +} + +aspect X { + static int count = 0; + + before(Colored c1,Fruit f,Colored c2): execution(* m(..)) && !within(X) && @args(c1,f,c2) { + System.err.println("Two colors:"+c1.color()+","+c2.color()); + System.err.println("Fruit is:"+f.value()); + count++; + if (!c1.color().equals("yellow")) + throw new RuntimeException("Color1 should be yellow"); + if (!f.value().equals("apple")) + throw new RuntimeException("Fruit should be apple"); + if (!c2.color().equals("red")) + throw new RuntimeException("Color2 should be red"); + + } + + public static void verify() { + if (count!=3) throw new Error("Should be 3 runs: "+count); + } +} + diff --git a/tests/java5/annotations/binding/AtThis1.aj b/tests/java5/annotations/binding/AtThis1.aj new file mode 100644 index 000000000..500a8b45c --- /dev/null +++ b/tests/java5/annotations/binding/AtThis1.aj @@ -0,0 +1,36 @@ +import java.lang.annotation.*; + +@Retention(RetentionPolicy.RUNTIME) +@interface Colored { String color(); } + +@Colored(color="yellow") +public class AtThis1 { + public static void main(String[]argv) { + new AtThis1().m(); + X.verify(); + } + + @Colored(color="red") + public void m() { + System.err.println("method"); + } + +} + +aspect X { + static int count = 0; + + before(Colored c): call(* *(..)) && !within(X) && @this(c) { + System.err.println(thisJoinPoint+" > "+c.color()); + count++; + + if (!c.color().equals("yellow")) + throw new RuntimeException("Color should be yellow"); + } + + public static void verify() { + if (count!=1) throw new Error("Should be 1 run: "+count); + } + +} + diff --git a/tests/java5/annotations/binding/AtThis2.aj b/tests/java5/annotations/binding/AtThis2.aj new file mode 100644 index 000000000..f8ceac43d --- /dev/null +++ b/tests/java5/annotations/binding/AtThis2.aj @@ -0,0 +1,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); + } +} + diff --git a/tests/java5/annotations/binding/AtThis3.aj b/tests/java5/annotations/binding/AtThis3.aj new file mode 100644 index 000000000..787e26497 --- /dev/null +++ b/tests/java5/annotations/binding/AtThis3.aj @@ -0,0 +1,44 @@ +import java.lang.annotation.*; + +@Retention(RetentionPolicy.RUNTIME) @interface Colored { String color(); } + +public class AtThis3 { + public static void main(String[]argv) { + new A().start(); + new B().start(); + new C().start(); + X.verify(); + } +} + +@Colored(color="yellow") +class A { + public void start() { m();} + public void m() { System.err.println("method"); } +} + +class B extends A { } + +@Colored(color="blue") class C extends B { } + +aspect X { + static int count = 0; + + before(Colored c): call(* m(..)) && !within(X) && @this(c) { + System.err.println(c.color()); + count++; + if (count == 1 && !c.color().equals("yellow")) + throw new RuntimeException("First advice execution, color should be yellow"); + + // The pointcut does 'match' on call to B.m() but at runtime the + // annotation check fails so the advice isn't run + + if (count == 2 && !c.color().equals("blue")) + throw new RuntimeException("Second advice execution, color should be blue"); + } + + public static void verify() { + if (count!=2) throw new Error("Should be 2 runs: "+count); + } +} + diff --git a/tests/java5/annotations/binding/AtThis4.aj b/tests/java5/annotations/binding/AtThis4.aj new file mode 100644 index 000000000..07ba6b040 --- /dev/null +++ b/tests/java5/annotations/binding/AtThis4.aj @@ -0,0 +1,46 @@ +import java.lang.annotation.*; + +@Retention(RetentionPolicy.RUNTIME) @Inherited @interface Colored { String color(); } + +public class AtThis4 { + public static void main(String[]argv) { + new A().start(); + new B().start(); + new C().start(); + X.verify(); + } +} + +@Colored(color="yellow") +class A { + public void start() { m();} // Can't match @this() on calls from static method, + // hence this start() method... + public void m() { System.err.println("method"); } +} + +class B extends A { } // inherits yellow color :) + +@Colored(color="blue") class C extends B { } + +aspect X { + static int count = 0; + + before(Colored c): call(* m(..)) && !within(X) && @this(c) { + System.err.println(c.color() + thisJoinPoint); + count++; + if (count == 1 && !c.color().equals("yellow")) + throw new RuntimeException("First advice execution, color should be yellow"); + + if (count == 2 && !c.color().equals("yellow")) + throw new RuntimeException("Second advice execution, color should be yellow"); + + if (count == 3 && !c.color().equals("blue")) + throw new RuntimeException("Third advice execution, color should be blue"); + + } + + public static void verify() { + if (count!=3) throw new Error("Should be 3 runs: "+count); + } +} + diff --git a/tests/java5/annotations/binding/AtThis5.aj b/tests/java5/annotations/binding/AtThis5.aj new file mode 100644 index 000000000..9614558c4 --- /dev/null +++ b/tests/java5/annotations/binding/AtThis5.aj @@ -0,0 +1,55 @@ +import java.lang.annotation.*; + +@Retention(RetentionPolicy.RUNTIME) @Inherited @interface Colored { String color(); } + +public class AtThis5 { + public static void main(String[]argv) { + new A().start(new B()); + new B().start(new C()); + new C().start(new A()); + X.verify(); + } +} + +@Colored(color="yellow") +class A { + public void start(A a) { a.m();} // Can't match @this() on calls from static method, + // hence this start() method... + public void m() { System.err.println("method"); } +} + +class B extends A { } // inherits yellow color :) + +@Colored(color="blue") class C extends B { } + +aspect X { + static int count = 0; + + before(Colored cThis,Colored cTarg): + call(* m(..)) && !within(X) && @this(cThis) && @target(cTarg) { + + count++; + if (count == 1 && + !(cThis.color().equals("yellow") && cTarg.color().equals("yellow"))) + throw new RuntimeException("1st run, colors should be yellow:yellow but are "+ + cThis.color()+":"+cTarg.color()); + + if (count == 2 && + !(cThis.color().equals("yellow") && cTarg.color().equals("blue"))) + throw new RuntimeException("2nd run, colors should be yellow:blue but are "+ + cThis.color()+":"+cTarg.color()); + + if (count == 3 && + !(cThis.color().equals("blue") && cTarg.color().equals("yellow"))) + throw new RuntimeException("3rd run, colors should be blue:yellow but are "+ + cThis.color()+":"+cTarg.color()); + + if (count > 3) + throw new RuntimeException("Advice should only run three times"); + } + + public static void verify() { + if (count!=3) throw new Error("Should be 3 runs: "+count); + } +} + diff --git a/tests/java5/annotations/binding/CallAnnBinding7.aj b/tests/java5/annotations/binding/CallAnnBinding7.aj new file mode 100644 index 000000000..565def3df --- /dev/null +++ b/tests/java5/annotations/binding/CallAnnBinding7.aj @@ -0,0 +1,43 @@ +import java.lang.annotation.*; + +@Retention(RetentionPolicy.RUNTIME) +@interface Colored { String color(); } + +public class CallAnnBinding7 { + public static void main(String[]argv) { + new CallAnnBinding7().m1(); + new CallAnnBinding7().m2(); + new CallAnnBinding7().m3(); + } + + @Colored(color="red") + public void m1() { + System.err.println("method1"); + } + + @Colored(color="green") + public void m2() { + System.err.println("method2"); + } + + @Colored(color="blue") + public void m3() { + System.err.println("method3"); + } + +} + +aspect X { + int i = 0; + + pointcut p(Colored c): call(* *(..)) && !within(X) && @annotation(c); + + before(Colored c): p(c) { + i++; + if (i==1 && !c.color().equals("red")) throw new RuntimeException("First time through should be red, but is "+c.color()); + if (i==2 && !c.color().equals("green")) throw new RuntimeException("Second time through should be green, but is "+c.color()); + if (i==3 && !c.color().equals("blue")) throw new RuntimeException("Third time through should be blue, but is "+c.color()); + System.err.println(c.color()); + } +} +