--- /dev/null
+import java.lang.annotation.*;\r
+\r
+@Retention(RetentionPolicy.RUNTIME)\r
+@interface Colored { String color(); }\r
+\r
+@Colored(color="yellow")\r
+public class AtTarget1 {\r
+ public static void main(String[]argv) {\r
+ new AtTarget1().m();\r
+ }\r
+\r
+ @Colored(color="red")\r
+ public void m() {\r
+ System.err.println("method");\r
+ }\r
+\r
+}\r
+\r
+aspect X {\r
+ int adviceExecutions = 0;\r
+\r
+ before(Colored c): call(* *(..)) && !within(X) && @target(c) {\r
+ System.err.println(c.color());\r
+ adviceExecutions++;\r
+ \r
+ if (!c.color().equals("yellow")) \r
+ throw new RuntimeException("Color should be yellow");\r
+ \r
+ if (adviceExecutions>1)\r
+ throw new RuntimeException("Advice shouldn't be called more than once");\r
+ }\r
+\r
+}\r
+\r
--- /dev/null
+import java.lang.annotation.*;\r
+\r
+@Retention(RetentionPolicy.RUNTIME)\r
+@interface Colored { String color(); }\r
+\r
+@Retention(RetentionPolicy.RUNTIME)\r
+@interface Material { String material(); }\r
+\r
+@Colored(color="yellow") @Material(material="wood")\r
+public class AtTarget2 {\r
+ public static void main(String[]argv) {\r
+ new AtTarget2().m();\r
+ new SubA().m();\r
+ new SubB().m();\r
+ }\r
+\r
+ @Colored(color="red")\r
+ public void m() {\r
+ System.err.println("method running\n");\r
+ }\r
+}\r
+\r
+@Material(material="metal") @Colored(color="green")\r
+class SubA extends AtTarget2 { }\r
+\r
+@Material(material="jelly") @Colored(color="magenta")\r
+class SubB extends SubA { }\r
+\r
+aspect X {\r
+ int adviceexecutions = 0;\r
+\r
+ before(Colored c,Material m): call(* *(..)) && !within(X) && @target(c) && @target(m) {\r
+ System.err.println("advice running ("+c.color()+","+m.material()+")");\r
+ adviceexecutions++;\r
+ \r
+ if (adviceexecutions == 1) {\r
+ if (!c.color().equals("yellow"))\r
+ throw new RuntimeException("First advice execution, color should be yellow");\r
+ if (!m.material().equals("wood"))\r
+ throw new RuntimeException("First advice execution, material should be wood");\r
+ }\r
+ if (adviceexecutions == 2) {\r
+ if (!c.color().equals("green"))\r
+ throw new RuntimeException("Second advice execution, color should be green");\r
+ if (!m.material().equals("metal"))\r
+ throw new RuntimeException("Second advice execution, material should be metal");\r
+ }\r
+ if (adviceexecutions == 3) {\r
+ if (!c.color().equals("magenta"))\r
+ throw new RuntimeException("Third advice execution, color should be magenta");\r
+ if (!m.material().equals("jelly"))\r
+ throw new RuntimeException("Third advice execution, material should be jelly");\r
+ }\r
+ if (adviceexecutions > 3) \r
+ throw new RuntimeException("Advice should only run twice");\r
+ }\r
+}\r
+\r
--- /dev/null
+import java.lang.annotation.*;\r
+\r
+@Retention(RetentionPolicy.RUNTIME) @interface Colored { String color(); }\r
+\r
+public class AtTarget3 {\r
+ public static void main(String[]argv) {\r
+ new A().m();\r
+ new B().m();\r
+ new C().m();\r
+ }\r
+}\r
+\r
+@Colored(color="yellow")\r
+class A {\r
+ public void m() { System.err.println("method"); }\r
+}\r
+\r
+class B extends A { }\r
+\r
+@Colored(color="blue") class C extends B { }\r
+\r
+aspect X {\r
+ int adviceexecutions = 0;\r
+\r
+ before(Colored c): call(* *(..)) && !within(X) && @target(c) {\r
+ System.err.println(c.color());\r
+ adviceexecutions++;\r
+ if (adviceexecutions == 1 && !c.color().equals("yellow"))\r
+ throw new RuntimeException("First advice execution, color should be yellow");\r
+ \r
+ // The pointcut does 'match' on call to B.m() but at runtime the annotation check fails so the advice isn't run\r
+ \r
+ if (adviceexecutions == 2 && !c.color().equals("blue"))\r
+ throw new RuntimeException("Second advice execution, color should be blue");\r
+ \r
+ if (adviceexecutions > 2) \r
+ throw new RuntimeException("Advice should only run twice");\r
+ }\r
+}\r
+\r
--- /dev/null
+import java.lang.annotation.*;\r
+\r
+@Retention(RetentionPolicy.RUNTIME) @Inherited @interface Colored { String color(); }\r
+\r
+public class AtTarget4 {\r
+ public static void main(String[]argv) {\r
+ new A().m();\r
+ new B().m();\r
+ new C().m();\r
+ }\r
+}\r
+\r
+@Colored(color="yellow")\r
+class A {\r
+ public void m() { System.err.println("method"); }\r
+}\r
+\r
+class B extends A { } // inherits yellow color :)\r
+\r
+@Colored(color="blue") class C extends B { }\r
+\r
+aspect X {\r
+ int adviceexecutions = 0;\r
+\r
+ before(Colored c): call(* *(..)) && !within(X) && @target(c) {\r
+ System.err.println(c.color() + thisJoinPoint);\r
+ adviceexecutions++;\r
+ if (adviceexecutions == 1 && !c.color().equals("yellow"))\r
+ throw new RuntimeException("First advice execution, color should be yellow");\r
+ \r
+ if (adviceexecutions == 2 && !c.color().equals("yellow"))\r
+ throw new RuntimeException("Second advice execution, color should be yellow");\r
+ \r
+ if (adviceexecutions == 3 && !c.color().equals("blue"))\r
+ throw new RuntimeException("Third advice execution, color should be blue");\r
+ \r
+ if (adviceexecutions > 3) \r
+ throw new RuntimeException("Advice should only run twice");\r
+ }\r
+}\r
+\r
--- /dev/null
+import java.lang.annotation.*;\r
+\r
+@Retention(RetentionPolicy.RUNTIME)\r
+@interface Colored { String color(); }\r
+\r
+public class CallAnnBinding {\r
+ public static void main(String[]argv) {\r
+ new CallAnnBinding().m1();\r
+ new CallAnnBinding().m2();\r
+ new CallAnnBinding().m3();\r
+ }\r
+\r
+ @Colored(color="red")\r
+ public void m1() {\r
+ System.err.println("method1");\r
+ }\r
+\r
+ @Colored(color="green")\r
+ public void m2() {\r
+ System.err.println("method2");\r
+ }\r
+\r
+ @Colored(color="blue")\r
+ public void m3() {\r
+ System.err.println("method3");\r
+ }\r
+\r
+}\r
+\r
+aspect X {\r
+ int i = 0;\r
+ \r
+ before(Colored c): call(* *(..)) && !within(X) && @annotation(c) {\r
+ i++;\r
+ if (i==1 && !c.color().equals("red")) throw new RuntimeException("First time through should be red, but is "+c.color());\r
+ if (i==2 && !c.color().equals("green")) throw new RuntimeException("Second time through should be green, but is "+c.color());\r
+ if (i==3 && !c.color().equals("blue")) throw new RuntimeException("Third time through should be blue, but is "+c.color());\r
+ System.err.println(c.color());\r
+ }\r
+}\r
+\r
--- /dev/null
+import java.lang.annotation.*;\r
+\r
+@Retention(RetentionPolicy.RUNTIME)\r
+@interface Colored { String color(); }\r
+\r
+public class CallAnnBinding2 {\r
+ public static void main(String[]argv) {\r
+ SecondaryClass sc = new SecondaryClass();\r
+ sc.m1(1);\r
+ sc.m2("hello");\r
+ sc.m3("hello",1);\r
+ }\r
+\r
+}\r
+\r
+class SecondaryClass {\r
+ \r
+ @Colored(color="cyan")\r
+ public void m1(int i) {\r
+ }\r
+ \r
+ @Colored(color="magenta")\r
+ public void m2(String s) {\r
+ }\r
+ \r
+ @Colored(color="mauve")\r
+ public void m3(String s,int i) { \r
+ }\r
+}\r
+\r
+aspect X {\r
+ int i = 0;\r
+ \r
+ before(Colored c): call(* SecondaryClass.*(..)) && !within(X) && @annotation(c) {\r
+ i++;\r
+ if (i==1) checkColor(1,c,"cyan");\r
+ if (i==2) checkColor(2,c,"magenta");\r
+ if (i==3) checkColor(3,c,"mauve");\r
+ System.err.println(c.color());\r
+ }\r
+ \r
+ public void checkColor(int run, Colored c,String exp) {\r
+ if (!c.color().equals(exp)) \r
+ throw new RuntimeException("Advice execution #"+run+" expected "+exp+" but got "+c.color());\r
+ }\r
+}\r
+\r
--- /dev/null
+import java.lang.annotation.*;\r
+\r
+@Retention(RetentionPolicy.RUNTIME)\r
+@interface Colored { String color(); }\r
+\r
+public class CallAnnBinding3 {\r
+ public static void main(String[]argv) {\r
+ SecondaryClass sc = new SecondaryClass();\r
+ \r
+ // tackle the primitives ...\r
+ \r
+ sc.m1(1); // int\r
+ sc.m2(true); // boolean\r
+ sc.m3(new Byte("3").byteValue());// byte\r
+ sc.m4('a'); // char\r
+ sc.m5(new Long(444L).longValue()); // long\r
+ sc.m6(new Double(3.3).doubleValue()); // double\r
+ sc.m7(new Float(2.2).floatValue()); // float\r
+ sc.m8(new Short("2").shortValue()); // short\r
+ \r
+ // how about darn arrays\r
+ \r
+ sc.m9(new int[]{1,2,3},new String[]{"a","b"});\r
+ \r
+ // and the ultimate ... double depth dastardly arrays\r
+ \r
+ String a[][] = new String[5][];\r
+ a[0]= new String[3];\r
+ sc.m10(a);\r
+ }\r
+\r
+}\r
+\r
+class SecondaryClass {\r
+ \r
+ @Colored(color="red") public void m1(int i) { }\r
+ @Colored(color="orange") public void m2(boolean b) {} \r
+ @Colored(color="yellow") public void m3(byte b) { }\r
+ @Colored(color="green") public void m4(char c) { }\r
+ @Colored(color="blue") public void m5(long l) { }\r
+ @Colored(color="indigo") public void m6(double d) { }\r
+ @Colored(color="violet") public void m7(float f) { }\r
+ @Colored(color="black") public void m8(short s) { }\r
+ \r
+ @Colored(color="taupe") public void m9(int[] x,String[] ss) {}\r
+ \r
+ @Colored(color="beige") public void m10(String[][] s) {}\r
+}\r
+\r
+aspect X {\r
+ int i = 0;\r
+ \r
+ before(Colored c): call(* SecondaryClass.*(..)) && !within(X) && @annotation(c) {\r
+ i++;\r
+ if (i==1) checkColor(1,c,"red");\r
+ if (i==2) checkColor(2,c,"orange");\r
+ if (i==3) checkColor(3,c,"yellow");\r
+ if (i==4) checkColor(4,c,"green");\r
+ if (i==5) checkColor(5,c,"blue");\r
+ if (i==6) checkColor(6,c,"indigo");\r
+ if (i==7) checkColor(6,c,"violet");\r
+ if (i==8) checkColor(6,c,"black");\r
+ \r
+ if (i==9) checkColor(6,c,"taupe");\r
+\r
+ if (i==10) checkColor(6,c,"beige");\r
+ \r
+ if (i==11) throw new RuntimeException("Advice running more times than expected");\r
+ System.err.println(c.color());\r
+ }\r
+ \r
+ public void checkColor(int run, Colored c,String exp) {\r
+ if (!c.color().equals(exp)) \r
+ throw new RuntimeException("Advice execution #"+run+" expected "+exp+" but got "+c.color());\r
+ }\r
+}\r
+\r
--- /dev/null
+import java.lang.annotation.*;\r
+\r
+@Retention(RetentionPolicy.RUNTIME)\r
+@interface Colored { String color(); }\r
+\r
+interface Marker { @Colored(color="blue") public void m1(); public void m2(); }\r
+\r
+public class CallAnnBinding4 {\r
+ public static void main(String[]argv) {\r
+ Marker marker = new SecondaryClass();\r
+ \r
+ // tackle the primitives ...\r
+ \r
+ marker.m1();\r
+ marker.m2();\r
+ \r
+ if (X.i!=1) throw new RuntimeException("Why did the advice not run once?");\r
+ }\r
+\r
+}\r
+\r
+class SecondaryClass implements Marker {\r
+ \r
+ @Colored(color="red") public void m1() {}\r
+ @Colored(color="orange") public void m2() {} \r
+}\r
+\r
+aspect X {\r
+ public static int i = 0;\r
+ \r
+ before(Colored c): call(* m*(..)) && !within(X) && @annotation(c) {\r
+ i++;\r
+ if (i==1) checkColor(1,c,"blue");\r
+ \r
+ if (i==2) throw new RuntimeException("Advice running more times than expected");\r
+ System.err.println(c.color());\r
+ }\r
+ \r
+ public void checkColor(int run, Colored c,String exp) {\r
+ if (!c.color().equals(exp)) \r
+ throw new RuntimeException("Advice execution #"+run+" expected "+exp+" but got "+c.color());\r
+ }\r
+}\r
+\r
--- /dev/null
+import java.lang.annotation.*;\r
+\r
+@Retention(RetentionPolicy.RUNTIME)\r
+@interface Colored { String color(); }\r
+\r
+interface Marker { public void m1(); public void m2(); }\r
+\r
+public class CallAnnBinding5 {\r
+ public static void main(String[]argv) {\r
+ SecondaryClass sc = new SecondaryClass();\r
+ sc.m1();\r
+ sc.m2();\r
+ if (X.i!=1) throw new RuntimeException("Why did the advice not run?");\r
+ }\r
+\r
+}\r
+\r
+class SecondaryClass {\r
+ @Colored(color="red") public void m1() { }\r
+ public void m2() {} \r
+}\r
+\r
+aspect X {\r
+ public static int i = 0;\r
+ \r
+ before(Colored c): call(* SecondaryClass.*(..)) && !within(X) && @annotation(c) {\r
+ i++;\r
+ if (i==1) checkColor(1,c,"red");\r
+ \r
+ if (i==11) throw new RuntimeException("Advice running more times than expected");\r
+ System.err.println(c.color());\r
+ }\r
+ \r
+ public void checkColor(int run, Colored c,String exp) {\r
+ if (!c.color().equals(exp)) \r
+ throw new RuntimeException("Advice execution #"+run+" expected "+exp+" but got "+c.color());\r
+ }\r
+}\r
+\r
--- /dev/null
+import java.lang.annotation.*;\r
+\r
+@Retention(RetentionPolicy.RUNTIME)\r
+@interface Colored { String color(); }\r
+\r
+public class CallAnnBinding6 {\r
+ public static void main(String[]argv) {\r
+ A b = new B();\r
+ b.m();\r
+ if (X.i!=1) throw new RuntimeException("Why did the advice not run?");\r
+ }\r
+}\r
+\r
+class A {\r
+ @Colored(color="RedA") public void m() {}\r
+}\r
+class B extends A {\r
+ @Colored(color="RedB") public void m() {}\r
+}\r
+\r
+aspect X {\r
+ public static int i = 0;\r
+ \r
+ before(Colored c): call(* m*(..)) && !within(X) && @annotation(c) {\r
+ i++;\r
+ if (i==1) checkColor(1,c,"RedA");\r
+ \r
+ if (i==11) throw new RuntimeException("Advice running more times than expected");\r
+ System.err.println(c.color());\r
+ }\r
+ \r
+ public void checkColor(int run, Colored c,String exp) {\r
+ if (!c.color().equals(exp)) \r
+ throw new RuntimeException("Advice execution #"+run+" expected "+exp+" but got "+c.color());\r
+ }\r
+}\r
+\r
--- /dev/null
+import java.lang.annotation.*;\r
+\r
+@Retention(RetentionPolicy.RUNTIME)\r
+@interface Colored { String color(); }\r
+\r
+public class ExecutionAnnBinding1 {\r
+ public static void main(String[]argv) {\r
+ new ExecutionAnnBinding1().m1();\r
+ new ExecutionAnnBinding1().m2();\r
+ new ExecutionAnnBinding1().m3();\r
+ }\r
+\r
+ @Colored(color="red")\r
+ public void m1() {\r
+ System.err.println("method1");\r
+ }\r
+\r
+ @Colored(color="green")\r
+ public void m2() {\r
+ System.err.println("method2");\r
+ }\r
+\r
+ @Colored(color="blue")\r
+ public void m3() {\r
+ System.err.println("method3");\r
+ }\r
+\r
+}\r
+\r
+aspect X {\r
+ int i = 0;\r
+ \r
+ before(Colored c): execution(* *(..)) && !within(X) && @annotation(c) {\r
+ i++;\r
+ if (i==1 && !c.color().equals("red")) \r
+ throw new RuntimeException("First time through should be red, but is "+c.color());\r
+ if (i==2 && !c.color().equals("green")) \r
+ throw new RuntimeException("Second time through should be green, but is "+c.color());\r
+ if (i==3 && !c.color().equals("blue")) \r
+ throw new RuntimeException("Third time through should be blue, but is "+c.color());\r
+ System.err.println(c.color());\r
+ }\r
+}\r
+\r
--- /dev/null
+package a.b.c;
+import g.h.i.B;
+
+import d.e.f.*;
+
+public class A {
+ public static void main(String []argv) {
+ new A().a();
+ new B().b();
+ }
+
+ @Color("blue")
+ public void a() {
+ System.err.println("a.b.c.A.a() running");
+ }
+}
--- /dev/null
+package g.h.i;
+
+import d.e.f.*;
+
+public class B {
+
+ @Color("red") public void b() { System.err.println("g.h.i.B.b running");}
+}
--- /dev/null
+package d.e.f;
+import java.lang.annotation.*;
+@Retention(RetentionPolicy.RUNTIME) public @interface Color {String value();}
--- /dev/null
+// Color should be resolved via the import statement...
+import d.e.f.Color;
+
+public aspect X {
+
+ before(): call(* *(..)) && @annotation(@Color) {
+ System.err.println("Before call to "+thisJoinPoint);
+ }
+
+ before(): execution(* *(..)) && @annotation(@Color) {
+ System.err.println("Before execution of "+thisJoinPoint);
+ }
+}
--- /dev/null
+// Same as X but includes annotation binding
+import d.e.f.Color;
+
+public aspect X2 {
+
+ before(Color c): call(* *(..)) && @annotation(c) {
+ System.err.println("Before call to "+thisJoinPoint+" color is "+c);
+ }
+
+ before(Color c): execution(* *(..)) && @annotation(c) {
+ System.err.println("Before execution of "+thisJoinPoint+" color is "+c);
+ }
+
+}