1/*
2 * Copyright (c) 2016, 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 8160156
26 * @summary javac is generating let expressions unnecessarily
27 * @library /tools/lib
28 * @modules
29 *      jdk.compiler/com.sun.tools.javac.api
30 *      jdk.compiler/com.sun.tools.javac.main
31 * @build toolbox.ToolBox toolbox.JavacTask
32 * @run main LetExpressionsAreUnnecessarilyGeneratedTest
33 */
34
35import java.nio.file.Files;
36import java.nio.file.Path;
37import java.nio.file.Paths;
38
39import toolbox.JavacTask;
40import toolbox.Task;
41import toolbox.TestRunner;
42import toolbox.ToolBox;
43
44public class LetExpressionsAreUnnecessarilyGeneratedTest extends TestRunner {
45    ToolBox tb;
46
47    public static void main(String... args) throws Exception {
48        new LetExpressionsAreUnnecessarilyGeneratedTest().runTests();
49    }
50
51    public LetExpressionsAreUnnecessarilyGeneratedTest() {
52        super(System.err);
53        tb = new ToolBox();
54    }
55
56    protected void runTests() throws Exception {
57        runTests(m -> new Object[] { Paths.get(m.getName()) });
58    }
59
60    @Test
61    public void testDontGenerateLetExpr(Path testBase) throws Exception {
62        Path src = testBase.resolve("src");
63        tb.writeJavaFiles(src,
64                "package base;\n" +
65                "public abstract class Base {\n" +
66                "    protected int i = 1;\n" +
67                "}",
68
69                "package sub;\n" +
70                "import base.Base;\n" +
71                "public class Sub extends Base {\n" +
72                "    private int i = 4;\n" +
73                "    void m() {\n" +
74                "        new Runnable() {\n" +
75                "            public void run() {\n" +
76                "                Sub.super.i += 10;\n" +
77                "            }\n" +
78                "        };\n" +
79                "    }\n" +
80                "}");
81
82        Path out = testBase.resolve("out");
83        Files.createDirectories(out);
84        Path base = src.resolve("base");
85        Path sub = src.resolve("sub");
86
87        new JavacTask(tb)
88            .outdir(out)
89            .files(tb.findJavaFiles(base))
90            .run(Task.Expect.SUCCESS);
91
92        new JavacTask(tb)
93            .classpath(out)
94            .outdir(out)
95            .files(tb.findJavaFiles(sub))
96            .run(Task.Expect.SUCCESS);
97    }
98}
99