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 4147520
27  * @summary Verify correct implementation of qualified 'this' and 'super'.
28  * @author maddox
29  *
30  * @run compile QualifiedThisAndSuper_1.java
31  * @run main QualifiedThisAndSuper_1
32  */
33 
34 public class QualifiedThisAndSuper_1 {
35 
36     public class AS {
37         String s = "ass";
38         private String t = "ast";
39         protected String u = "asu";
m()40         String m() { return "asm"; }
n()41         private String n() { return "asn"; }
o()42         protected String o() { return "aso"; }
43     }
44 
45     public class BS {
46         String s = "bss";
47         private String t = "bst";
48         protected String u = "bsu";
m()49         String m() { return "bsm"; }
n()50         private String n() { return "bsn"; }
o()51         protected String o() { return "bso"; }
52     }
53 
54     public class CS {
55         String s = "css";
56         private String t = "cst";
57         protected String u = "csu";
m()58         String m() { return "csm"; }
n()59         private String n() { return "csn"; }
o()60         protected String o() { return "cso"; }
61     }
62 
63 
check(String expr, String result, String expected)64     void check(String expr, String result, String expected) {
65         if (!result.equals(expected)) {
66             throw new Error("Evaluated "+ expr +
67                             " : result " + result + ", expected " + expected);
68         }
69     }
70 
71     public class A extends AS {
A()72         A() { super(); }
73         String s = "as";
74         private String t = "at";
75         protected String u = "au";
m()76         String m() { return "am"; }
n()77         private String n() { return "an"; }
o()78         protected String o() { return "ao"; }
79         public class B extends BS {
B()80             B() { super(); }
81             String s = "bs";
82             private String t = "bt";
83             protected String u = "bu";
m()84             String m() { return "bm"; }
n()85             private String n() { return "bn"; }
o()86             protected String o() { return "bo"; }
87             public class C extends CS {
C()88                 C() { super(); }
89                 String s = "cs";
90                 private String t = "ct";
91                 protected String u = "cu";
m()92                 String m() { return "cm"; }
n()93                 private String n() { return "cn"; }
o()94                 protected String o() { return "co"; }
test()95                 void test() {
96 
97                     check("this.m()", this.m(), "cm");
98 
99                     check("A.this.m()", A.this.m(), "am");
100                     check("B.this.m()", B.this.m(), "bm");
101                     check("C.this.m()", C.this.m(), "cm");
102 
103                     check("super.m()", super.m(), "csm");
104 
105                     check("A.super.m()", A.super.m(), "asm");
106                     check("B.super.m()", B.super.m(), "bsm");
107                     check("C.super.m()", C.super.m(), "csm");
108 
109                     // should re-use access methods.
110                     check("A.super.m()", A.super.m(), "asm");
111                     check("B.super.m()", B.super.m(), "bsm");
112                     check("C.super.m()", C.super.m(), "csm");
113 
114                     //---
115 
116                     check("this.n()", this.n(), "cn");
117 
118                     check("A.this.n()", A.this.n(), "an");
119                     check("B.this.n()", B.this.n(), "bn");
120                     check("C.this.n()", C.this.n(), "cn");
121 
122 
123                     check("super.n()", super.n(), "csn");
124 
125 
126 
127                     check("A.super.n()", A.super.n(), "asn");
128 
129                     check("B.super.n()", B.super.n(), "bsn");
130                     check("C.super.n()", C.super.n(), "csn");
131 
132                     // should re-use access methods.
133                     check("A.super.n()", A.super.n(), "asn");
134                     check("B.super.n()", B.super.n(), "bsn");
135                     check("C.super.n()", C.super.n(), "csn");
136 
137                     //---
138 
139                     check("this.o()", this.o(), "co");
140 
141                     check("A.this.o()", A.this.o(), "ao");
142                     check("B.this.o()", B.this.o(), "bo");
143                     check("C.this.o()", C.this.o(), "co");
144 
145                     check("super.o()", super.o(), "cso");
146 
147                     check("A.super.o()", A.super.o(), "aso");
148                     check("B.super.o()", B.super.o(), "bso");
149                     check("C.super.o()", C.super.o(), "cso");
150 
151                     // should re-use access methods.
152                     check("A.super.o()", A.super.o(), "aso");
153                     check("B.super.o()", B.super.o(), "bso");
154                     check("C.super.o()", C.super.o(), "cso");
155 
156                     //---
157 
158                     check("this.s", this.s, "cs");
159 
160                     check("A.this.s", A.this.s, "as");
161                     check("B.this.s", B.this.s, "bs");
162                     check("C.this.s", C.this.s, "cs");
163 
164                     //---
165 
166                     check("this.t", this.t, "ct");
167 
168                     check("A.this.t", A.this.t, "at");
169                     check("B.this.t", B.this.t, "bt");
170                     check("C.this.t", C.this.t, "ct");
171 
172                     //---
173 
174                     check("this.u", this.u, "cu");
175 
176                     check("A.this.u", A.this.u, "au");
177                     check("B.this.u", B.this.u, "bu");
178                     check("C.this.u", C.this.u, "cu");
179 
180                     //---
181 
182                     check("super.s", super.s, "css");
183 
184                     check("A.super.s", A.super.s, "ass");
185                     check("B.super.s", B.super.s, "bss");
186                     check("C.super.s", C.super.s, "css");
187 
188                     //---
189 
190                     check("super.t", super.t, "cst");
191 
192                     check("A.super.t", A.super.t, "ast");
193                     check("B.super.t", B.super.t, "bst");
194                     check("C.super.t", C.super.t, "cst");
195 
196                     //---
197 
198                     check("super.u", super.u, "csu");
199 
200                     check("A.super.u", A.super.u, "asu");
201                     check("B.super.u", B.super.u, "bsu");
202                     check("C.super.u", C.super.u, "csu");
203 
204                     //---
205 
206                     A.this.s = "foo";
207                     System.out.println(A.this.s);
208                     check("A.this.s", A.this.s, "foo");
209                     B.this.s = "bar";
210                     System.out.println(B.this.s);
211                     check("B.this.s", B.this.s, "bar");
212                     C.this.s = "baz";
213                     System.out.println(C.this.s);
214                     check("C.this.s", C.this.s, "baz");
215 
216                     A.this.t = "foo";
217                     System.out.println(A.this.t);
218                     check("A.this.t", A.this.t, "foo");
219                     B.this.t = "bar";
220                     System.out.println(B.this.t);
221                     check("B.this.t", B.this.t, "bar");
222                     C.this.t = "baz";
223                     System.out.println(C.this.t);
224                     check("C.this.t", C.this.t, "baz");
225 
226                     A.this.u = "foo";
227                     System.out.println(A.this.u);
228                     check("A.this.u", A.this.u, "foo");
229                     B.this.u = "bar";
230                     System.out.println(B.this.u);
231                     check("B.this.u", B.this.u, "bar");
232                     C.this.u = "baz";
233                     System.out.println(C.this.u);
234                     check("C.this.u", C.this.u, "baz");
235 
236                     A.super.s = "foo";
237                     System.out.println(A.super.s);
238                     check("A.super.s", A.super.s, "foo");
239                     B.super.s = "bar";
240                     System.out.println(B.super.s);
241                     check("B.super.s", B.super.s, "bar");
242                     C.super.s = "baz";
243                     System.out.println(C.super.s);
244                     check("C.super.s", C.super.s, "baz");
245 
246                     A.super.t = "foo";
247                     System.out.println(A.super.t);
248                     check("A.super.t", A.super.t, "foo");
249                     B.super.t = "bar";
250                     System.out.println(B.super.t);
251                     check("B.super.t", B.super.t, "bar");
252                     C.super.t = "baz";
253                     System.out.println(C.super.t);
254                     check("C.super.t", C.super.t, "baz");
255 
256                     A.super.u = "foo";
257                     System.out.println(A.super.u);
258                     check("A.super.u", A.super.u, "foo");
259                     B.super.u = "bar";
260                     System.out.println(B.super.u);
261                     check("B.super.u", B.super.u, "bar");
262                     C.super.u = "baz";
263                     System.out.println(C.super.u);
264                     check("C.super.u", C.super.u, "baz");
265 
266                 }
267             }
test()268             void test() throws Exception {
269                 C c = new C();
270                 c.test();
271             }
272         }
test()273         void test() throws Exception {
274             B b = new B();
275             b.test();
276         }
277     }
278 
main(String[] args)279     public static void main(String[] args) throws Exception {
280         A a = new QualifiedThisAndSuper_1().new A();
281         a.test();
282     }
283 }
284