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.

CallAnnBinding3.aj 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import java.lang.annotation.*;
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @interface Colored { String color(); }
  4. public class CallAnnBinding3 {
  5. public static void main(String[]argv) {
  6. SecondaryClass sc = new SecondaryClass();
  7. // tackle the primitives ...
  8. sc.m1(1); // int
  9. sc.m2(true); // boolean
  10. sc.m3(new Byte("3").byteValue());// byte
  11. sc.m4('a'); // char
  12. sc.m5(new Long(444L).longValue()); // long
  13. sc.m6(new Double(3.3).doubleValue()); // double
  14. sc.m7(new Float(2.2).floatValue()); // float
  15. sc.m8(new Short("2").shortValue()); // short
  16. // how about darn arrays
  17. sc.m9(new int[]{1,2,3},new String[]{"a","b"});
  18. // and the ultimate ... double depth dastardly arrays
  19. String a[][] = new String[5][];
  20. a[0]= new String[3];
  21. sc.m10(a);
  22. }
  23. }
  24. class SecondaryClass {
  25. @Colored(color="red") public void m1(int i) { }
  26. @Colored(color="orange") public void m2(boolean b) {}
  27. @Colored(color="yellow") public void m3(byte b) { }
  28. @Colored(color="green") public void m4(char c) { }
  29. @Colored(color="blue") public void m5(long l) { }
  30. @Colored(color="indigo") public void m6(double d) { }
  31. @Colored(color="violet") public void m7(float f) { }
  32. @Colored(color="black") public void m8(short s) { }
  33. @Colored(color="taupe") public void m9(int[] x,String[] ss) {}
  34. @Colored(color="beige") public void m10(String[][] s) {}
  35. }
  36. aspect X {
  37. int i = 0;
  38. before(Colored c): call(* SecondaryClass.*(..)) && !within(X) && @annotation(c) {
  39. i++;
  40. if (i==1) checkColor(1,c,"red");
  41. if (i==2) checkColor(2,c,"orange");
  42. if (i==3) checkColor(3,c,"yellow");
  43. if (i==4) checkColor(4,c,"green");
  44. if (i==5) checkColor(5,c,"blue");
  45. if (i==6) checkColor(6,c,"indigo");
  46. if (i==7) checkColor(6,c,"violet");
  47. if (i==8) checkColor(6,c,"black");
  48. if (i==9) checkColor(6,c,"taupe");
  49. if (i==10) checkColor(6,c,"beige");
  50. if (i==11) throw new RuntimeException("Advice running more times than expected");
  51. System.err.println(c.color());
  52. }
  53. public void checkColor(int run, Colored c,String exp) {
  54. if (!c.color().equals(exp))
  55. throw new RuntimeException("Advice execution #"+run+" expected "+exp+" but got "+c.color());
  56. }
  57. }