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.

AroundChangeThis.java 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import org.aspectj.testing.Tester;
  2. public class AroundChangeThis {
  3. public static void main(String[] args) {
  4. C c1 = new C("c1");
  5. C c2 = new C("c2");
  6. SubC sc = new SubC("sc");
  7. c1.m(c2);
  8. Tester.checkAndClearEvents(new String[] { "c1.m(c2)", "c2.m(c1)" });
  9. c1.m(sc);
  10. Tester.checkAndClearEvents(new String[] { "c1.m(sc)", "sc.m(c1)" });
  11. // this is the 1.3 behaviour
  12. // sc.m(c1);
  13. // Tester.checkAndClearEvents(new String[] { "sc.m(c1)", "c1.m(sc)" });
  14. try {
  15. // the 1.4 behaviour is....
  16. // in byte code we have a call to SubC.m
  17. sc.m(c1);
  18. Tester.checkFailed("Expected ClassCastException");
  19. } catch (ClassCastException e) {
  20. }
  21. try {
  22. sc.m1(c1);
  23. Tester.checkFailed("Expected ClassCastException");
  24. } catch (ClassCastException e) {
  25. }
  26. Tester.printEvents();
  27. }
  28. }
  29. class C {
  30. private String name;
  31. public C(String name) { this.name = name; }
  32. public String toString() { return name; }
  33. public void m(Object other) {
  34. Tester.event(this + ".m(" + other + ")");
  35. }
  36. }
  37. class SubC extends C {
  38. public SubC(String name) { super(name); }
  39. public void m1(Object other) {
  40. Tester.event(this + ".m1(" + other + ")");
  41. }
  42. }
  43. aspect A {
  44. /* Swaps this with arg for calls of C.m(C) */
  45. void around(C thisC, C argC): execution(void m*(*)) && this(thisC) && args(argC) {
  46. proceed(argC, thisC);
  47. proceed(thisC, argC);
  48. }
  49. void around(C thisC, C argC): call(void m*(*)) && target(thisC) && args(argC) {
  50. proceed(argC, thisC);
  51. }
  52. }