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