1 /*
2  * Copyright (c) 2018, 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.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 /*
27  * @test
28  * @bug 8204610
29  * @summary Compiler confused by parenthesized "this" in final fields assignments
30  * @library /tools/javac/lib
31  * @modules jdk.compiler/com.sun.tools.javac.api
32  *          jdk.compiler/com.sun.tools.javac.code
33  *          jdk.compiler/com.sun.tools.javac.comp
34  *          jdk.compiler/com.sun.tools.javac.main
35  *          jdk.compiler/com.sun.tools.javac.tree
36  *          jdk.compiler/com.sun.tools.javac.util
37  * @build combo.ComboTestHelper
38 
39  * @run main T8204610
40  */
41 
42 import combo.ComboInstance;
43 import combo.ComboParameter;
44 import combo.ComboTask.Result;
45 import combo.ComboTestHelper;
46 
47 public class T8204610 extends ComboInstance<T8204610> {
48 
49     enum ParenKind implements ComboParameter {
50         NONE(""),
51         ONE("#P"),
52         TWO("#P#P"),
53         THREE("#P#P#P");
54 
55         String parensTemplate;
56 
ParenKind(String parensTemplate)57         ParenKind(String parensTemplate) {
58             this.parensTemplate = parensTemplate;
59         }
60 
61         @Override
expand(String optParameter)62         public String expand(String optParameter) {
63             return parensTemplate.replaceAll("#P", optParameter.equals("OPEN") ? "(" : ")");
64         }
65     }
66 
main(String... args)67     public static void main(String... args) {
68         new ComboTestHelper<T8204610>()
69                 .withArrayDimension("PAREN", (x, pk, idx) -> x.parenKinds[idx] = pk, 3, ParenKind.values())
70                 .run(T8204610::new);
71     }
72 
73     ParenKind[] parenKinds = new ParenKind[3];
74 
75     @Override
doWork()76     public void doWork() {
77         newCompilationTask()
78                 .withSourceFromTemplate(bodyTemplate)
79                 .analyze(this::check);
80     }
81 
82     String bodyTemplate = "class Test {\n" +
83                           "   final int x;\n" +
84                           "   Test() {\n" +
85                           "      #{PAREN[0].OPEN} #{PAREN[1].OPEN} this #{PAREN[1].CLOSE} . #{PAREN[2].OPEN} x #{PAREN[2].CLOSE} #{PAREN[0].CLOSE} = 1;\n" +
86                           "   } }";
87 
check(Result<?> res)88     void check(Result<?> res) {
89         boolean expectedFail = parenKinds[2] != ParenKind.NONE;
90         if (expectedFail != res.hasErrors()) {
91             fail("unexpected compilation result for source:\n" +
92                 res.compilationInfo());
93         }
94     }
95 }
96