QualifiedThisAccessTest.java 7.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
/*
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * @test
 * @bug 8129740 8133111 8157142
 * @summary Incorrect class file created when passing lambda in inner class constructor
 * @run main QualifiedThisAccessTest
 */

public class QualifiedThisAccessTest { // Not referenced by lambda, so should not be captured.

    public class Universe { // Not referenced by lambda, so should not be captured.

        public String name;
        public int galaxiesCount;

        public Universe(String name, int galaxiesCount) {
            this.name = name;
            this.galaxiesCount = galaxiesCount;
        }

        public String toString() {
            return "Universe" + name + " of " + galaxiesCount + " galaxies";
        }

        class Galaxy {

            String name;
            private int starsCount;

            Galaxy(String name, int starsCount) {
                this.name = name;
                this.starsCount = starsCount;
            }

            public String toString() {
                return "galaxy " + name + " of " + starsCount + " solar systems";
            }

            int starsCount() {
                return starsCount;
            }

            private String name() {
                return name;
            }

            class SolarSystem {

                String name;
                int planetsCount;

                SolarSystem(String name, int planetsCount) {
                    this.name = name;
                    this.planetsCount = planetsCount;
                }

                public String toString() {
                    return "Solar System of " + name + " with " + planetsCount + " planets";
                }

                int planetsCount() {
                    return planetsCount;
                }

                SolarSystem copy(SolarSystem s) {
                    return s;
                }

                class Planet {

                    String name;
                    int moonsCount;

                    Planet(String name, int moonsCount, Runnable r) {
                        this.name = name;
                        this.moonsCount = moonsCount;
                        r.run();
                    }
                    Planet (String name, int moonsCount) {
                        this(name, moonsCount, ()-> {
                            StringBuffer buf = new StringBuffer();
                            buf.append("This planet belongs to the galaxy "
                                        + Galaxy.this.name + " with " + starsCount + " stars\n");
                            buf.append("This planet belongs to the galaxy "
                                        + Universe.Galaxy.this.name + " with " + starsCount() + " stars\n");
                            buf.append("This planet belongs to the galaxy "
                                        + Galaxy.this.name() + " with " + starsCount() + " stars\n");
                            buf.append("This planet belongs to the galaxy "
                                        + Universe.Galaxy.this.name() + " with "
                                        + (Universe.Galaxy.this).starsCount() + " stars\n");

                            buf.append("This planet belongs to the solar system "
                                        + SolarSystem.this.name + " with " + planetsCount + " planets\n");
                            buf.append("This planet belongs to the solar system "
                                        + Galaxy.SolarSystem.this.name + " with " + planetsCount() + " planets\n");
                            buf.append("This planet belongs to the solar system "
                                        + (SolarSystem.this).name + " with " + planetsCount + " planets\n");
                            buf.append("This planet belongs to the solar system "
                                        + Universe.Galaxy.SolarSystem.this.name + " with "
                                        + Universe.Galaxy.SolarSystem.this.planetsCount + " planets\n");
                            buf.append("This planet belongs to the solar system "
                                        + Universe.Galaxy.SolarSystem.this.name.toLowerCase().toUpperCase()
                                        + " with " + Universe.Galaxy.SolarSystem.this.planetsCount + " planets\n");
                            buf.append("This planet belongs to the solar system "
                                        + copy(Universe.Galaxy.SolarSystem.this).name.toLowerCase().toUpperCase()
                                        + " with " + Universe.Galaxy.SolarSystem.this.planetsCount + " planets\n");
                            if (!buf.toString().equals(output))
                                throw new AssertionError("Unexpected value\n" + buf);
                        });
                    }

                    static final String output =
                        "This planet belongs to the galaxy Mily way with 23456789 stars\n" +
                        "This planet belongs to the galaxy Mily way with 23456789 stars\n" +
                        "This planet belongs to the galaxy Mily way with 23456789 stars\n" +
                        "This planet belongs to the galaxy Mily way with 23456789 stars\n" +
                        "This planet belongs to the solar system Sun with 9 planets\n" +
                        "This planet belongs to the solar system Sun with 9 planets\n" +
                        "This planet belongs to the solar system Sun with 9 planets\n" +
                        "This planet belongs to the solar system Sun with 9 planets\n" +
                        "This planet belongs to the solar system SUN with 9 planets\n" +
                        "This planet belongs to the solar system SUN with 9 planets\n";


                    public String toString() {
                        return "Planet " + name + " with " + moonsCount + " moon(s)";
                    }
                }
            }
        }
    }
    public static void main(String[] args) {
        new QualifiedThisAccessTest().new Universe("Universe", 12345678).new Galaxy("Mily way", 23456789).new SolarSystem("Sun", 9).new Planet("Earth", 1);
    }
}