blob: 107a4d974fdf3e67be35f995cd1e96362e232544 (
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
|
import org.aspectj.testing.Tester;
/**
* PR#476:
* Member initializations are run after explicit
* constructor calls ("this()") when they should be run beforehand.
* The following program would produce a NullPointerException because
* 'member' is set to null *after* the explicit constructor sets it
* to "correctValue".
*/
public class MemberInitializationsAfterExplicitConstructorCalls {
public static void main(String[] args) {
// passes - no constructor call to this
ThisCall thisCall = new ThisCall("foo");
thisCall.go();
// fails - constructor call to this
thisCall = new ThisCall();
thisCall.go();
}
static class ThisCall {
String init = "INIT";
String member = null;
/** set init and member to input */
ThisCall(String input) {
this.init = input;
this.member = input;
}
ThisCall() {
this("correctValue");
Tester.check(!"INIT".equals(init), "String constructor: !\"INIT\".equals(init)");
Tester.check(null != member, "String constructor: null != member");
// get NPE here if using member
}
void go() {
Tester.check(!"INIT".equals(init), "instance method: !\"INIT\".equals(init)");
Tester.check(null != member, "instance method: null != member");
// get NPE here if using member
}
}
}
|