aboutsummaryrefslogtreecommitdiffstats
path: root/tests/bugs/ConstructorMain.java
blob: 3814d9a9116503972b1e38b96c609663240e4bb8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
public class ConstructorMain { // Bug 61538

  public static void main(String args[]) { 
  	  
	  ConstructorAspects.preinitcalls = 0;
	  B b1 = new B();
	  // preinitcalls should be 2
	  // System.out.println("Preinitcalls="+ConstructorAspects.preinitcalls);
	  int bPreinitcalls = ConstructorAspects.preinitcalls;
	  
      // Only difference between B and C is the order in which the 
      // constructors are declared in the class file
	  ConstructorAspects.preinitcalls = 0;
	  C c1 = new C();
	  // preinitcalls should be 2
	  // System.out.println("Preinitcalls="+ConstructorAspects.preinitcalls);
	  int cPreinitcalls = ConstructorAspects.preinitcalls;
	  
	  if (bPreinitcalls!=cPreinitcalls)
	  	throw new RuntimeException("Both b("+bPreinitcalls+") and c("+cPreinitcalls+") should report the same number of preinit jps");
	}
}

class A {
  int x = 4;
  A (int x) { this.x = x; }
}

class B extends A {
  int y;
  static int k = 4;
  static int j = 5;
  static int l = 6;

  B (int x, int y) { super(x+y); this.y = x+y; }

  B (int x) { this(x+l, x+l); this.y = x+l; }

  B () { this(k+j); this.y = l; }


}
class C extends A {
  int y;
  static int k = 4;
  static int j = 5;
  static int l = 6;

  C () { this(k+j); this.y = l; }

  C (int x) { this(x+l, x+l); this.y = x+l; }

  C (int x, int y) { super(x+y); this.y = x+y; }

}


aspect ConstructorAspects {
	
  public static int preinitcalls = 0;
  public static int initcalls = 0;
  public static int execs = 0;

  static private int aspectnesting = 0;
  private final static boolean log = false;

  static void message(String s) { 
  	if (log) {
  	  for (int i=0; i<aspectnesting; i++) System.out.print("---+");
	  System.out.println(s);
  	}
  }


  // call of all constructors
  pointcut allconstrcalls() :  call(*..new(..)) &&
		   !within(ConstructorAspects) && !call(java.lang..new(..));

  // execution of all constructors
  pointcut allconstrexecutions() : execution(*..new(..)) &&
		   !within(ConstructorAspects);
		   
  // intialization of all constructors
  pointcut allconstrinitializations() : initialization(*..new(..)) &&
		   !within(ConstructorAspects);

  // preinitialization of all constructors
  pointcut allconstrpreinitializations() : preinitialization(*..new(..)) &&
		  !within(ConstructorAspects);

  before(): !within(ConstructorAspects) && allconstrpreinitializations() {
  	preinitcalls++; 
  }
  // before advice
  before () : !within(ConstructorAspects) &&
			  (allconstrpreinitializations()  ||
			  allconstrinitializations() ||
			  allconstrexecutions() ) {
			  message(
				  "BEFORE: " +  thisJoinPointStaticPart.getSourceLocation() +
				  " " +thisJoinPointStaticPart.toLongString());
			  aspectnesting++;
			  }

  // after advice
  after () returning : !within(ConstructorAspects) &&
			  (allconstrpreinitializations() ||
			  allconstrinitializations() ||
			  allconstrexecutions() )
			  {
			  aspectnesting--;
			  message(
				  "AFTER: " +  thisJoinPointStaticPart.getSourceLocation() +
				  " " +thisJoinPointStaticPart.toLongString());
			  }

}