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.

InitBinding.aj 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import java.lang.annotation.*;
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @interface Colored { String color(); }
  4. public class InitBinding {
  5. public static void main(String[]argv) {
  6. new A();
  7. new B();
  8. new C();
  9. X.verifyRun();
  10. }
  11. }
  12. class A {
  13. @Colored(color="orange") public A() {}
  14. }
  15. class B {
  16. @Colored(color="yellow") public B() {}
  17. }
  18. class C {
  19. @Colored(color="brown") public C() {}
  20. }
  21. aspect X {
  22. // Expected color order
  23. static String exp[] = new String[]{"orange","yellow","brown"};
  24. static int i = 0; // Count of advice executions
  25. before(Colored c): preinitialization(new(..)) && !within(X) && @annotation(c) {
  26. System.err.println(thisJoinPoint+" color="+c.color());
  27. if (!c.color().equals(exp[i])) throw new RuntimeException("not "+exp[i]+"? "+c.color());
  28. i++;
  29. }
  30. public static void verifyRun() {
  31. if (X.i != exp.length)
  32. throw new RuntimeException("Expected "+exp.length+" advice runs but did "+X.i);
  33. }
  34. }
  35. //aspect X {
  36. // int i = 0;
  37. //
  38. // before(Colored c): preinitialization(new(..)) && !within(X) && @annotation(c) {
  39. // i++;
  40. // System.err.println(thisJoinPoint+" color="+c.color());
  41. // if (i==1 && !c.color().equals("orange")) throw new RuntimeException("First time through should be red, but is "+c.color());
  42. // if (i==2 && !c.color().equals("yellow")) throw new RuntimeException("Second time through should be green, but is "+c.color());
  43. // if (i==3 && !c.color().equals("brown")) throw new RuntimeException("Third time through should be blue, but is "+c.color());
  44. // System.err.println(c.color());
  45. // }
  46. //}
  47. //