test/tools/javac/AccessMethods/ConstructorAccess.java

changeset 1
9a66ca7c79fa
child 554
9d9f26857129
equal deleted inserted replaced
-1:000000000000 1:9a66ca7c79fa
1 /*
2 * Copyright 1998 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24 /*
25 * @test
26 * @bug 4116460
27 * @summary Test access methods for private constructors.
28 * @author William Maddox (maddox)
29 *
30 * @compile ConstructorAccess.java
31 * @run main ConstructorAccess
32 */
33
34 public class ConstructorAccess {
35 int i = 0;
36 char c = 'x';
37
38 private ConstructorAccess() {
39 this.i = 42;
40 }
41
42 private ConstructorAccess(int i, char c) {
43 this.i = i;
44 this.c = c;
45 }
46
47 class Inner {
48 int j;
49 char c;
50 boolean b;
51
52 private Inner() {
53 this.j = 0;
54 this.b = false;
55 this.c = 'x';
56 }
57 private Inner(int i, char c) {
58 this.j = i;
59 this.b = true;
60 this.c = c;
61 ConstructorAccess.this.i = i;
62 }
63 private Inner(int i, boolean b) {
64 this.j = i;
65 this.b = b;
66 this.c = 'x';
67 }
68 void foo() throws Exception {
69 ConstructorAccess x = new ConstructorAccess();
70 if (x.i != 42 || x.c != 'x') {
71 throw new Exception("error 1");
72 }
73 ConstructorAccess y = new ConstructorAccess(555, 'y');
74 if (y.i != 555 || y.c != 'y') {
75 throw new Exception("error2");
76 }
77 }
78 void check(int j, char c, boolean b) throws Exception {
79 if (this.j != j || this.c != c || this.b != b) {
80 throw new Exception("error3");
81 }
82 }
83 }
84
85 void bar() throws Exception {
86 Inner x = new Inner();
87 x.check(0, 'x', false);
88 x.foo();
89 Inner y = new Inner(747, 'z');
90 y.check(747, 'z', true);
91 if (this.i != 747) {
92 throw new Exception("error 4");
93 }
94 Inner z = new Inner(777, true);
95 z.check(777, 'x' , true);
96 }
97
98 class InnerSub extends Inner {
99 private InnerSub() {
100 super();
101 }
102 private InnerSub(int i) {
103 super(i, 'w');
104 }
105 private InnerSub(int i, boolean b) {
106 super(i, b);
107 }
108 }
109
110 public static void main(String[] args) throws Exception {
111 ConstructorAccess o = new ConstructorAccess();
112 o.bar();
113 InnerSub x = o.new InnerSub();
114 x.check(0, 'x', false);
115 x.foo();
116 InnerSub y = o.new InnerSub(767);
117 y.check(767, 'w', true);
118 if (o.i != 767) {
119 throw new Exception("error 5");
120 }
121 InnerSub z = o.new InnerSub(777, true);
122 z.check(777, 'x' , true);
123 }
124 }

mercurial