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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
|
// todo: need header
import org.aspectj.testing.Tester;
/**
* "Coverage" tests for
* PR#476:
* Member initializations are run after explicit
* constructor calls ("this()" or "super()") when they should be run beforehand.
* <p>Status:
* 10 additional variants are defined, 5 of which fail, emitting 30 errors.
* <p>background:
* <br>The effective order of operation during initialization should be:
* <ol>
* <ol>superclass static initialization</ol>
* <ol>selfclass static initialization</ol>
* <ol>superclass member initialization</ol>
* <ol>superclass constructor</ol>
* <ol>selfclass member initialization</ol>
* <ol>selfclass constructor</ol>
* </ol>
* Other relevant rules:
* <li>this() or super() if present must be the first statement in a constructor</li>
* <li>Cannot use this (and hence this.member) in either this() or super()
* (checked by javac, not ajc) </li>
* <li>Cannot refer to parent instance members in either this() or super()
* (checked by javac, not ajc) </li>
* <li>an enclosing instance is accessible only in the body of an instance
* method, constructor (after the explicit constructor invocation, if any),
* initializer block, or in the initializer expression of an instance variable.</li>
* <p>fault model:
* the compiler is inserting member initialization after the explicit
* constructor call in the intermediate code. I.e., it produces:
* <pre>ThisCall() {
* this("correctValue");
* {
* this.initString = "INIT";
* this.initNull = null;
* }</pre>
* when it should produce:
* <pre>ThisCall() {
* this("correctValue");</pre>
*
* <p>fix model:
* Since member initialization must occur before this() call,
* and this() must be first in the constructor,
* I see no way to implement before advice on member initializers
* using preprocessing to produce source code except to put them only
* (and always) in the constructors without this() calls.
*
* <p>Variants tested in this coverage extension of the original test case:
* <li>{type}[Object, String, Primitive]: Different member types</li>
* <li>location[top, bottom, mixed]: location of the member initializer in the class declaration -
* before constructor, after constructor</li>
* <li>initializer[simpleExpression, blockExpression, none]:
* type of member initialization
* (<code>Member m = x;<code> or <code>Member m; { m = x; }<code>)
* with location variants. </li>
* <li>initializerLocus[this (default), super, enclosing ]:
* fields being initialized - this instance, superclass, enclosing class
* <li>{enclosingClass}[none, Outer, ]: Different member types</li>
*
* <p>Variants not (yet?) tested:
* <li>static variants</li>
* <li>{super}[{default},Child]: Calling <code>super()</code> rather than <code>this()</code> </li>
*
* <p>Untestable variants:
* <li>Illegal to use this or super member values in explicit constructor call parameter
* evaluation: <code>super("result: " + member)</code>
* or <code>this("result: " + member)</code>.
* or <code>this("result: " + super.member)</code>.
*
* $Id: MemberInitializationsAfterExplicitConstructorCallsCoverage.java,v 1.2 2001/08/03 22:38:49 isberg Exp $
*/
public class MemberInitializationsAfterExplicitConstructorCallsCoverage {
public static final String INPUT = "input";
public static final String INIT = "INIT";
public static void main(String[] args) {
test();
}
public static void test() {
boolean doPassingTests = true;
boolean doFailingTests = true;
//--------- proof that test code is correct
{ ThisCallTopSimple thisCall = new ThisCallTopSimple(1); thisCall.go(); }
//--------- passing test cases
//--- this duplicates original test case
// ThisCall thisCall;
// no constructor call to this
// thisCall = new ThisCall(INPUT);
// thisCall.go();
//--- new coverage tests - 5 tests, 6 errors each, 30 errors
if (doPassingTests) {
{ ThisCallTopSimple thisCall = new ThisCallTopSimple(INPUT); thisCall.go(); }
{ ThisCallTopBlock thisCall = new ThisCallTopBlock(INPUT); thisCall.go(); }
{ ThisCallBottomSimple thisCall = new ThisCallBottomSimple(INPUT); thisCall.go(); }
{ ThisCallBottomBlock thisCall = new ThisCallBottomBlock(INPUT); thisCall.go(); }
{ ThisCallMixed thisCall = new ThisCallMixed(INPUT); thisCall.go(); }
// all super cases pass
{ ThisCallChild thisCall = new ThisCallChild(); thisCall.go(); }
{ ThisCallChild thisCall = new ThisCallChild(2); thisCall.go(); }
{ ThisCallChild thisCall = new ThisCallChild(INPUT); thisCall.go(); }
// enclosed inner class initializer can access enclosing members
{ ThisCallEnclosing.ThisCallEnclosed thisCall
= (new ThisCallEnclosing("ignored")).new ThisCallEnclosed(); }
}
// { ThisCallChild thisCall = new ThisCallChild(); thisCall.go(); }
//--------- failing test cases
//--- duplicate original test case
// fails - constructor call to this
//thisCall = new ThisCall();
//thisCall.go();
//--- new coverage tests
if (doFailingTests) {
{ ThisCallTopSimple thisCall = new ThisCallTopSimple(); thisCall.go(); }
{ ThisCallTopBlock thisCall = new ThisCallTopBlock(); thisCall.go(); }
{ ThisCallBottomSimple thisCall = new ThisCallBottomSimple(); thisCall.go(); }
{ ThisCallBottomBlock thisCall = new ThisCallBottomBlock(); thisCall.go(); }
{ ThisCallMixed thisCall = new ThisCallMixed(); thisCall.go(); }
}
//--------- impossible test cases
//---- unable to test superclass initialization before instance
// { ThisCallChild thisCall = new ThisCallChild((long)1l); thisCall.go(); }
}
/** variant: location top, initializer simpleExpression */
static class ThisCallTopSimple {
/** type primitive, location top, initializer simpleExpression */
int initOne = 1;
/** type String, location top, initializer simpleExpression */
String initString = "INIT";
/** type Object, location top, initializer simpleExpression */
Object initNull = null;
/** type String, location top, initializer none */
String initNone;
/** no bug when calling this directly */
ThisCallTopSimple (String input) {
checkMembersHaveInitializedValues("constructor ThisCallTopSimple(\" + input + \")");
setValues(input);
checkMembersHaveSetValues("constructor ThisCallTopSimple.ThisCallTopSimple(\" + input + \")");
}
void setValues(String input) {
this.initString = input;
this.initNull = input;
this.initNone = input;
this.initOne = 2;
}
/** proof that test code is correct */
ThisCallTopSimple (int ignored) {
checkMembersHaveInitializedValues("constructor ThisCallTopSimple.ThisCallTopSimple(int)");
setValues(INPUT);
checkMembersHaveSetValues("constructor ThisCallTopSimple.ThisCallTopSimple(int)");
}
/** bug when calling this which calls ThisCall(String) */
ThisCallTopSimple () {
this(INPUT);
checkMembersHaveSetValues("constructor ThisCallTopSimple.ThisCallTopSimple()");
}
/** redundant check - same check at end of constructors */
void go() {
checkMembersHaveSetValues("method ThisCallTopSimple.go()");
}
/** the same method for all variants */
protected void checkMembersHaveInitializedValues(String label) {
Tester.checkEqual("INIT", initString, label + " initialized ");
Tester.checkEqual((Object) null, initNull, label + " initialized ");
Tester.checkEqual((Object) null, initNone, label + " initialized ");
Tester.checkEqual(1, initOne, label + " initialized ");
}
/** the same method for all variants */
protected void checkMembersHaveSetValues(String label) {
Tester.checkEqual(2, initOne, label + " set ");
Tester.checkEqual("input", initString, label + " set ");
Tester.checkEqual("input", initNone, label + " set ");
// Object uses strict/reference identity - input dependency
Tester.checkEqual(INPUT, initNull, label + " set ");
}
} // ThisCallTopSimple
/** variant: location top, initializer blockExpression */
static class ThisCallTopBlock {
/** top declarations */
/** type primitive, location top, initializer blockExpression */
int initOne;
/** type String, location top, initializer blockExpression */
String initString;
/** type Object, location top, initializer blockExpression */
Object initNull;
/** type String, location top, initializer none */
String initNone;
/** top initializer block */
{
initOne = 1;
initString = "INIT";
initNull = null;
}
/** no bug when calling this directly */
ThisCallTopBlock (String input) {
checkMembersHaveInitializedValues("constructor ThisCallTopBlock(\" + input + \")");
this.initString = input;
this.initNull = input;
this.initNone = input;
this.initOne = 2;
checkMembersHaveSetValues("constructor ThisCallTopSimple.ThisCall(\" + input + \")");
}
/** bug when calling this which calls ThisCallTopBlock(String) */
ThisCallTopBlock () {
this(INPUT);
checkMembersHaveSetValues("constructor ThisCallTopSimple.ThisCallTopBlock()");
}
/** redundant check - same check at end of constructors */
void go() {
checkMembersHaveSetValues("method ThisCallTopBlock.go()");
}
/** the same method for all variants */
protected void checkMembersHaveInitializedValues(String label) {
Tester.checkEqual("INIT", initString, label + " initialized ");
Tester.checkEqual((Object) null, initNull, label + " initialized ");
Tester.checkEqual((Object) null, initNone, label + " initialized ");
Tester.checkEqual(1, initOne, label + " initialized ");
}
/** the same method for all variants */
protected void checkMembersHaveSetValues(String label) {
Tester.checkEqual(2, initOne, label + " set ");
Tester.checkEqual("input", initString, label + " set ");
Tester.checkEqual("input", initNone, label + " set ");
// Object uses strict/reference identity - input dependency
Tester.checkEqual(INPUT, initNull, label + " set ");
}
} // ThisCallTopBlock
/** variant: location bottom, initializer simpleExpression */
static class ThisCallBottomSimple {
/** no bug when calling this directly */
ThisCallBottomSimple (String input) {
checkMembersHaveInitializedValues("constructor ThisCallBottomSimple(\" + input + \")");
this.initString = input;
this.initNull = input;
this.initNone = input;
this.initOne = 2;
checkMembersHaveSetValues("constructor ThisCallBottomSimple.ThisCallBottomSimple(\" + input + \")");
}
/** bug when calling this which calls ThisCallBottomSimple(String) */
ThisCallBottomSimple () {
this(INPUT);
checkMembersHaveSetValues("constructor ThisCallBottomSimple.ThisCallBottomSimple()");
}
/** redundant check - same check at end of constructors */
void go() {
checkMembersHaveSetValues("method ThisCallBottomSimple.go()");
}
/** the same method for all variants */
protected void checkMembersHaveInitializedValues(String label) {
Tester.checkEqual("INIT", initString, label + " initialized ");
Tester.checkEqual((Object) null, initNull, label + " initialized ");
Tester.checkEqual((Object) null, initNone, label + " initialized ");
Tester.checkEqual(1, initOne, label + " initialized ");
}
/** the same method for all variants */
protected void checkMembersHaveSetValues(String label) {
Tester.checkEqual(2, initOne, label + " set ");
Tester.checkEqual("input", initString, label + " set ");
Tester.checkEqual("input", initNone, label + " set ");
// Object uses strict/reference identity - input dependency
Tester.checkEqual(INPUT, initNull, label + " set ");
}
/** type primitive, location bottom, initializer simpleExpression */
int initOne = 1;
/** type String, location bottom, initializer simpleExpression */
String initString = "INIT";
/** type Object, location bottom, initializer simpleExpression */
Object initNull = null;
/** type String, location bottom, initializer none */
String initNone;
} // ThisCallBottomSimple
/** variant: location bottom, initializer blockExpression */
static class ThisCallBottomBlock {
/** no bug when calling this directly */
ThisCallBottomBlock (String input) {
checkMembersHaveInitializedValues("constructor ThisCallBottomBlock(\" + input + \")");
this.initString = input;
this.initNull = input;
this.initNone = input;
this.initOne = 2;
checkMembersHaveSetValues("constructor ThisCallBottomBlock.ThisCallBottomBlock(\" + input + \")");
}
/** bug when calling this which calls ThisCallBottomBlock(String) */
ThisCallBottomBlock () {
this(INPUT);
checkMembersHaveSetValues("constructor ThisCallBottemBlock.ThisCallBottomBlock()");
}
/** redundant check - same check at end of constructors */
void go() {
checkMembersHaveSetValues("method ThisCallBottomBlock.go()");
}
/** the same method for all variants */
protected void checkMembersHaveInitializedValues(String label) {
Tester.checkEqual("INIT", initString, label + " initialized ");
Tester.checkEqual((Object) null, initNull, label + " initialized ");
Tester.checkEqual((Object) null, initNone, label + " initialized ");
Tester.checkEqual(1, initOne, label + " initialized ");
}
/** the same method for all variants */
protected void checkMembersHaveSetValues(String label) {
Tester.checkEqual(2, initOne, label + " set ");
Tester.checkEqual("input", initString, label + " set ");
Tester.checkEqual("input", initNone, label + " set ");
// Object uses strict/reference identity - input dependency
Tester.checkEqual(INPUT, initNull, label + " set ");
}
/** bottom declarations */
/** type primitive, location bottom, initializer blockExpression */
int initOne;
/** type String, location bottom, initializer blockExpression */
String initString;
/** type Object, location bottom, initializer blockExpression */
Object initNull;
/** type String, location bottom, initializer none */
String initNone;
/** bottom initializer block */
{
initOne = 1;
initString = "INIT";
initNull = null;
}
} // ThisCallBottomBlock
/** variant: location mixed, initializer mixed */
static class ThisCallMixed {
/** type primitive, location top, initializer simpleExpression */
int initOne = 1;
/** type String, location top, initializer simpleExpression */
String initString;
/** no bug when calling this directly */
ThisCallMixed (String input) {
checkMembersHaveInitializedValues("constructor ThisCallMixed(\" + input + \")");
this.initString = input;
this.initNull = input;
this.initNone = input;
this.initOne = 2;
checkMembersHaveSetValues("constructor ThisCallMixed.ThisCallMixed(\" + input + \")");
}
/** bug when calling this which calls ThisCallMixed(String) */
ThisCallMixed () {
this(INPUT);
checkMembersHaveSetValues("constructor ThisCallMixed.ThisCallMixed()");
}
/** redundant check - same check at end of constructors */
void go() {
checkMembersHaveSetValues("method ThisCallMixed.go()");
}
/** the same method for all variants */
protected void checkMembersHaveInitializedValues(String label) {
Tester.checkEqual("INIT", initString, label + " initialized ");
Tester.checkEqual((Object) null, initNull, label + " initialized ");
Tester.checkEqual((Object) null, initNone, label + " initialized ");
Tester.checkEqual(1, initOne, label + " initialized ");
}
/** the same method for all variants */
protected void checkMembersHaveSetValues(String label) {
Tester.checkEqual(2, initOne, label + " set ");
Tester.checkEqual("input", initString, label + " set ");
Tester.checkEqual("input", initNone, label + " set ");
// Object uses strict/reference identity - input dependency
Tester.checkEqual(INPUT, initNull, label + " set ");
}
/** bottom declarations */
/** type String, location bottom, initializer none */
String initNone;
/** type Object, location bottom, initializer blockExpression */
Object initNull;
/** bottom (partial) initializer block */
{
initString = "INIT";
initNull = null;
}
} // ThisCallMixed
static class ThisCallChild extends ThisCallParent {
/** type primitive, location top, initializer simpleExpression */
int initOne = 1;
/** type String, location top, initializer simpleExpression */
String initString = "INIT";
/** type Object, location top, initializer simpleExpression */
Object initNull = null;
/** type String, location top, initializer none */
String initNone;
/** no bug when calling this directly */
ThisCallChild (String input) {
checkMembersHaveInitializedValues("constructor ThisCallChild(\" + input + \")");
setValues(input);
checkMembersHaveSetValues("constructor ThisCallChild.ThisCallChild((\" + input + \")");;
Tester.checkEqual(parentObject, INPUT, "ThisCallChild.ThisCallChild(int ignored)");
}
void setValues(String input) {
this.initString = input;
this.initNull = input;
this.initNone = input;
this.initOne = 2;
}
/**
* @param correctResult
* @param actual
* @param expected
* @param failedResult
* @param testerMessage the String to use for Tester on failure -
* Tester unused if null
* @return correctResult if expected.equals(actual), failedResult otherwise
*/
static private String checkObject(String correctResult
, Object expected
, Object actual
, String failedResult
, String testerMessage) {
if (null == expected) {
if (null == actual) {
return correctResult;
} // else failures fall through
} else if ((null != actual) && (expected.equals(actual))) {
return correctResult;
}
// failures
if (null != testerMessage) {
Tester.checkEqual(actual, expected, testerMessage);
}
return failedResult;
}
/** proof that test code is correct */
ThisCallChild (int ignored) {
checkMembersHaveInitializedValues("constructor ThisCallChild.ThisCallChild(int)");
setValues(INPUT);
checkMembersHaveSetValues("constructor ThisCallChild.ThisCallChild(int)");
Tester.checkEqual(parentObject, INPUT, "ThisCallChild.ThisCallChild(int ignored)");
}
/** no bug when calling this which calls ThisCall(String) */
ThisCallChild () {
super(INPUT);
checkMembersHaveInitializedValues("constructor ThisCallChild.ThisCallChild()");
setValues(INPUT);
checkMembersHaveSetValues("constructor ThisCallChild.ThisCallChild()");
Tester.checkEqual(parentObject, INPUT, "ThisCallChild.ThisCallChild()");
}
private static final String tccsuperlabel =
"ThisCallChild.ThisCallChild(long)/* parent initialization complete before child */";
/** unable to access superclass member state before explicitly invoking constructor */
ThisCallChild (long ignored) {
// this would do the check, but it is illegal
// this(checkObject(INPUT, INPUT, parentObject, tccsuperLabel + "_FAILED", tccsuperlabel));
// this(checkObject(INPUT, INPUT, this$.getParentObject(), tccsuperlabel + "_FAILED", tccsuperlabel));
checkMembersHaveInitializedValues("constructor ThisCallChild.ThisCallChild()");
setValues(INPUT);
checkMembersHaveSetValues("constructor ThisCallChild.ThisCallChild()");
Tester.checkEqual(parentObject, INPUT, "ThisCallChild.ThisCallChild()");
}
/** redundant check - same check at end of constructors */
void go() {
checkMembersHaveSetValues("method ThisCallChild.go()");
Tester.checkEqual(parentObject, INPUT, "ThisCallChild.go()");
}
/** the same method for all variants */
protected void checkMembersHaveInitializedValues(String label) {
Tester.checkEqual("INIT", initString, label + " initialized ");
Tester.checkEqual((Object) null, initNull, label + " initialized ");
Tester.checkEqual((Object) null, initNone, label + " initialized ");
Tester.checkEqual(1, initOne, label + " initialized ");
}
/** the same method for all variants */
protected void checkMembersHaveSetValues(String label) {
Tester.checkEqual(2, initOne, label + " set ");
Tester.checkEqual("input", initString, label + " set ");
Tester.checkEqual("input", initNone, label + " set ");
// Object uses strict/reference identity - input dependency
Tester.checkEqual(INPUT, initNull, label + " set ");
}
}
static class ThisCallParent {
/** not available to in child explicit constructor parameter expression */
protected Object parentObject = INIT;
/** not available to in child explicit constructor parameter expression */
protected Object getParentObject() { return parentObject; }
/** no bug here */
ThisCallParent() {
Tester.checkEqual(parentObject, INIT, "ThisCallParent.ThisCallParent()");
parentObject = INPUT;
}
/** no bug here */
ThisCallParent(String input) {
Tester.checkEqual(parentObject, INIT, "ThisCallParent.ThisCallParent(\"" + input + "\")");
parentObject = input;
}
}
} // MemberInitializationsAfterExplicitConstructorCallsCoverage
/** variant: location enclosing */
class ThisCallEnclosing {
public static final String INPUT = "input";
public static final String INIT = "INIT";
String initString = INIT;
String constructedString;
public ThisCallEnclosed getEnclosed() {
return new ThisCallEnclosed();
}
/** no bug when calling this directly */
ThisCallEnclosing (String ignored) {
constructedString = INPUT;
initString = INPUT;
}
public class ThisCallEnclosed {
boolean didCheck;
{
// check enclosing instance in initializer
Tester.checkEqual(INPUT, initString, "ThisCallEnclosed.<initializer> initString");
Tester.checkEqual(INPUT, constructedString, "ThisCallEnclosed.<initializer> constructedString");
didCheck = true;
}
public ThisCallEnclosed() {
this("init: " + initString + " constructed: " + constructedString);
Tester.check(didCheck, "initializer ran before ThisCallEnclosed() body");
}
public ThisCallEnclosed(String s) {
Tester.checkEqual(INPUT, initString, "ThisCallEnclosed(String) initString");
Tester.checkEqual(INPUT, constructedString, "ThisCallEnclosed(String) constructedString");
Tester.check(didCheck, "initializer ran before ThisCallEnclosed(String) body");
}
}
} // ThisCallEnclosing
|