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.

FieldAnnBinding2.aj 1.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import java.lang.annotation.*;
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @interface Colored { String color();}
  4. public class FieldAnnBinding2 {
  5. @Colored(color="red") static int i;
  6. @Colored(color="blue") String s;
  7. @Colored(color="green") boolean b;
  8. public static void main(String[]argv) {
  9. FieldAnnBinding2 fab = new FieldAnnBinding2();
  10. i = 5;
  11. fab.s = "abc";
  12. fab.b = true;
  13. System.err.println("i="+fab.i);
  14. System.err.println("s="+fab.s);
  15. System.err.println("b="+fab.b);
  16. X.verifyRun();
  17. }
  18. }
  19. aspect X {
  20. // Expected color order
  21. static String exp[] = new String[]{"red","blue","green"};
  22. static int i = 0; // Count of advice executions
  23. before(Colored c): get(* *) && withincode(* main(..)) && @annotation(c) {
  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. }