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.

AtTarget4.aj 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import java.lang.annotation.*;
  2. @Retention(RetentionPolicy.RUNTIME) @Inherited @interface Colored { String color(); }
  3. public class AtTarget4 {
  4. public static void main(String[]argv) {
  5. new A().m();
  6. new B().m();
  7. new C().m();
  8. }
  9. }
  10. @Colored(color="yellow")
  11. class A {
  12. public void m() { System.err.println("method"); }
  13. }
  14. class B extends A { } // inherits yellow color :)
  15. @Colored(color="blue") class C extends B { }
  16. aspect X {
  17. int adviceexecutions = 0;
  18. before(Colored c): call(* *(..)) && !within(X) && @target(c) {
  19. System.err.println(c.color() + thisJoinPoint);
  20. adviceexecutions++;
  21. if (adviceexecutions == 1 && !c.color().equals("yellow"))
  22. throw new RuntimeException("First advice execution, color should be yellow");
  23. if (adviceexecutions == 2 && !c.color().equals("yellow"))
  24. throw new RuntimeException("Second advice execution, color should be yellow");
  25. if (adviceexecutions == 3 && !c.color().equals("blue"))
  26. throw new RuntimeException("Third advice execution, color should be blue");
  27. if (adviceexecutions > 3)
  28. throw new RuntimeException("Advice should only run twice");
  29. }
  30. }