1 /*
  2  * Copyright (c) 2020, 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 8243047
 27  * @summary javac should not crash while processing exits in class initializers in Flow
 28  * @enablePreview
 29  * @library /tools/lib /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.main
 33  *          jdk.compiler/com.sun.tools.javac.util
 34  * @build toolbox.ToolBox toolbox.JavacTask
 35  * @build combo.ComboTestHelper
 36  * @compile ClassBlockExits.java
 37  * @run main ClassBlockExits
 38  */
 39 
 40 import combo.ComboInstance;
 41 import combo.ComboParameter;
 42 import combo.ComboTask;
 43 import combo.ComboTestHelper;
 44 import java.io.StringWriter;
 45 import toolbox.ToolBox;
 46 
 47 public class ClassBlockExits extends ComboInstance<ClassBlockExits> {
 48     protected ToolBox tb;
 49 
 50     ClassBlockExits() {
 51         super();
 52         tb = new ToolBox();
 53     }
 54 
 55     public static void main(String... args) throws Exception {
 56         new ComboTestHelper<ClassBlockExits>()
 57                 .withDimension("BLOCK", (x, block) -> x.block = block, Block.values())
 58                 .withDimension("EXIT", (x, exit) -> x.exit = exit, Exit.values())
 59                 .run(ClassBlockExits::new);
 60     }
 61 
 62     private Block block;
 63     private Exit exit;
 64 
 65     private static final String MAIN_TEMPLATE =
 66             """
 67             public class Test {
 68                 #{BLOCK}
 69                 void t() {}
 70             }
 71             """;
 72 
 73     @Override
 74     protected void doWork() throws Throwable {
 75         StringWriter out = new StringWriter();
 76 
 77         ComboTask task = newCompilationTask()
 78                 .withSourceFromTemplate(MAIN_TEMPLATE, pname -> switch (pname) {
 79                         case "BLOCK" -> block;
 80                         case "BODY" -> exit;
 81                         default -> throw new UnsupportedOperationException(pname);
 82                     })
 83                 .withOption("-XDshould-stop.at=FLOW")
 84                 .withOption("-XDdev")
 85                 .withWriter(out);
 86 
 87         task.analyze(result -> {
 88             if (out.toString().length() > 0) {
 89                 throw new AssertionError("No output expected, but got" + out + "\n\n" + result.compilationInfo());
 90             }
 91         });
 92     }
 93 
 94     public enum Block implements ComboParameter {
 95         STATIC("""
 96                static {
 97                    #{BODY}
 98                }
 99                """),
100         INSTANCE("""
101                  {
102                      #{BODY}
103                  }
104                  """),
105         STATIC_INITIALIZER("""
106                     private static int i = switch (0) { default: #{BODY} case 0: yield 0; };
107                     """),
108         INITIALIZER("""
109                     private int i = switch (0) { default: #{BODY} case 0: yield 0; };
110                     """);
111         private final String block;
112 
113         private Block(String block) {
114             this.block = block;
115         }
116 
117         @Override
118         public String expand(String optParameter) {
119             return block;
120         }
121     }
122 
123     public enum Exit implements ComboParameter {
124         RETURN("return;"),
125         RETURN_VALUE("return null;"),
126         BREAK("break;"),
127         BREAK_LABEL("break LABEL;"),
128         CONTINUE("continue;"),
129         CONTINUE_LABEL("continue LABEL;"),
130         YIELD("yield 0;");
131         private final String code;
132 
133         private Exit(String code) {
134             this.code = code;
135         }
136 
137         @Override
138         public String expand(String optParameter) {
139             return code;
140         }
141     }
142 }