jdk

Форк
0
/
AccessMethodsLHS.java 
706 строк · 19.5 Кб
1
/*
2
 * Copyright (c) 1998, Oracle and/or its affiliates. All rights reserved.
3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
 *
5
 * This code is free software; you can redistribute it and/or modify it
6
 * under the terms of the GNU General Public License version 2 only, as
7
 * published by the Free Software Foundation.
8
 *
9
 * This code is distributed in the hope that it will be useful, but WITHOUT
10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12
 * version 2 for more details (a copy is included in the LICENSE file that
13
 * accompanied this code).
14
 *
15
 * You should have received a copy of the GNU General Public License version
16
 * 2 along with this work; if not, write to the Free Software Foundation,
17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
 *
19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
 * or visit www.oracle.com if you need additional information or have any
21
 * questions.
22
 */
23

24
/*
25
 * @test
26
 * @bug 4102566
27
 * @summary Test access methods for assignment targets.
28
 * @author William Maddox (maddox)
29
 *
30
 * @compile AccessMethodsLHS.java
31
 * @run main AccessMethodsLHS
32
 */
33

34
public class AccessMethodsLHS {
35

36
    static void fail(String msg) throws Exception {
37
        throw new Exception("FAIL: " + msg);
38
    }
39

40
    static int arg = 123456;
41

42
    private int i;
43
    private double d;
44

45
    private void m(int x) throws Exception {
46
        System.out.println("called AccessMethodsLHS.m");
47
        if (x != 123456)
48
            AccessMethodsLHS.fail("bad argument");
49
    }
50

51
    public static class Bar {
52
        private int i;
53
        private double d;
54
        private String s;
55

56
        private void m(int x) throws Exception {
57
            System.out.println("called AccessMethodsLHS.Bar.m");
58
            if (x != 123456)
59
                AccessMethodsLHS.fail("bad argument");
60
        }
61

62
        // Static members are permitted in a static inner class.
63

64
        static private int si;
65
        static private double sd;
66
        static private String ss;
67

68
        static private void sm(int x) throws Exception {
69
            System.out.println("called AccessMethodsLHS.Bar.sm");
70
            if (x != 123456)
71
                AccessMethodsLHS.fail("bad argument");
72
        }
73
    }
74

75
    public static class Baz {
76
        private int i;
77
        private double d;
78
        private String s;
79

80
        private void m(int x) throws Exception {
81
            System.out.println("called Baz.m");
82
            if (x != 123456)
83
                AccessMethodsLHS.fail("bad argument");
84
        }
85

86
        // Compiler rejects static members here correctly.
87

88
        // static private int si;
89
        // static private double sd;
90
        // static private String ss;
91
    }
92

93
    public class Quux {
94
        void DoIt () throws Exception {
95
            m(arg);
96
            i = 1;
97
            d = 1.0;
98
            i += 5;
99
            i--;
100
        }
101
        void DoMore(AccessMethodsLHS f) throws Exception {
102
            f.m(arg);
103
        }
104
    }
105

106
    public static class Quem {
107
        void DoIt () {
108
            // Illegal static refs to non-static vars
109
            // i = 1;
110
            // d = 1.0;
111
            // i += 5;
112
            // i--;
113
        }
114
    }
115

116
    static int effects = 0;
117

118
    static Bar iBar(Bar x) { effects++; return x; }
119

120
    static Baz iBaz(Baz x) { effects++; return x; }
121

122
    static void checkEffects(int i) throws Exception {
123
        if (effects != 1) {
124
            AccessMethodsLHS.fail("incorrect side-effect behavior (" + effects + "): " + i);
125
        }
126
        effects = 0;
127
    }
128

129
    static public void main(String args[]) throws Exception {
130

131
        Bar b = new Bar();
132
        Baz c = new Baz();
133

134
        System.out.println("testing assignment");
135

136
        AccessMethodsLHS f = new AccessMethodsLHS();
137

138
        Quux q1 = f.new Quux();
139
        q1.DoIt();
140
        q1.DoMore(f);
141

142
        Quem q2 = new Quem();
143
        q2.DoIt();
144

145
        // *** Static class, Non-static members ***
146

147
        b.m(arg);
148

149
        // Integer (1 word)
150

151
        b.i = 5;
152
        System.out.println(b.i);
153
        if (b.i != 5)
154
            AccessMethodsLHS.fail("simple assignment");
155
        System.out.println(b.i);
156

157

158
        if ((b.i += 10) != 15)
159
            AccessMethodsLHS.fail("add-assign result");
160
        System.out.println(b.i);
161

162
        if (b.i != 15)
163
            AccessMethodsLHS.fail("add-assign effect");
164
        System.out.println(b.i);
165

166
        b.s = "foo";
167
        if (!(b.s += "bar").equals("foobar"))
168
            AccessMethodsLHS.fail("concat-assign result");
169
        System.out.println(b.s);
170

171
        if (!b.s.equals("foobar"))
172
            AccessMethodsLHS.fail("concat-assign effect");
173
        System.out.println(b.s);
174

175
        b.s = "foo";
176
        b.s += "bar";
177
        if (!b.s.equals("foobar"))
178
            AccessMethodsLHS.fail("concat-assign effect (novalue)");
179
        System.out.println(b.s);
180

181
        b.i = 0;
182
        b.i++;
183
        if (b.i != 1)
184
            AccessMethodsLHS.fail("post-increment effect");
185
        System.out.println(b.i);
186

187
        b.i = 5;
188
        if (b.i++ != 5)
189
            AccessMethodsLHS.fail("post-increment result");
190
        if (b.i != 6)
191
            AccessMethodsLHS.fail("post-increment effect (embedded)");
192
        System.out.println(b.i);
193

194
        b.i = 1;
195
        --b.i;
196
        if (b.i != 0)
197
            AccessMethodsLHS.fail("pre-decrement effect");
198

199
        b.i = 5;
200
        if (--b.i != 4)
201
            AccessMethodsLHS.fail("pre-decrement result");
202
        if (b.i != 4)
203
            AccessMethodsLHS.fail("pre-decrement effect (embedded)");
204
        System.out.println(b.i);
205

206
        // Double (2 word)
207

208
        b.d = 5.0;
209
        System.out.println(b.d);
210
        if (b.d != 5.0)
211
            AccessMethodsLHS.fail("simple assignment");
212
        System.out.println(b.d);
213

214
        if ((b.d += 10) != 15.0)
215
            AccessMethodsLHS.fail("add-assign result");
216
        System.out.println(b.d);
217

218
        if (b.d != 15.0)
219
            AccessMethodsLHS.fail("add-assign effect");
220
        System.out.println(b.d);
221

222
        b.d = 0.0;
223
        b.d++;
224
        if (b.d != 1.0)
225
            AccessMethodsLHS.fail("post-increment effect");
226
        System.out.println(b.d);
227

228
        b.d = 5.0;
229
        if (b.d++ != 5.0)
230
            AccessMethodsLHS.fail("post-increment result");
231
        if (b.d != 6.0)
232
            AccessMethodsLHS.fail("post-increment effect (embedded)");
233
        System.out.println(b.d);
234

235
        b.d = 1.0;
236
        --b.d;
237
        if (b.d != 0.0)
238
            AccessMethodsLHS.fail("pre-decrement effect");
239

240
        b.d = 5.0;
241
        if (--b.d != 4.0)
242
            AccessMethodsLHS.fail("pre-decrement result");
243
        if (b.d != 4.0)
244
            AccessMethodsLHS.fail("pre-decrement effect (embedded)");
245
        System.out.println(b.d);
246

247
        // Integer (1 word) with side-effects in object reference
248

249
        iBar(b).i = 5;
250
        checkEffects(1);
251
        System.out.println(b.i);
252
        if (b.i != 5)
253
            AccessMethodsLHS.fail("simple assignment");
254

255
        System.out.println(b.i);
256

257
        if ((iBar(b).i += 10) != 15)
258
            AccessMethodsLHS.fail("add-assign result");
259
        checkEffects(2);
260
        System.out.println(b.i);
261

262
        if (b.i != 15)
263
            AccessMethodsLHS.fail("add-assign effect");
264
        System.out.println(b.i);
265

266
        b.i = 0;
267
        iBar(b).i++;
268
        checkEffects(3);
269
        if (b.i != 1)
270
            AccessMethodsLHS.fail("post-increment effect");
271
        System.out.println(b.i);
272

273
        b.i = 5;
274
        if (iBar(b).i++ != 5)
275
            AccessMethodsLHS.fail("post-increment result");
276
        checkEffects(4);
277
        if (b.i != 6)
278
            AccessMethodsLHS.fail("post-increment effect (embedded)");
279
        System.out.println(b.i);
280

281
        b.i = 1;
282
        --iBar(b).i;
283
        checkEffects(5);
284
        if (b.i != 0)
285
            AccessMethodsLHS.fail("pre-decrement effect");
286

287
        b.i = 5;
288
        if (--iBar(b).i != 4)
289
            AccessMethodsLHS.fail("pre-decrement result");
290
        checkEffects(6);
291
        if (b.i != 4)
292
            AccessMethodsLHS.fail("pre-decrement effect (embedded)");
293
        System.out.println(b.i);
294

295

296
        // *** Static class, Static members ***
297

298
        Bar.sm(arg);
299

300
        // Integer (1 word)
301

302
        Bar.si = 5;
303
        System.out.println(Bar.si);
304
        if (Bar.si != 5)
305
            AccessMethodsLHS.fail("simple assignment");
306
        System.out.println(Bar.si);
307

308
        if ((Bar.si += 10) != 15)
309
            AccessMethodsLHS.fail("add-assign result");
310
        System.out.println(Bar.si);
311

312
        if (Bar.si != 15)
313
            AccessMethodsLHS.fail("add-assign effect");
314
        System.out.println(Bar.si);
315

316
        Bar.ss = "foo";
317
        if (!(Bar.ss += "bar").equals("foobar"))
318
            AccessMethodsLHS.fail("concat-assign result");
319
        System.out.println(Bar.ss);
320

321
        if (!Bar.ss.equals("foobar"))
322
            AccessMethodsLHS.fail("concat-assign effect");
323
        System.out.println(Bar.ss);
324

325
        Bar.ss = "foo";
326
        Bar.ss += "bar";
327
        if (!Bar.ss.equals("foobar"))
328
            AccessMethodsLHS.fail("concat-assign effect (novalue)");
329
        System.out.println(Bar.ss);
330

331
        Bar.si = 0;
332
        Bar.si++;
333
        if (Bar.si != 1)
334
            AccessMethodsLHS.fail("post-increment effect");
335
        System.out.println(Bar.si);
336

337
        Bar.si = 5;
338
        if (Bar.si++ != 5)
339
            AccessMethodsLHS.fail("post-increment result");
340
        if (Bar.si != 6)
341
            AccessMethodsLHS.fail("post-increment effect (embedded)");
342
        System.out.println(Bar.si);
343

344
        Bar.si = 1;
345
        --Bar.si;
346
        if (Bar.si != 0)
347
            AccessMethodsLHS.fail("pre-decrement effect");
348

349
        Bar.si = 5;
350
        if (--Bar.si != 4)
351
            AccessMethodsLHS.fail("pre-decrement result");
352
        if (Bar.si != 4)
353
            AccessMethodsLHS.fail("pre-decrement effect (embedded)");
354
        System.out.println(Bar.si);
355

356
        // Double (2 word)
357

358
        Bar.sd = 5.0;
359
        System.out.println(Bar.sd);
360
        if (Bar.sd != 5.0)
361
            AccessMethodsLHS.fail("simple assignment");
362
        System.out.println(Bar.sd);
363

364
        if ((Bar.sd += 10) != 15.0)
365
            AccessMethodsLHS.fail("add-assign result");
366
        System.out.println(Bar.sd);
367

368
        if (Bar.sd != 15.0)
369
            AccessMethodsLHS.fail("add-assign effect");
370
        System.out.println(Bar.sd);
371

372
        Bar.sd = 0.0;
373
        Bar.sd++;
374
        if (Bar.sd != 1.0)
375
            AccessMethodsLHS.fail("post-increment effect");
376
        System.out.println(Bar.sd);
377

378
        Bar.sd = 5.0;
379
        if (Bar.sd++ != 5.0)
380
            AccessMethodsLHS.fail("post-increment result");
381
        if (Bar.sd != 6.0)
382
            AccessMethodsLHS.fail("post-increment effect (embedded)");
383
        System.out.println(Bar.sd);
384

385
        Bar.sd = 1.0;
386
        --Bar.sd;
387
        if (Bar.sd != 0.0)
388
            AccessMethodsLHS.fail("pre-decrement effect");
389

390
        Bar.sd = 5.0;
391
        if (--Bar.sd != 4.0)
392
            AccessMethodsLHS.fail("pre-decrement result");
393
        if (Bar.sd != 4.0)
394
            AccessMethodsLHS.fail("pre-decrement effect (embedded)");
395
        System.out.println(Bar.sd);
396

397

398
        // *** Static class, Static members (invoked via object reference) ***
399

400
        b.sm(arg);
401

402
        iBar(b).sm(arg);
403
        checkEffects(101);
404

405
        // Integer (1 word)
406

407
        b.si = 5;
408
        System.out.println(b.si);
409
        if (b.si != 5)
410
            AccessMethodsLHS.fail("simple assignment");
411
        System.out.println(b.si);
412

413
        if ((b.si += 10) != 15)
414
            AccessMethodsLHS.fail("add-assign result");
415
        System.out.println(b.si);
416

417
        if (b.si != 15)
418
            AccessMethodsLHS.fail("add-assign effect");
419
        System.out.println(b.si);
420

421
        b.ss = "foo";
422
        if (!(b.ss += "bar").equals("foobar"))
423
            AccessMethodsLHS.fail("concat-assign result");
424
        System.out.println(b.ss);
425

426
        if (!b.ss.equals("foobar"))
427
            AccessMethodsLHS.fail("concat-assign effect");
428
        System.out.println(b.ss);
429

430
        b.ss = "foo";
431
        b.ss += "bar";
432
        if (!b.ss.equals("foobar"))
433
            AccessMethodsLHS.fail("concat-assign effect (novalue)");
434
        System.out.println(b.ss);
435

436
        b.si = 0;
437
        b.si++;
438
        if (b.si != 1)
439
            AccessMethodsLHS.fail("post-increment effect");
440
        System.out.println(b.si);
441

442
        b.si = 5;
443
        if (b.si++ != 5)
444
            AccessMethodsLHS.fail("post-increment result");
445
        if (b.si != 6)
446
            AccessMethodsLHS.fail("post-increment effect (embedded)");
447
        System.out.println(b.si);
448

449
        b.si = 1;
450
        --b.si;
451
        if (b.si != 0)
452
            AccessMethodsLHS.fail("pre-decrement effect");
453

454
        b.si = 5;
455
        if (--b.si != 4)
456
            AccessMethodsLHS.fail("pre-decrement result");
457
        if (b.si != 4)
458
            AccessMethodsLHS.fail("pre-decrement effect (embedded)");
459
        System.out.println(b.si);
460

461
        // Double (2 word)
462

463
        b.sd = 5.0;
464
        System.out.println(b.sd);
465
        if (b.sd != 5.0)
466
            AccessMethodsLHS.fail("simple assignment");
467
        System.out.println(b.sd);
468

469
        if ((b.sd += 10) != 15.0)
470
            AccessMethodsLHS.fail("add-assign result");
471
        System.out.println(b.sd);
472

473
        if (b.sd != 15.0)
474
            AccessMethodsLHS.fail("add-assign effect");
475
        System.out.println(b.sd);
476

477
        b.sd = 0.0;
478
        b.sd++;
479
        if (b.sd != 1.0)
480
            AccessMethodsLHS.fail("post-increment effect");
481
        System.out.println(b.sd);
482

483
        b.sd = 5.0;
484
        if (b.sd++ != 5.0)
485
            AccessMethodsLHS.fail("post-increment result");
486
        if (b.sd != 6.0)
487
            AccessMethodsLHS.fail("post-increment effect (embedded)");
488
        System.out.println(b.sd);
489

490
        b.sd = 1.0;
491
        --b.sd;
492
        if (b.sd != 0.0)
493
            AccessMethodsLHS.fail("pre-decrement effect");
494

495
        b.sd = 5.0;
496
        if (--b.sd != 4.0)
497
            AccessMethodsLHS.fail("pre-decrement result");
498
        if (b.sd != 4.0)
499
            AccessMethodsLHS.fail("pre-decrement effect (embedded)");
500
        System.out.println(b.sd);
501

502
        // Integer (1 word) with side-effects in object reference
503

504
        iBar(b).si = 5;
505
        checkEffects(7);
506
        System.out.println(b.si);
507
        if (b.si != 5)
508
            AccessMethodsLHS.fail("simple assignment");
509
        System.out.println(b.si);
510

511
        if ((iBar(b).si += 10) != 15)
512
            AccessMethodsLHS.fail("add-assign result");
513
        checkEffects(8);
514
        System.out.println(b.si);
515

516
        if (b.si != 15)
517
            AccessMethodsLHS.fail("add-assign effect");
518
        System.out.println(b.si);
519

520
        iBar(b).si = 0;
521
        checkEffects(9);
522
        iBar(b).si++;
523
        checkEffects(10);
524
        if (b.si != 1)
525
            AccessMethodsLHS.fail("post-increment effect");
526
        System.out.println(b.si);
527

528
        b.si = 5;
529
        if (iBar(b).si++ != 5)
530
            AccessMethodsLHS.fail("post-increment result");
531
        checkEffects(11);
532
        if (b.si != 6)
533
            AccessMethodsLHS.fail("post-increment effect (embedded)");
534
        System.out.println(b.si);
535

536
        b.si = 1;
537
        --iBar(b).si;
538
        checkEffects(12);
539
        if (b.si != 0)
540
            AccessMethodsLHS.fail("pre-decrement effect");
541

542
        b.si = 5;
543
        if (--iBar(b).si != 4)
544
            AccessMethodsLHS.fail("pre-decrement result");
545
        checkEffects(13);
546
        if (b.si != 4)
547
            AccessMethodsLHS.fail("pre-decrement effect (embedded)");
548
        System.out.println(b.si);
549

550

551
        // *** Non-static class, Non-static members ***
552

553
        c.m(arg);
554

555
        iBaz(c).m(arg);
556
        checkEffects(102);
557

558
        // Integer (1 word)
559

560
        c.i = 5;
561
        System.out.println(c.i);
562
        if (c.i != 5)
563
            AccessMethodsLHS.fail("simple assignment");
564
        System.out.println(c.i);
565

566
        if ((c.i += 10) != 15)
567
            AccessMethodsLHS.fail("add-assign result");
568
        System.out.println(c.i);
569

570
        if (c.i != 15)
571
            AccessMethodsLHS.fail("add-assign effect");
572
        System.out.println(c.i);
573

574
        c.s = "foo";
575
        if (!(c.s += "bar").equals("foobar"))
576
            AccessMethodsLHS.fail("concat-assign result");
577
        System.out.println(c.s);
578

579
        if (!c.s.equals("foobar"))
580
            AccessMethodsLHS.fail("concat-assign effect");
581
        System.out.println(c.s);
582

583
        c.s = "foo";
584
        c.s += "bar";
585
        if (!c.s.equals("foobar"))
586
            AccessMethodsLHS.fail("concat-assign effect (novalue)");
587
        System.out.println(c.s);
588

589
        c.i = 0;
590
        c.i++;
591
        if (c.i != 1)
592
            AccessMethodsLHS.fail("post-increment effect");
593
        System.out.println(c.i);
594

595
        c.i = 5;
596
        if (c.i++ != 5)
597
            AccessMethodsLHS.fail("post-increment result");
598
        if (c.i != 6)
599
            AccessMethodsLHS.fail("post-increment effect (embedded)");
600
        System.out.println(c.i);
601

602
        c.i = 1;
603
        --c.i;
604
        if (c.i != 0)
605
            AccessMethodsLHS.fail("pre-decrement effect");
606

607
        c.i = 5;
608
        if (--c.i != 4)
609
            AccessMethodsLHS.fail("pre-decrement result");
610
        if (c.i != 4)
611
            AccessMethodsLHS.fail("pre-decrement effect (embedded)");
612
        System.out.println(c.i);
613

614
        // Double (2 word)
615

616
        c.d = 5.0;
617
        System.out.println(c.d);
618
        if (c.d != 5.0)
619
            AccessMethodsLHS.fail("simple assignment");
620
        System.out.println(c.d);
621

622
        if ((c.d += 10) != 15.0)
623
            AccessMethodsLHS.fail("add-assign result");
624
        System.out.println(c.d);
625

626
        if (c.d != 15.0)
627
            AccessMethodsLHS.fail("add-assign effect");
628
        System.out.println(c.d);
629

630
        c.d = 0.0;
631
        c.d++;
632
        if (c.d != 1.0)
633
            AccessMethodsLHS.fail("post-increment effect");
634
        System.out.println(c.d);
635

636
        c.d = 5.0;
637
        if (c.d++ != 5.0)
638
            AccessMethodsLHS.fail("post-increment result");
639
        if (c.d != 6.0)
640
            AccessMethodsLHS.fail("post-increment effect (embedded)");
641
        System.out.println(c.d);
642

643
        c.d = 1.0;
644
        --c.d;
645
        if (c.d != 0.0)
646
            AccessMethodsLHS.fail("pre-decrement effect");
647

648
        c.d = 5.0;
649
        if (--c.d != 4.0)
650
            AccessMethodsLHS.fail("pre-decrement result");
651
        if (c.d != 4.0)
652
            AccessMethodsLHS.fail("pre-decrement effect (embedded)");
653
        System.out.println(c.d);
654

655
        // Double (2 word) with side-effects in object reference
656

657
        iBaz(c).d = 5.0;
658
        checkEffects(14);
659
        System.out.println(c.d);
660
        if (c.d != 5.0)
661
            AccessMethodsLHS.fail("simple assignment");
662
        System.out.println(c.d);
663

664
        if ((iBaz(c).d += 10) != 15.0)
665
            AccessMethodsLHS.fail("add-assign result");
666
        checkEffects(15);
667
        System.out.println(c.d);
668

669
        if (c.d != 15.0)
670
            AccessMethodsLHS.fail("add-assign effect");
671
        System.out.println(c.d);
672

673
        c.d = 0.0;
674
        iBaz(c).d++;
675
        checkEffects(16);
676
        if (c.d != 1.0)
677
            AccessMethodsLHS.fail("post-increment effect");
678
        System.out.println(c.d);
679

680
        c.d = 5.0;
681
        if (iBaz(c).d++ != 5.0)
682
            AccessMethodsLHS.fail("post-increment result");
683
        checkEffects(17);
684
        if (c.d != 6.0)
685
            AccessMethodsLHS.fail("post-increment effect (embedded)");
686
        System.out.println(c.d);
687

688
        c.d = 1.0;
689
        --iBaz(c).d;
690
        checkEffects(18);
691
        if (c.d != 0.0)
692
            AccessMethodsLHS.fail("pre-decrement effect");
693

694
        c.d = 5.0;
695
        if (--iBaz(c).d != 4.0)
696
            AccessMethodsLHS.fail("pre-decrement result");
697
        checkEffects(19);
698
        if (c.d != 4.0)
699
            AccessMethodsLHS.fail("pre-decrement effect (embedded)");
700
        System.out.println(c.d);
701

702
        // All done.
703

704
        System.out.println("done");
705
    }
706
}
707

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.