QualifiedThisAccessTest.java revision 3087:ed4c306ec942
1/*
2 * Copyright (c) 2015, 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 8129740 8133111
27 * @summary Incorrect class file created when passing lambda in inner class constructor
28 * @run main QualifiedThisAccessTest
29 */
30
31public class QualifiedThisAccessTest { // Not referenced by lambda, so should not be captured.
32
33    public class Universe { // Not referenced by lambda, so should not be captured.
34
35        public String name;
36        public int galaxiesCount;
37
38        public Universe(String name, int galaxiesCount) {
39            this.name = name;
40            this.galaxiesCount = galaxiesCount;
41        }
42
43        public String toString() {
44            return "Universe" + name + " of " + galaxiesCount + " galaxies";
45        }
46
47        class Galaxy {
48
49            String name;
50            private int starsCount;
51
52            Galaxy(String name, int starsCount) {
53                this.name = name;
54                this.starsCount = starsCount;
55            }
56
57            public String toString() {
58                return "galaxy " + name + " of " + starsCount + " solar systems";
59            }
60
61            int starsCount() {
62                return starsCount;
63            }
64
65            private String name() {
66                return name;
67            }
68
69            class SolarSystem {
70
71                String name;
72                int planetsCount;
73
74                SolarSystem(String name, int planetsCount) {
75                    this.name = name;
76                    this.planetsCount = planetsCount;
77                }
78
79                public String toString() {
80                    return "Solar System of " + name + " with " + planetsCount + " planets";
81                }
82
83                int planetsCount() {
84                    return planetsCount;
85                }
86
87                SolarSystem copy(SolarSystem s) {
88                    return s;
89                }
90
91                class Planet {
92
93                    String name;
94                    int moonsCount;
95
96                    Planet(String name, int moonsCount, Runnable r) {
97                        this.name = name;
98                        this.moonsCount = moonsCount;
99                        r.run();
100                    }
101                    Planet (String name, int moonsCount) {
102                        this(name, moonsCount, ()-> {
103                            StringBuffer buf = new StringBuffer();
104                            buf.append("This planet belongs to the galaxy "
105                                        + Galaxy.this.name + " with " + starsCount + " stars\n");
106                            buf.append("This planet belongs to the galaxy "
107                                        + Universe.Galaxy.this.name + " with " + starsCount() + " stars\n");
108                            buf.append("This planet belongs to the galaxy "
109                                        + Galaxy.this.name() + " with " + starsCount() + " stars\n");
110                            buf.append("This planet belongs to the galaxy "
111                                        + Universe.Galaxy.this.name() + " with "
112                                        + (Universe.Galaxy.this).starsCount() + " stars\n");
113
114                            buf.append("This planet belongs to the solar system "
115                                        + SolarSystem.this.name + " with " + planetsCount + " planets\n");
116                            buf.append("This planet belongs to the solar system "
117                                        + Galaxy.SolarSystem.this.name + " with " + planetsCount() + " planets\n");
118                            buf.append("This planet belongs to the solar system "
119                                        + (SolarSystem.this).name + " with " + planetsCount + " planets\n");
120                            buf.append("This planet belongs to the solar system "
121                                        + Universe.Galaxy.SolarSystem.this.name + " with "
122                                        + Universe.Galaxy.SolarSystem.this.planetsCount + " planets\n");
123                            buf.append("This planet belongs to the solar system "
124                                        + Universe.Galaxy.SolarSystem.this.name.toLowerCase().toUpperCase()
125                                        + " with " + Universe.Galaxy.SolarSystem.this.planetsCount + " planets\n");
126                            buf.append("This planet belongs to the solar system "
127                                        + copy(Universe.Galaxy.SolarSystem.this).name.toLowerCase().toUpperCase()
128                                        + " with " + Universe.Galaxy.SolarSystem.this.planetsCount + " planets\n");
129                            if (!buf.toString().equals(output))
130                                throw new AssertionError("Unexpected value\n" + buf);
131                        });
132                    }
133
134                    static final String output =
135                        "This planet belongs to the galaxy Mily way with 23456789 stars\n" +
136                        "This planet belongs to the galaxy Mily way with 23456789 stars\n" +
137                        "This planet belongs to the galaxy Mily way with 23456789 stars\n" +
138                        "This planet belongs to the galaxy Mily way with 23456789 stars\n" +
139                        "This planet belongs to the solar system Sun with 9 planets\n" +
140                        "This planet belongs to the solar system Sun with 9 planets\n" +
141                        "This planet belongs to the solar system Sun with 9 planets\n" +
142                        "This planet belongs to the solar system Sun with 9 planets\n" +
143                        "This planet belongs to the solar system SUN with 9 planets\n" +
144                        "This planet belongs to the solar system SUN with 9 planets\n";
145
146
147                    public String toString() {
148                        return "Planet " + name + " with " + moonsCount + " moon(s)";
149                    }
150                }
151            }
152        }
153    }
154    public static void main(String[] args) {
155        new QualifiedThisAccessTest().new Universe("Universe", 12345678).new Galaxy("Mily way", 23456789).new SolarSystem("Sun", 9).new Planet("Earth", 1);
156    }
157}
158