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 8276836
 27  * @summary Check that switch expression with no value does not crash the compiler.
 28  * @enablePreview
 29  * @library /tools/lib /tools/javac/lib
 30  * @modules
 31  *      java.base/jdk.internal
 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 SwitchExpressionNoValue.java
 39  * @run main/othervm SwitchExpressionNoValue
 40  */
 41 
 42 import combo.ComboInstance;
 43 import combo.ComboParameter;
 44 import combo.ComboTask;
 45 import combo.ComboTestHelper;
 46 import java.io.InputStream;
 47 import java.lang.reflect.InvocationTargetException;
 48 import java.nio.file.Path;
 49 import java.nio.file.Paths;
 50 import java.util.Iterator;
 51 import java.util.Objects;
 52 import javax.tools.Diagnostic;
 53 import toolbox.ToolBox;
 54 
 55 import javax.tools.JavaFileObject;
 56 
 57 public class SwitchExpressionNoValue extends ComboInstance<SwitchExpressionNoValue> {
 58     protected ToolBox tb;
 59 
 60     SwitchExpressionNoValue() {
 61         super();
 62         tb = new ToolBox();
 63     }
 64 
 65     public static void main(String... args) throws Exception {
 66         new ComboTestHelper<SwitchExpressionNoValue>()
 67                 .withDimension("SWITCH_EXPRESSION", (x, method) -> x.switchExpression = method, SwitchExpression.values())
 68                 .withDimension("EXPRESSION", (x, expression) -> x.expression = expression, Expression.values())
 69                 .withDimension("CONTEXT", (x, context) -> x.context = context, Context.values())
 70                 .withFilter(test -> test.context.expressionType == test.expression.expressionType &&
 71                                     test.context.expressionType == test.switchExpression.expressionType)
 72                 .run(SwitchExpressionNoValue::new);
 73     }
 74 
 75     private SwitchExpression switchExpression;
 76     private Expression expression;
 77     private Context context;
 78 
 79     private static final String MAIN_TEMPLATE =
 80             """
 81             public class Test {
 82                 public static void doTest() {
 83                     #{CONTEXT}
 84                 }
 85                 static int i;
 86                 static int[] arr = new int[0];
 87                 static void m(int i, Object o, int j) {}
 88             }
 89             """;
 90 
 91     @Override
 92     protected void doWork() throws Throwable {
 93         Path base = Paths.get(".");
 94 
 95         ComboTask task = newCompilationTask()
 96                 .withSourceFromTemplate(MAIN_TEMPLATE, pname -> switch (pname) {
 97                         case "SWITCH_EXPRESSION" -> switchExpression;
 98                         case "EXPRESSION" -> expression;
 99                         case "CONTEXT" -> context;
100                         default -> throw new UnsupportedOperationException(pname);
101                     });
102 
103         task.generate(result -> {
104             try {
105                 if (result.hasErrors()) {
106                     throw new AssertionError(result.diagnosticsForKind(Diagnostic.Kind.ERROR));
107                 }
108                 Iterator<? extends JavaFileObject> filesIt = result.get().iterator();
109                 JavaFileObject file = filesIt.next();
110                 if (filesIt.hasNext()) {
111                     throw new IllegalStateException("More than one classfile returned!");
112                 }
113                 byte[] data;
114                 try (InputStream input = file.openInputStream()) {
115                     data = input.readAllBytes();
116                 }
117                 ClassLoader inMemoryLoader = new ClassLoader() {
118                     protected Class<?> findClass(String name) throws ClassNotFoundException {
119                         if ("Test".equals(name)) {
120                             return defineClass(name, data, 0, data.length);
121                         }
122                         return super.findClass(name);
123                     }
124                 };
125                 Class<?> test = Class.forName("Test", false, inMemoryLoader);
126                 try {
127                 java.lang.reflect.Method doTest = test.getDeclaredMethod("doTest");
128                     doTest.invoke(null);
129                     throw new AssertionError("No expected exception!");
130                 } catch (Throwable ex) {
131                     while (ex instanceof InvocationTargetException) {
132                         ex = ((InvocationTargetException) ex).getCause();
133                     }
134                     if (ex instanceof RuntimeException && "test".equals(ex.getMessage())) {
135                         //OK
136                     } else {
137                         throw new IllegalStateException(ex);
138                     }
139                 }
140             } catch (Throwable ex) {
141                 throw new IllegalStateException(ex);
142             }
143         });
144     }
145 
146     private void assertEquals(Object o1, Object o2) {
147         if (!Objects.equals(o1, o2)) {
148             throw new AssertionError();
149         }
150     }
151 
152     public enum SwitchExpression implements ComboParameter {
153         INT("switch (i) { case 0 -> throw new RuntimeException(\"test\"); default -> {if (true) throw new RuntimeException(\"test\"); else yield 0; } }", ExpressionType.INT),
154         BOOLEAN("switch (i) { case 0 -> throw new RuntimeException(\"test\"); default -> {if (true) throw new RuntimeException(\"test\"); else yield true; } }", ExpressionType.BOOLEAN)
155         ;
156         private final String expression;
157         private final ExpressionType expressionType;
158 
159         private SwitchExpression(String expression, ExpressionType expressionType) {
160             this.expression = expression;
161             this.expressionType = expressionType;
162         }
163 
164         @Override
165         public String expand(String optParameter) {
166             return expression;
167         }
168     }
169 
170     public enum Expression implements ComboParameter {
171         SIMPLE("#{SWITCH_EXPRESSION}", ExpressionType.INT),
172         BINARY_SIMPLE("3 + #{SWITCH_EXPRESSION}", ExpressionType.INT),
173         BINARY_LONGER1("3 + #{SWITCH_EXPRESSION} + #{SWITCH_EXPRESSION} + #{SWITCH_EXPRESSION}", ExpressionType.INT),
174         BINARY_LONGER2("3 + switch (0) { default -> 0; } + #{SWITCH_EXPRESSION} + #{SWITCH_EXPRESSION}", ExpressionType.INT),
175         BINARY_LONGER3("3 + #{SWITCH_EXPRESSION} + switch (0) { default -> 0; } + #{SWITCH_EXPRESSION}", ExpressionType.INT),
176         BINARY_BOOLEAN("\"\".isEmpty() && #{SWITCH_EXPRESSION}", ExpressionType.BOOLEAN),
177         ;
178         private final String expression;
179         private final ExpressionType expressionType;
180 
181         private Expression(String expression, ExpressionType expressionType) {
182             this.expression = expression;
183             this.expressionType = expressionType;
184         }
185 
186         @Override
187         public String expand(String optParameter) {
188             return expression;
189         }
190     }
191 
192     public enum Context implements ComboParameter {
193         ASSIGNMENT("i = #{EXPRESSION};", ExpressionType.INT),
194         COMPOUND_ASSIGNMENT("i += #{EXPRESSION};", ExpressionType.INT),
195         METHOD_INVOCATION("m(0, #{EXPRESSION}, 0);", ExpressionType.INT),
196         ARRAY_DEREF("arr[#{EXPRESSION}] = 0;", ExpressionType.INT),
197         IF("if (#{EXPRESSION});", ExpressionType.BOOLEAN),
198         WHILE("while (#{EXPRESSION});", ExpressionType.BOOLEAN)
199         ;
200         private final String code;
201         private final ExpressionType expressionType;
202         private Context(String code, ExpressionType expressionType) {
203             this.code = code;
204             this.expressionType = expressionType;
205         }
206         @Override
207         public String expand(String optParameter) {
208             return code;
209         }
210     }
211 
212     enum ExpressionType {
213         INT,
214         BOOLEAN;
215     }
216 }