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.

AroundInnerCalls.java 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import org.aspectj.testing.Tester;
  2. public class AroundInnerCalls {
  3. public static void main(String[] args) {
  4. new Outer().foo();
  5. Tester.check("Outer.foo() calls Outer.Inner.mi()");
  6. Tester.check("Outer.foo() calls Outer.InnerRandom.nextHook(..)");
  7. Tester.check("Outer.InnerRandom.nextHook(..) calls Outer.InnerRandom.next(..)");
  8. Tester.check("Outer.Inner.mi() calls PrintStream.println(..)");
  9. Tester.check("X.toString()");
  10. Tester.check("Outer.foo() calls Outer.1.nextInt(..)");
  11. }
  12. }
  13. class Outer {
  14. private class Inner extends Object {
  15. public void mi() {
  16. System.out.println(".");
  17. }
  18. }
  19. public void foo() {
  20. new Inner().mi();
  21. new InnerRandom().nextHook(2);
  22. new java.util.Random() { public String toString() { Tester.note("X.toString()"); return "X"; } }.nextInt(2);
  23. }
  24. private class InnerRandom extends java.util.Random {
  25. public int nextHook(int bits) {
  26. return next(bits);
  27. }
  28. }
  29. }
  30. aspect A {
  31. Object around(): call(* *(..)) && !within(A) {
  32. // System.out.println
  33. Tester.note
  34. (thisEnclosingJoinPointStaticPart.getSignature().toShortString() +
  35. " calls " + thisJoinPointStaticPart.getSignature().toShortString());
  36. return proceed();
  37. }
  38. before(Object caller, Object callee):
  39. this(caller) && target(callee) && call(* *(..)) && !within(A)
  40. {
  41. System.out.println(thisEnclosingJoinPointStaticPart.getSignature().toShortString() +
  42. " calls " + thisJoinPointStaticPart.getSignature().toShortString());
  43. System.out.println
  44. (caller + "." + thisEnclosingJoinPointStaticPart.getSignature().getName() +
  45. " calls " + callee + "." + thisJoinPoint.getSignature().getName());
  46. }
  47. }