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.

Driver.java 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import org.aspectj.testing.Tester;
  2. public class Driver {
  3. public static void main(String[] args) { test(); }
  4. public static void test() {
  5. C0 c = new C2();
  6. c.m1(2, 2);
  7. Tester.checkEqual(A.C0_m1_calls, 1, "C2.m1 -- C0 calls");
  8. Tester.checkEqual(A.C2_m1_calls, 1, "C2.m1 -- C2 calls");
  9. Tester.checkEqual(A.m1_afters, 1, "C2.m1 -- after advice");
  10. A.clearCounts();
  11. c.m2(c);
  12. Tester.checkEqual(A.C0_m1_calls, 1, "C2.m2 -- C0.m1 calls");
  13. Tester.checkEqual(A.C2_m1_calls, 0, "C2.m2 -- C2.m1 calls");
  14. Tester.checkEqual(A.m1_afters, 0, "C2.m2 -- after m1 advice");
  15. Tester.checkEqual(A.C0_m2_calls, 1, "C2.m2 -- C0.m2 calls");
  16. Tester.checkEqual(A.C2_m2_calls, 1, "C2.m2 -- C2.m2 calls");
  17. Tester.checkEqual(A.m2_afters, 1, "C2.m2 -- after m2 advice");
  18. c.m3("hi");
  19. Tester.checkEqual(A.C0_m3_calls, 1, "C2.m3 -- C0 calls");
  20. Tester.checkEqual(A.C1_m3_calls, 1, "C2.m3 -- C1 calls");
  21. Tester.checkEqual(A.C2_m3_calls, 1, "C2.m3 -- C2 calls");
  22. }
  23. }
  24. class C0 {
  25. public C0() {
  26. }
  27. void m1(int x, double y) {
  28. A.C0_m1_calls += 1;
  29. }
  30. void m2(C0 c0) {
  31. A.C0_m2_calls += 1;
  32. }
  33. void m3(String s) {
  34. A.C0_m3_calls += 1;
  35. }
  36. }
  37. class C1 extends C0 {
  38. void m3(String s) {
  39. super.m3(s);
  40. A.C1_m3_calls += 1;
  41. }
  42. }
  43. class C2 extends C1 {
  44. public boolean C2_after_called = false;
  45. public void setC2_after_called( boolean newVal ) {
  46. C2_after_called = newVal;
  47. }
  48. public C2() {
  49. super();
  50. }
  51. void m1(int x, double y) {
  52. A.C2_m1_calls += 1;
  53. super.m1(x*2, x+y);
  54. }
  55. void m2(C0 c0) {
  56. A.C2_m2_calls += 1;
  57. super.m1(2, 2);
  58. super.m2(this);
  59. }
  60. void m3(String s) {
  61. super.m3(s);
  62. A.C2_m3_calls += 1;
  63. }
  64. }
  65. aspect A {
  66. public static int C0_m1_calls = 0;
  67. public static int C2_m1_calls = 0;
  68. public static int C0_m2_calls = 0;
  69. public static int C2_m2_calls = 0;
  70. public static int C0_m3_calls = 0;
  71. public static int C1_m3_calls = 0;
  72. public static int C2_m3_calls = 0;
  73. public static int m1_afters = 0;
  74. public static int m2_afters = 0;
  75. public static void clearCounts() {
  76. C0_m1_calls = 0;
  77. C2_m1_calls = 0;
  78. C0_m2_calls = 0;
  79. C2_m2_calls = 0;
  80. C0_m3_calls = 0;
  81. C1_m3_calls = 0;
  82. C2_m3_calls = 0;
  83. m1_afters = 0;
  84. m2_afters = 0;
  85. }
  86. after(): target(C0+) && call(void m1(int, double)) {
  87. m1_afters += 1;
  88. }
  89. after(): target(C0+) && call(* m2(..)) {
  90. m2_afters += 1;
  91. }
  92. after(): target(C0+) && call(* m3(..)) {
  93. int x = 22;
  94. }
  95. after(C2 c2) returning():
  96. target(c2) && call(new()) {
  97. c2.setC2_after_called( true );
  98. }
  99. }