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.

StaticTypeInIf.java 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import org.aspectj.testing.Tester;
  2. // we want to be doing tests based on dynamic type in if
  3. public class StaticTypeInIf {
  4. public static void main(String[] args) {
  5. D d = new D();
  6. C c = new C();
  7. c.foo(c);
  8. Tester.checkAndClearEvents(new String[] {});
  9. A.setF(d, false);
  10. c.foo(d);
  11. Tester.checkAndClearEvents(new String[] {});
  12. A.setF(d, true);
  13. c.foo(d);
  14. Tester.checkAndClearEvents(new String[] {"args"});
  15. A.setF(d, false);
  16. d.foo(c);
  17. Tester.checkAndClearEvents(new String[] {});
  18. A.setF(d, true);
  19. d.foo(c);
  20. Tester.checkAndClearEvents(new String[] {"this", "target"});
  21. A.setF(d, false);
  22. d.foo(d);
  23. Tester.checkAndClearEvents(new String[] {});
  24. A.setF(d, true);
  25. d.foo(d);
  26. Tester.checkAndClearEvents(new String[] {"args", "this", "target"});
  27. }
  28. }
  29. class C {
  30. void foo(C c) {}
  31. }
  32. class D extends C {
  33. void foo(C c) {}
  34. }
  35. aspect A {
  36. private boolean D.f = false;
  37. static void setF(D d, boolean b) { d.f = b; }
  38. pointcut foo(D d): call(void C.foo(C)) && target(d) && if(d.f);
  39. before (C c): foo(c) {
  40. Tester.event("target");
  41. }
  42. before (D d): if(d.f) && call(void C.foo(C)) && args(d) {
  43. Tester.event("args");
  44. }
  45. before (D d): execution(void C.foo(C)) && this(d) && if(d.f) {
  46. Tester.event("this");
  47. }
  48. }