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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import org.aspectj.testing.Tester;
  2. public class Driver {
  3. public static void main(String[] args) { test(); }
  4. public static void test() {
  5. Foo.staticMethod();
  6. Foo.introducedStaticMethod();
  7. Foo foo = new Foo(10);
  8. foo.nonStaticMethod();
  9. foo.introducedNonStaticMethod();
  10. Tester.checkEqual(A.fooStaticCounter, 1, "A.fooStaticCounter");
  11. Tester.checkEqual(A.fooCounter, 1, "A.fooCounter");
  12. Tester.checkEqual(A.aStaticCounter, 1, "A.aStaticCounter");
  13. Tester.checkEqual(A.aCounter, 1, "A.aCounter");
  14. // these is only one constructor call, for Foo
  15. Tester.checkEqual(A.constructorCounter, 1, "constructor calls");
  16. // one for Foo, one for A
  17. Tester.checkEqual(A.initializationCounter, 2, "initializations");
  18. Tester.check(A.ranIntroducedConstructor,
  19. "no overriding of the real thing");
  20. }
  21. }
  22. class Foo {
  23. static void staticMethod() { }
  24. void nonStaticMethod() { }
  25. }
  26. aspect A0_8beta1 {
  27. after() returning(): /*target(*) &&*/ call(new(int)) {
  28. A.constructorCounter++;
  29. }
  30. after() returning(): /*target(*) &&*/ initialization(new(..)) && !within(A0_8beta1) {
  31. System.out.println("init at " + thisJoinPoint);
  32. A.initializationCounter++;
  33. }
  34. before(): within(Foo) && execution(static * Foo.*(..)) {
  35. A.fooStaticCounter++;
  36. }
  37. before(): within(A) && execution(static * Foo.*(..)) {
  38. A.aStaticCounter++;
  39. }
  40. before(): within(A) && execution(!static * Foo.*(..)) {
  41. A.aCounter++;
  42. System.out.println("external before advise on " + thisJoinPoint);
  43. }
  44. }
  45. aspect A pertarget(target(Foo)){
  46. static int constructorCounter = 0;
  47. static int initializationCounter = 0;
  48. static int aStaticCounter = 0;
  49. static int aCounter = 0;
  50. static int fooStaticCounter = 0;
  51. static int fooCounter = 0;
  52. static boolean ranIntroducedConstructor = false;
  53. //introduction Foo {
  54. static void Foo.introducedStaticMethod() {
  55. // System.out.println(thisJoinPoint.className +"."+
  56. // thisJoinPoint.methodName);
  57. }
  58. void Foo.introducedNonStaticMethod() {
  59. // System.out.println(thisJoinPoint.className +"."+
  60. // thisJoinPoint.methodName);
  61. }
  62. Foo.new(int n) { this(); ranIntroducedConstructor = true; }
  63. // make sure advice doesn't go on the toString() method
  64. // this would result in an infinite recursion
  65. before(): within(Foo) && execution(!static * Foo.*(..)) {
  66. fooCounter++;
  67. //System.out.println("before advise on " +
  68. //thisJoinPoint.className +"."+ thisJoinPoint.methodName);
  69. }
  70. public A() { System.err.println("creating: " + this); }
  71. //XXX moved to other aspect, need to think about this...
  72. //before(): within(A) && executions(!static * Foo.*(..)) {
  73. //aCounter++;
  74. //System.out.println("before advise on " + thisJoinPoint);
  75. //}
  76. }