test/tools/javac/QualifiedThisAndSuper_1.java

Wed, 14 Nov 2018 10:18:25 -0800

author
diazhou
date
Wed, 14 Nov 2018 10:18:25 -0800
changeset 3762
7909abb85562
parent 554
9d9f26857129
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Added tag jdk8u201-b04 for changeset a7f48b9dfb82

     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  */
    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  */
    34 public class QualifiedThisAndSuper_1 {
    36     public class AS {
    37         String s = "ass";
    38         private String t = "ast";
    39         protected String u = "asu";
    40         String m() { return "asm"; }
    41         private String n() { return "asn"; }
    42         protected String o() { return "aso"; }
    43     }
    45     public class BS {
    46         String s = "bss";
    47         private String t = "bst";
    48         protected String u = "bsu";
    49         String m() { return "bsm"; }
    50         private String n() { return "bsn"; }
    51         protected String o() { return "bso"; }
    52     }
    54     public class CS {
    55         String s = "css";
    56         private String t = "cst";
    57         protected String u = "csu";
    58         String m() { return "csm"; }
    59         private String n() { return "csn"; }
    60         protected String o() { return "cso"; }
    61     }
    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     }
    71     public class A extends AS {
    72         A() { super(); }
    73         String s = "as";
    74         private String t = "at";
    75         protected String u = "au";
    76         String m() { return "am"; }
    77         private String n() { return "an"; }
    78         protected String o() { return "ao"; }
    79         public class B extends BS {
    80             B() { super(); }
    81             String s = "bs";
    82             private String t = "bt";
    83             protected String u = "bu";
    84             String m() { return "bm"; }
    85             private String n() { return "bn"; }
    86             protected String o() { return "bo"; }
    87             public class C extends CS {
    88                 C() { super(); }
    89                 String s = "cs";
    90                 private String t = "ct";
    91                 protected String u = "cu";
    92                 String m() { return "cm"; }
    93                 private String n() { return "cn"; }
    94                 protected String o() { return "co"; }
    95                 void test() {
    97                     check("this.m()", this.m(), "cm");
    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");
   103                     check("super.m()", super.m(), "csm");
   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");
   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");
   114                     //---
   116                     check("this.n()", this.n(), "cn");
   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");
   123                     check("super.n()", super.n(), "csn");
   127                     check("A.super.n()", A.super.n(), "asn");
   129                     check("B.super.n()", B.super.n(), "bsn");
   130                     check("C.super.n()", C.super.n(), "csn");
   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");
   137                     //---
   139                     check("this.o()", this.o(), "co");
   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");
   145                     check("super.o()", super.o(), "cso");
   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");
   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");
   156                     //---
   158                     check("this.s", this.s, "cs");
   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");
   164                     //---
   166                     check("this.t", this.t, "ct");
   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");
   172                     //---
   174                     check("this.u", this.u, "cu");
   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");
   180                     //---
   182                     check("super.s", super.s, "css");
   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");
   188                     //---
   190                     check("super.t", super.t, "cst");
   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");
   196                     //---
   198                     check("super.u", super.u, "csu");
   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");
   204                     //---
   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");
   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");
   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");
   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");
   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");
   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");
   266                 }
   267             }
   268             void test() throws Exception {
   269                 C c = new C();
   270                 c.test();
   271             }
   272         }
   273         void test() throws Exception {
   274             B b = new B();
   275             b.test();
   276         }
   277     }
   279     public static void main(String[] args) throws Exception {
   280         A a = new QualifiedThisAndSuper_1().new A();
   281         a.test();
   282     }
   283 }

mercurial