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.

Tricky2.java 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import org.aspectj.testing.Tester;
  2. import org.aspectj.runtime.NoAspectException;
  3. public class Simple {
  4. public static void main(String[] args) {
  5. C c = new C();
  6. c.m();
  7. // aspect A is bound to the base class C
  8. Tester.checkEqual(c.history.toString(), "A.before, C.m, ", "C");
  9. SubC1 subc1 = new SubC1();
  10. subc1.m();
  11. // aspect A is also bound to the subclass SubC1
  12. //Tester.checkEqual(subc1.history.toString(), "A.before, SubC1.m, C.m, ", "SubC1");
  13. SubC2 subc2 = new SubC2();
  14. subc2.m();
  15. // aspect A is overriden by SubA on the class SubC2
  16. //Tester.checkEqual(subc2.history.toString(), "SubA.before, A.before, C.m, ", "SubC2");
  17. // retrieving the SubA aspect using its super's aspectOf static method
  18. //Tester.check(A.aspectOf(subc2) instanceof SubA, "A.aspectOf(subc2) instanceof SubA");
  19. // retrieving the SubA aspect using its own aspectOf method
  20. //Tester.check(SubA.aspectOf(subc2) instanceof SubA, "SubA.aspectOf(subc2) instanceof SubA");
  21. try {
  22. // trying to retrieve a SubA aspect where only an A aspect is present
  23. SubA subA = SubA.aspectOf(c);
  24. Tester.checkFailed("SubA.aspectOf(c) should throw an exception");
  25. } catch (NoAspectException nae) {
  26. Tester.note("NoAspectException caught");
  27. }
  28. Tester.check("NoAspectException caught");
  29. }
  30. }
  31. class C {
  32. // records what methods are called and advice are run
  33. public StringBuffer history = new StringBuffer();
  34. public void m() {
  35. history.append("C.m, ");
  36. }
  37. }
  38. class SubC1 extends C {
  39. public void m() {
  40. history.append("SubC1.m, ");
  41. super.m();
  42. }
  43. }
  44. class SubC2 extends C {
  45. }
  46. abstract aspect A {
  47. //StringBuffer history = null;
  48. after (Object object) returning (): instanceof(object) && receptions(new(..)) {
  49. //history = c.history;
  50. System.out.println("created a: " + object + " on a " + this);
  51. }
  52. abstract pointcut targets();
  53. before (): targets() {
  54. System.out.println("A.before, ");
  55. }
  56. }
  57. aspect SubA extends A of eachobject(instanceof(C)) {
  58. pointcut targets(): receptions(void m());
  59. }