You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CallAnnBinding.aj 1.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import java.lang.annotation.*;
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @interface Colored { String color(); }
  4. public class CallAnnBinding {
  5. public static void main(String[]argv) {
  6. new CallAnnBinding().m1();
  7. new CallAnnBinding().m2();
  8. new CallAnnBinding().m3();
  9. }
  10. @Colored(color="red")
  11. public void m1() {
  12. System.err.println("method1");
  13. }
  14. @Colored(color="green")
  15. public void m2() {
  16. System.err.println("method2");
  17. }
  18. @Colored(color="blue")
  19. public void m3() {
  20. System.err.println("method3");
  21. }
  22. }
  23. aspect X {
  24. int i = 0;
  25. before(Colored c): call(* *(..)) && !within(X) && @annotation(c) {
  26. i++;
  27. if (i==1 && !c.color().equals("red")) throw new RuntimeException("First time through should be red, but is "+c.color());
  28. if (i==2 && !c.color().equals("green")) throw new RuntimeException("Second time through should be green, but is "+c.color());
  29. if (i==3 && !c.color().equals("blue")) throw new RuntimeException("Third time through should be blue, but is "+c.color());
  30. System.err.println(c.color());
  31. }
  32. }