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.

ConstructorMain.java 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. public class ConstructorMain { // Bug 61538
  2. public static void main(String args[]) {
  3. ConstructorAspects.preinitcalls = 0;
  4. B b1 = new B();
  5. // preinitcalls should be 2
  6. // System.out.println("Preinitcalls="+ConstructorAspects.preinitcalls);
  7. int bPreinitcalls = ConstructorAspects.preinitcalls;
  8. // Only difference between B and C is the order in which the
  9. // constructors are declared in the class file
  10. ConstructorAspects.preinitcalls = 0;
  11. C c1 = new C();
  12. // preinitcalls should be 2
  13. // System.out.println("Preinitcalls="+ConstructorAspects.preinitcalls);
  14. int cPreinitcalls = ConstructorAspects.preinitcalls;
  15. if (bPreinitcalls!=cPreinitcalls)
  16. throw new RuntimeException("Both b("+bPreinitcalls+") and c("+cPreinitcalls+") should report the same number of preinit jps");
  17. }
  18. }
  19. class A {
  20. int x = 4;
  21. A (int x) { this.x = x; }
  22. }
  23. class B extends A {
  24. int y;
  25. static int k = 4;
  26. static int j = 5;
  27. static int l = 6;
  28. B (int x, int y) { super(x+y); this.y = x+y; }
  29. B (int x) { this(x+l, x+l); this.y = x+l; }
  30. B () { this(k+j); this.y = l; }
  31. }
  32. class C extends A {
  33. int y;
  34. static int k = 4;
  35. static int j = 5;
  36. static int l = 6;
  37. C () { this(k+j); this.y = l; }
  38. C (int x) { this(x+l, x+l); this.y = x+l; }
  39. C (int x, int y) { super(x+y); this.y = x+y; }
  40. }
  41. aspect ConstructorAspects {
  42. public static int preinitcalls = 0;
  43. public static int initcalls = 0;
  44. public static int execs = 0;
  45. static private int aspectnesting = 0;
  46. private final static boolean log = false;
  47. static void message(String s) {
  48. if (log) {
  49. for (int i=0; i<aspectnesting; i++) System.out.print("---+");
  50. System.out.println(s);
  51. }
  52. }
  53. // call of all constructors
  54. pointcut allconstrcalls() : call(*..new(..)) &&
  55. !within(ConstructorAspects) && !call(java.lang..new(..));
  56. // execution of all constructors
  57. pointcut allconstrexecutions() : execution(*..new(..)) &&
  58. !within(ConstructorAspects);
  59. // intialization of all constructors
  60. pointcut allconstrinitializations() : initialization(*..new(..)) &&
  61. !within(ConstructorAspects);
  62. // preinitialization of all constructors
  63. pointcut allconstrpreinitializations() : preinitialization(*..new(..)) &&
  64. !within(ConstructorAspects);
  65. before(): !within(ConstructorAspects) && allconstrpreinitializations() {
  66. preinitcalls++;
  67. }
  68. // before advice
  69. before () : !within(ConstructorAspects) &&
  70. (allconstrpreinitializations() ||
  71. allconstrinitializations() ||
  72. allconstrexecutions() ) {
  73. message(
  74. "BEFORE: " + thisJoinPointStaticPart.getSourceLocation() +
  75. " " +thisJoinPointStaticPart.toLongString());
  76. aspectnesting++;
  77. }
  78. // after advice
  79. after () returning : !within(ConstructorAspects) &&
  80. (allconstrpreinitializations() ||
  81. allconstrinitializations() ||
  82. allconstrexecutions() )
  83. {
  84. aspectnesting--;
  85. message(
  86. "AFTER: " + thisJoinPointStaticPart.getSourceLocation() +
  87. " " +thisJoinPointStaticPart.toLongString());
  88. }
  89. }