1 /*
  2  * Copyright (c) 2022, 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 8292756
 27  * @summary Verify the Scope can be safely and correctly resized to accommodate pattern binding variables
 28  *          when the Scope for a guard is constructed.
 29  * @enablePreview
 30  * @library /tools/lib /tools/javac/lib
 31  * @modules
 32  *      jdk.compiler/com.sun.tools.javac.api
 33  *      jdk.compiler/com.sun.tools.javac.file
 34  *      jdk.compiler/com.sun.tools.javac.main
 35  *      jdk.compiler/com.sun.tools.javac.util
 36  * @build toolbox.ToolBox toolbox.JavacTask
 37  * @build combo.ComboTestHelper
 38  * @compile ScopeResizeTest.java
 39  * @run main ScopeResizeTest
 40  */
 41 
 42 import combo.ComboInstance;
 43 import combo.ComboParameter;
 44 import combo.ComboTask;
 45 import combo.ComboTestHelper;
 46 import java.util.stream.Stream;
 47 import toolbox.ToolBox;
 48 
 49 public class ScopeResizeTest extends ComboInstance<ScopeResizeTest> {
 50     protected ToolBox tb;
 51 
 52     ScopeResizeTest() {
 53         super();
 54         tb = new ToolBox();
 55     }
 56 
 57     public static void main(String... args) throws Exception {
 58         int variantsSize = 17;
 59         PredefinedVariables[] variants = Stream.iterate(0, i -> i + 1)
 60                                                .limit(variantsSize)
 61                                                .map(s -> new PredefinedVariables(s))
 62                                                .toArray(s -> new PredefinedVariables[s]);
 63         new ComboTestHelper<ScopeResizeTest>()
 64                 .withDimension("PREDEFINED_VARIABLES", (x, predefinedVariables) -> x.predefinedVariables = predefinedVariables, variants)
 65                 .run(ScopeResizeTest::new);
 66     }
 67 
 68     private PredefinedVariables predefinedVariables;
 69 
 70     private static final String MAIN_TEMPLATE =
 71             """
 72             public class Test {
 73                 public static void test(Object o) {
 74                     #{PREDEFINED_VARIABLES}
 75                     switch (o) {
 76                         case String s when s.isEmpty() -> {}
 77                         default -> {}
 78                     }
 79                 }
 80             }
 81             """;
 82 
 83     @Override
 84     protected void doWork() throws Throwable {
 85         ComboTask task = newCompilationTask()
 86                 .withSourceFromTemplate(MAIN_TEMPLATE, pname -> switch (pname) {
 87                         case "PREDEFINED_VARIABLES" -> predefinedVariables;
 88                         default -> throw new UnsupportedOperationException(pname);
 89                     });
 90 
 91         task.analyze(result -> {});
 92     }
 93 
 94     public record PredefinedVariables(int size) implements ComboParameter {
 95         @Override
 96         public String expand(String optParameter) {
 97             StringBuilder variables = new StringBuilder();
 98             for (int i = 0; i < size(); i++) {
 99                 variables.append("int i" + i + ";\n");
100             }
101             return variables.toString();
102         }
103     }
104 
105 }