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.

CallAnnBinding4.aj 1.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import java.lang.annotation.*;
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @interface Colored { String color(); }
  4. interface Marker { @Colored(color="blue") public void m1(); public void m2(); }
  5. public class CallAnnBinding4 {
  6. public static void main(String[]argv) {
  7. Marker marker = new SecondaryClass();
  8. // tackle the primitives ...
  9. marker.m1();
  10. marker.m2();
  11. if (X.i!=1) throw new RuntimeException("Why did the advice not run once?");
  12. }
  13. }
  14. class SecondaryClass implements Marker {
  15. @Colored(color="red") public void m1() {}
  16. @Colored(color="orange") public void m2() {}
  17. }
  18. aspect X {
  19. public static int i = 0;
  20. before(Colored c): call(* m*(..)) && !within(X) && @annotation(c) {
  21. i++;
  22. if (i==1) checkColor(1,c,"blue");
  23. if (i==2) throw new RuntimeException("Advice running more times than expected");
  24. System.err.println(c.color());
  25. }
  26. public void checkColor(int run, Colored c,String exp) {
  27. if (!c.color().equals(exp))
  28. throw new RuntimeException("Advice execution #"+run+" expected "+exp+" but got "+c.color());
  29. }
  30. }