1 /*
  2  * Copyright (c) 2019, 2021, 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 8236670
 27  * @summary Verify proper scope of binding related to loops and breaks.
 28  * @library /tools/lib /tools/javac/lib
 29  * @modules
 30  *      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 ConditionalTest.java
 37  * @run main ConditionalTest
 38  */
 39 
 40 import combo.ComboInstance;
 41 import combo.ComboParameter;
 42 import combo.ComboTask;
 43 import combo.ComboTestHelper;
 44 import java.nio.file.Path;
 45 import java.nio.file.Paths;
 46 import toolbox.ToolBox;
 47 
 48 public class ConditionalTest extends ComboInstance<ConditionalTest> {
 49     protected ToolBox tb;
 50 
 51     ConditionalTest() {
 52         super();
 53         tb = new ToolBox();
 54     }
 55 
 56     public static void main(String... args) throws Exception {
 57         new ComboTestHelper<ConditionalTest>()
 58                 .withDimension("COND", (x, cond) -> x.cond = cond, Pattern.values())
 59                 .withDimension("TRUE", (x, trueSec) -> x.trueSec = trueSec, Pattern.values())
 60                 .withDimension("FALSE", (x, falseSec) -> x.falseSec = falseSec, Pattern.values())
 61                 .run(ConditionalTest::new);
 62     }
 63 
 64     private Pattern cond;
 65     private Pattern trueSec;
 66     private Pattern falseSec;
 67 
 68     private static final String MAIN_TEMPLATE =
 69             """
 70             public class Test {
 71                 public static boolean doTest(Object o, boolean b) {
 72                     return #{COND} ? #{TRUE} : #{FALSE}
 73                 }
 74             }
 75             """;
 76 
 77     @Override
 78     protected void doWork() throws Throwable {
 79         Path base = Paths.get(".");
 80 
 81         ComboTask task = newCompilationTask()
 82                 .withSourceFromTemplate(MAIN_TEMPLATE, pname -> switch (pname) {
 83                         case "COND" -> cond;
 84                         case "TRUE" -> trueSec;
 85                         case "FALSE" -> falseSec;
 86                         default -> throw new UnsupportedOperationException(pname);
 87                     });
 88 
 89         task.analyze(result -> {
 90             boolean shouldPass;
 91             if (cond == Pattern.TRUE && (trueSec == Pattern.TRUE || trueSec == Pattern.FALSE)) { //6 cases covered
 92                 shouldPass = false; //already in scope in true section
 93             } else if (cond == Pattern.FALSE && (falseSec == Pattern.TRUE || falseSec == Pattern.FALSE)) { //6 cases covered
 94                 shouldPass = false; //already in scope in false section
 95             } else if (cond == Pattern.TRUE && falseSec == Pattern.TRUE) {
 96                 shouldPass = false; //JLS 6.3.1.4
 97             } else if (cond == Pattern.FALSE && trueSec == Pattern.TRUE) {
 98                 shouldPass = false; //JLS 6.3.1.4
 99             } else if (trueSec == Pattern.TRUE && falseSec == Pattern.TRUE) {
100                 shouldPass = false; //JLS 6.3.1.4
101             } else if (trueSec == Pattern.TRUE && falseSec == Pattern.TRUE) {
102                 shouldPass = false; //JLS 6.3.1.4
103             } else if (cond == Pattern.TRUE && falseSec == Pattern.FALSE) {
104                 shouldPass = false; //JLS 6.3.1.4
105             } else if (cond == Pattern.FALSE && trueSec == Pattern.FALSE) {
106                 shouldPass = false; //JLS 6.3.1.4
107             } else if (trueSec == Pattern.FALSE && falseSec == Pattern.FALSE) {
108                 shouldPass = false; //JLS 6.3.1.4
109             } else {
110                 shouldPass = true;
111             }
112 
113             if (!shouldPass) {
114                 result.containsKey("Blabla");
115             }
116         });
117     }
118 
119     public enum Pattern implements ComboParameter {
120         NONE("b"),
121         TRUE("o instanceof String s"),
122         FALSE("!(o instanceof String s)");
123         private final String code;
124 
125         private Pattern(String code) {
126             this.code = code;
127         }
128 
129         @Override
130         public String expand(String optParameter) {
131             return code;
132         }
133     }
134 
135 }