2
* Copyright (c) 1998, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.
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
27
* @summary Test access methods for assignment targets.
28
* @author William Maddox (maddox)
30
* @compile AccessMethodsLHS.java
31
* @run main AccessMethodsLHS
34
public class AccessMethodsLHS {
36
static void fail(String msg) throws Exception {
37
throw new Exception("FAIL: " + msg);
40
static int arg = 123456;
45
private void m(int x) throws Exception {
46
System.out.println("called AccessMethodsLHS.m");
48
AccessMethodsLHS.fail("bad argument");
51
public static class Bar {
56
private void m(int x) throws Exception {
57
System.out.println("called AccessMethodsLHS.Bar.m");
59
AccessMethodsLHS.fail("bad argument");
62
// Static members are permitted in a static inner class.
64
static private int si;
65
static private double sd;
66
static private String ss;
68
static private void sm(int x) throws Exception {
69
System.out.println("called AccessMethodsLHS.Bar.sm");
71
AccessMethodsLHS.fail("bad argument");
75
public static class Baz {
80
private void m(int x) throws Exception {
81
System.out.println("called Baz.m");
83
AccessMethodsLHS.fail("bad argument");
86
// Compiler rejects static members here correctly.
88
// static private int si;
89
// static private double sd;
90
// static private String ss;
94
void DoIt () throws Exception {
101
void DoMore(AccessMethodsLHS f) throws Exception {
106
public static class Quem {
108
// Illegal static refs to non-static vars
116
static int effects = 0;
118
static Bar iBar(Bar x) { effects++; return x; }
120
static Baz iBaz(Baz x) { effects++; return x; }
122
static void checkEffects(int i) throws Exception {
124
AccessMethodsLHS.fail("incorrect side-effect behavior (" + effects + "): " + i);
129
static public void main(String args[]) throws Exception {
134
System.out.println("testing assignment");
136
AccessMethodsLHS f = new AccessMethodsLHS();
138
Quux q1 = f.new Quux();
142
Quem q2 = new Quem();
145
// *** Static class, Non-static members ***
152
System.out.println(b.i);
154
AccessMethodsLHS.fail("simple assignment");
155
System.out.println(b.i);
158
if ((b.i += 10) != 15)
159
AccessMethodsLHS.fail("add-assign result");
160
System.out.println(b.i);
163
AccessMethodsLHS.fail("add-assign effect");
164
System.out.println(b.i);
167
if (!(b.s += "bar").equals("foobar"))
168
AccessMethodsLHS.fail("concat-assign result");
169
System.out.println(b.s);
171
if (!b.s.equals("foobar"))
172
AccessMethodsLHS.fail("concat-assign effect");
173
System.out.println(b.s);
177
if (!b.s.equals("foobar"))
178
AccessMethodsLHS.fail("concat-assign effect (novalue)");
179
System.out.println(b.s);
184
AccessMethodsLHS.fail("post-increment effect");
185
System.out.println(b.i);
189
AccessMethodsLHS.fail("post-increment result");
191
AccessMethodsLHS.fail("post-increment effect (embedded)");
192
System.out.println(b.i);
197
AccessMethodsLHS.fail("pre-decrement effect");
201
AccessMethodsLHS.fail("pre-decrement result");
203
AccessMethodsLHS.fail("pre-decrement effect (embedded)");
204
System.out.println(b.i);
209
System.out.println(b.d);
211
AccessMethodsLHS.fail("simple assignment");
212
System.out.println(b.d);
214
if ((b.d += 10) != 15.0)
215
AccessMethodsLHS.fail("add-assign result");
216
System.out.println(b.d);
219
AccessMethodsLHS.fail("add-assign effect");
220
System.out.println(b.d);
225
AccessMethodsLHS.fail("post-increment effect");
226
System.out.println(b.d);
230
AccessMethodsLHS.fail("post-increment result");
232
AccessMethodsLHS.fail("post-increment effect (embedded)");
233
System.out.println(b.d);
238
AccessMethodsLHS.fail("pre-decrement effect");
242
AccessMethodsLHS.fail("pre-decrement result");
244
AccessMethodsLHS.fail("pre-decrement effect (embedded)");
245
System.out.println(b.d);
247
// Integer (1 word) with side-effects in object reference
251
System.out.println(b.i);
253
AccessMethodsLHS.fail("simple assignment");
255
System.out.println(b.i);
257
if ((iBar(b).i += 10) != 15)
258
AccessMethodsLHS.fail("add-assign result");
260
System.out.println(b.i);
263
AccessMethodsLHS.fail("add-assign effect");
264
System.out.println(b.i);
270
AccessMethodsLHS.fail("post-increment effect");
271
System.out.println(b.i);
274
if (iBar(b).i++ != 5)
275
AccessMethodsLHS.fail("post-increment result");
278
AccessMethodsLHS.fail("post-increment effect (embedded)");
279
System.out.println(b.i);
285
AccessMethodsLHS.fail("pre-decrement effect");
288
if (--iBar(b).i != 4)
289
AccessMethodsLHS.fail("pre-decrement result");
292
AccessMethodsLHS.fail("pre-decrement effect (embedded)");
293
System.out.println(b.i);
296
// *** Static class, Static members ***
303
System.out.println(Bar.si);
305
AccessMethodsLHS.fail("simple assignment");
306
System.out.println(Bar.si);
308
if ((Bar.si += 10) != 15)
309
AccessMethodsLHS.fail("add-assign result");
310
System.out.println(Bar.si);
313
AccessMethodsLHS.fail("add-assign effect");
314
System.out.println(Bar.si);
317
if (!(Bar.ss += "bar").equals("foobar"))
318
AccessMethodsLHS.fail("concat-assign result");
319
System.out.println(Bar.ss);
321
if (!Bar.ss.equals("foobar"))
322
AccessMethodsLHS.fail("concat-assign effect");
323
System.out.println(Bar.ss);
327
if (!Bar.ss.equals("foobar"))
328
AccessMethodsLHS.fail("concat-assign effect (novalue)");
329
System.out.println(Bar.ss);
334
AccessMethodsLHS.fail("post-increment effect");
335
System.out.println(Bar.si);
339
AccessMethodsLHS.fail("post-increment result");
341
AccessMethodsLHS.fail("post-increment effect (embedded)");
342
System.out.println(Bar.si);
347
AccessMethodsLHS.fail("pre-decrement effect");
351
AccessMethodsLHS.fail("pre-decrement result");
353
AccessMethodsLHS.fail("pre-decrement effect (embedded)");
354
System.out.println(Bar.si);
359
System.out.println(Bar.sd);
361
AccessMethodsLHS.fail("simple assignment");
362
System.out.println(Bar.sd);
364
if ((Bar.sd += 10) != 15.0)
365
AccessMethodsLHS.fail("add-assign result");
366
System.out.println(Bar.sd);
369
AccessMethodsLHS.fail("add-assign effect");
370
System.out.println(Bar.sd);
375
AccessMethodsLHS.fail("post-increment effect");
376
System.out.println(Bar.sd);
380
AccessMethodsLHS.fail("post-increment result");
382
AccessMethodsLHS.fail("post-increment effect (embedded)");
383
System.out.println(Bar.sd);
388
AccessMethodsLHS.fail("pre-decrement effect");
392
AccessMethodsLHS.fail("pre-decrement result");
394
AccessMethodsLHS.fail("pre-decrement effect (embedded)");
395
System.out.println(Bar.sd);
398
// *** Static class, Static members (invoked via object reference) ***
408
System.out.println(b.si);
410
AccessMethodsLHS.fail("simple assignment");
411
System.out.println(b.si);
413
if ((b.si += 10) != 15)
414
AccessMethodsLHS.fail("add-assign result");
415
System.out.println(b.si);
418
AccessMethodsLHS.fail("add-assign effect");
419
System.out.println(b.si);
422
if (!(b.ss += "bar").equals("foobar"))
423
AccessMethodsLHS.fail("concat-assign result");
424
System.out.println(b.ss);
426
if (!b.ss.equals("foobar"))
427
AccessMethodsLHS.fail("concat-assign effect");
428
System.out.println(b.ss);
432
if (!b.ss.equals("foobar"))
433
AccessMethodsLHS.fail("concat-assign effect (novalue)");
434
System.out.println(b.ss);
439
AccessMethodsLHS.fail("post-increment effect");
440
System.out.println(b.si);
444
AccessMethodsLHS.fail("post-increment result");
446
AccessMethodsLHS.fail("post-increment effect (embedded)");
447
System.out.println(b.si);
452
AccessMethodsLHS.fail("pre-decrement effect");
456
AccessMethodsLHS.fail("pre-decrement result");
458
AccessMethodsLHS.fail("pre-decrement effect (embedded)");
459
System.out.println(b.si);
464
System.out.println(b.sd);
466
AccessMethodsLHS.fail("simple assignment");
467
System.out.println(b.sd);
469
if ((b.sd += 10) != 15.0)
470
AccessMethodsLHS.fail("add-assign result");
471
System.out.println(b.sd);
474
AccessMethodsLHS.fail("add-assign effect");
475
System.out.println(b.sd);
480
AccessMethodsLHS.fail("post-increment effect");
481
System.out.println(b.sd);
485
AccessMethodsLHS.fail("post-increment result");
487
AccessMethodsLHS.fail("post-increment effect (embedded)");
488
System.out.println(b.sd);
493
AccessMethodsLHS.fail("pre-decrement effect");
497
AccessMethodsLHS.fail("pre-decrement result");
499
AccessMethodsLHS.fail("pre-decrement effect (embedded)");
500
System.out.println(b.sd);
502
// Integer (1 word) with side-effects in object reference
506
System.out.println(b.si);
508
AccessMethodsLHS.fail("simple assignment");
509
System.out.println(b.si);
511
if ((iBar(b).si += 10) != 15)
512
AccessMethodsLHS.fail("add-assign result");
514
System.out.println(b.si);
517
AccessMethodsLHS.fail("add-assign effect");
518
System.out.println(b.si);
525
AccessMethodsLHS.fail("post-increment effect");
526
System.out.println(b.si);
529
if (iBar(b).si++ != 5)
530
AccessMethodsLHS.fail("post-increment result");
533
AccessMethodsLHS.fail("post-increment effect (embedded)");
534
System.out.println(b.si);
540
AccessMethodsLHS.fail("pre-decrement effect");
543
if (--iBar(b).si != 4)
544
AccessMethodsLHS.fail("pre-decrement result");
547
AccessMethodsLHS.fail("pre-decrement effect (embedded)");
548
System.out.println(b.si);
551
// *** Non-static class, Non-static members ***
561
System.out.println(c.i);
563
AccessMethodsLHS.fail("simple assignment");
564
System.out.println(c.i);
566
if ((c.i += 10) != 15)
567
AccessMethodsLHS.fail("add-assign result");
568
System.out.println(c.i);
571
AccessMethodsLHS.fail("add-assign effect");
572
System.out.println(c.i);
575
if (!(c.s += "bar").equals("foobar"))
576
AccessMethodsLHS.fail("concat-assign result");
577
System.out.println(c.s);
579
if (!c.s.equals("foobar"))
580
AccessMethodsLHS.fail("concat-assign effect");
581
System.out.println(c.s);
585
if (!c.s.equals("foobar"))
586
AccessMethodsLHS.fail("concat-assign effect (novalue)");
587
System.out.println(c.s);
592
AccessMethodsLHS.fail("post-increment effect");
593
System.out.println(c.i);
597
AccessMethodsLHS.fail("post-increment result");
599
AccessMethodsLHS.fail("post-increment effect (embedded)");
600
System.out.println(c.i);
605
AccessMethodsLHS.fail("pre-decrement effect");
609
AccessMethodsLHS.fail("pre-decrement result");
611
AccessMethodsLHS.fail("pre-decrement effect (embedded)");
612
System.out.println(c.i);
617
System.out.println(c.d);
619
AccessMethodsLHS.fail("simple assignment");
620
System.out.println(c.d);
622
if ((c.d += 10) != 15.0)
623
AccessMethodsLHS.fail("add-assign result");
624
System.out.println(c.d);
627
AccessMethodsLHS.fail("add-assign effect");
628
System.out.println(c.d);
633
AccessMethodsLHS.fail("post-increment effect");
634
System.out.println(c.d);
638
AccessMethodsLHS.fail("post-increment result");
640
AccessMethodsLHS.fail("post-increment effect (embedded)");
641
System.out.println(c.d);
646
AccessMethodsLHS.fail("pre-decrement effect");
650
AccessMethodsLHS.fail("pre-decrement result");
652
AccessMethodsLHS.fail("pre-decrement effect (embedded)");
653
System.out.println(c.d);
655
// Double (2 word) with side-effects in object reference
659
System.out.println(c.d);
661
AccessMethodsLHS.fail("simple assignment");
662
System.out.println(c.d);
664
if ((iBaz(c).d += 10) != 15.0)
665
AccessMethodsLHS.fail("add-assign result");
667
System.out.println(c.d);
670
AccessMethodsLHS.fail("add-assign effect");
671
System.out.println(c.d);
677
AccessMethodsLHS.fail("post-increment effect");
678
System.out.println(c.d);
681
if (iBaz(c).d++ != 5.0)
682
AccessMethodsLHS.fail("post-increment result");
685
AccessMethodsLHS.fail("post-increment effect (embedded)");
686
System.out.println(c.d);
692
AccessMethodsLHS.fail("pre-decrement effect");
695
if (--iBaz(c).d != 4.0)
696
AccessMethodsLHS.fail("pre-decrement result");
699
AccessMethodsLHS.fail("pre-decrement effect (embedded)");
700
System.out.println(c.d);
704
System.out.println("done");