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