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.
 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 8204610
27  * @summary Compiler confused by parenthesized "this" in final fields assignments
28  * @library /tools/javac/lib
29  * @modules jdk.compiler/com.sun.tools.javac.api
30  *          jdk.compiler/com.sun.tools.javac.file
31  *          jdk.compiler/com.sun.tools.javac.util
32  * @build combo.ComboTestHelper
33 
34  * @run main T8204610
35  */
36 
37 import combo.ComboInstance;
38 import combo.ComboParameter;
39 import combo.ComboTask.Result;
40 import combo.ComboTestHelper;
41 
42 public class T8204610 extends ComboInstance<T8204610> {
43 
44     enum ParenKind implements ComboParameter {
45         NONE(""),
46         ONE("#P"),
47         TWO("#P#P"),
48         THREE("#P#P#P");
49 
50         String parensTemplate;
51 
52         ParenKind(String parensTemplate) {
53             this.parensTemplate = parensTemplate;
54         }
55 
56         @Override
57         public String expand(String optParameter) {
58             return parensTemplate.replaceAll("#P", optParameter.equals("OPEN") ? "(" : ")");
59         }
60     }
61 
62     public static void main(String... args) {
63         new ComboTestHelper<T8204610>()
64                 .withArrayDimension("PAREN", (x, pk, idx) -> x.parenKinds[idx] = pk, 3, ParenKind.values())
65                 .run(T8204610::new);
66     }
67 
68     ParenKind[] parenKinds = new ParenKind[3];
69 
70     @Override
71     public void doWork() {
72         newCompilationTask()
73                 .withSourceFromTemplate(bodyTemplate)
74                 .analyze(this::check);
75     }
76 
77     String bodyTemplate = "class Test {\n" +
78                           "   final int x;\n" +
79                           "   Test() {\n" +
80                           "      #{PAREN[0].OPEN} #{PAREN[1].OPEN} this #{PAREN[1].CLOSE} . #{PAREN[2].OPEN} x #{PAREN[2].CLOSE} #{PAREN[0].CLOSE} = 1;\n" +
81                           "   } }";
82 
83     void check(Result<?> res) {
84         boolean expectedFail = parenKinds[2] != ParenKind.NONE;
85         if (expectedFail != res.hasErrors()) {
86             fail("unexpected compilation result for source:\n" +
87                 res.compilationInfo());
88         }
89     }
90 }