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.

CtorAnnBinding1.aj 1.2KB

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