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.

WithinCodeBinding1.aj 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import java.lang.annotation.*;
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @interface Colored { String color();}
  4. public class WithinCodeBinding1 {
  5. @Colored(color="red") void mRed() {System.out.println("red"); }
  6. @Colored(color="blue") void mBlue() {System.out.println("blue");}
  7. @Colored(color="green") void mGreen() {System.out.println("green");}
  8. @Colored(color="yellow") WithinCodeBinding1() {
  9. System.out.println("yellow");
  10. }
  11. public static void main(String[]argv) {
  12. WithinCodeBinding1 instance = new WithinCodeBinding1();
  13. instance.mRed();
  14. instance.mBlue();
  15. instance.mGreen();
  16. X.verifyRun();
  17. }
  18. }
  19. aspect X {
  20. // Expected color order
  21. static String exp[] = new String[]{"yellow","red","blue","green"};
  22. static int i = 0; // Count of advice executions
  23. before(Colored c): @withincode(c) && call(* println(..)) {
  24. System.err.println(thisJoinPoint+" color="+c.color());
  25. if (!c.color().equals(exp[i])) throw new RuntimeException("not "+exp[i]+"? "+c.color());
  26. i++;
  27. }
  28. public static void verifyRun() {
  29. if (X.i != exp.length)
  30. throw new RuntimeException("Expected "+exp.length+" advice runs but did "+X.i);
  31. }
  32. }