1 /*
  2  * Copyright (c) 2023, 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 8304487
 27  * @summary Compiler Implementation for Primitive types in patterns, instanceof, and switch (Preview)
 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 PrimitiveInstanceOfComboTest.java
 37  * @run main PrimitiveInstanceOfComboTest
 38  */
 39 
 40 import combo.ComboInstance;
 41 import combo.ComboParameter;
 42 import combo.ComboTask;
 43 import combo.ComboTestHelper;
 44 import toolbox.ToolBox;
 45 
 46 import javax.tools.Diagnostic;
 47 import javax.tools.JavaFileObject;
 48 import java.util.List;
 49 
 50 public class PrimitiveInstanceOfComboTest extends ComboInstance<PrimitiveInstanceOfComboTest> {
 51     private static final String JAVA_VERSION = System.getProperty("java.specification.version");
 52 
 53     protected ToolBox tb;
 54 
 55     PrimitiveInstanceOfComboTest() {
 56         super();
 57         tb = new ToolBox();
 58     }
 59 
 60     public static void main(String... args) throws Exception {
 61         new ComboTestHelper<PrimitiveInstanceOfComboTest>()
 62                 .withDimension("TYPE1", (x, type1) -> x.type1 = type1, Type.values())
 63                 .withDimension("TYPE2", (x, type2) -> x.type2 = type2, Type.values())
 64                 .withFailMode(ComboTestHelper.FailMode.FAIL_FAST)
 65                 .run(PrimitiveInstanceOfComboTest::new);
 66     }
 67 
 68     private Type type1;
 69     private Type type2;
 70 
 71     private static final String test1 =
 72             """
 73             public class Test {
 74                 public static void doTest(#{TYPE1} in) {
 75                     var r = (#{TYPE2}) in;
 76                 }
 77             }
 78             """;
 79 
 80     private static final String test2 =
 81             """
 82             public class Test {
 83                 public static void doTest(#{TYPE1} in) {
 84                     var r = in instanceof #{TYPE2};
 85                 }
 86             }
 87             """;
 88 
 89     // potential not-exhaustive errors are expected and filtered out in `doWork`
 90     private static final String test3 =
 91             """
 92             public class Test {
 93                 public static void doTest(#{TYPE1} in) {
 94                     switch(in) {
 95                        case #{TYPE2} x -> {}
 96                     }
 97                 }
 98             }
 99             """;
100 
101     @Override
102     protected void doWork() throws Throwable {
103         ComboTask task1 = newCompilationTask()
104                 .withSourceFromTemplate(test1.replace("#{TYPE1}", type1.code).replace("#{TYPE2}", type2.code))
105                 .withOption("--enable-preview")
106                 .withOption("-source").withOption(JAVA_VERSION);
107 
108         ComboTask task2 = newCompilationTask()
109                 .withSourceFromTemplate(test2.replace("#{TYPE1}", type1.code).replace("#{TYPE2}", type2.code))
110                 .withOption("--enable-preview")
111                 .withOption("-source").withOption(JAVA_VERSION);
112 
113         ComboTask task3 = newCompilationTask()
114                 .withSourceFromTemplate(test3.replace("#{TYPE1}", type1.code).replace("#{TYPE2}", type2.code))
115                 .withOption("--enable-preview")
116                 .withOption("-source").withOption(JAVA_VERSION);
117 
118         task1.generate(result1 -> {
119             task2.generate(result2 -> {
120                 task3.generate(result3 -> {
121                     List<Diagnostic<? extends JavaFileObject>> list1 = result1.diagnosticsForKind(Diagnostic.Kind.ERROR);
122                     List<Diagnostic<? extends JavaFileObject>> list2 = result2.diagnosticsForKind(Diagnostic.Kind.ERROR);
123                     List<Diagnostic<? extends JavaFileObject>> list3 = result3.diagnosticsForKind(Diagnostic.Kind.ERROR).stream().filter(e -> !e.getCode().equals("compiler.err.not.exhaustive.statement")).toList();
124                     if (!(list1.size() == list2.size() && list3.size() == list2.size())) {
125                         throw new AssertionError("Unexpected result: " +
126                                 "\n task1: " + result1.hasErrors() + ", info: " + result1.compilationInfo() +
127                                 "\n task2: " + result2.hasErrors() + ", info: " + result2.compilationInfo() +
128                                 "\n task3: " + result3.hasErrors() + ", info: " + result3.compilationInfo()
129                         );
130                     }
131                 });
132             });
133         });
134     }
135 
136     public enum Type implements ComboParameter {
137         BYTE("byte"),
138         SHORT("short"),
139         CHAR("char"),
140         INT("int"),
141         LONG("long"),
142         FLOAT("float"),
143         DOUBLE("double"),
144         BOOLEAN("boolean"),
145 
146         BYTE_r("Byte"),
147         SHORT_r("Short"),
148         CHAR_r("Character"),
149         INTEGER_r("Integer"),
150         LONG_r("Long"),
151         FLOAT_r("Float"),
152         DOUBLE_r("Double"),
153         BOOLEAN_r("Boolean");
154 
155         private final String code;
156 
157         Type(String code) {
158             this.code = code;
159         }
160 
161         @Override
162         public String expand(String optParameter) {
163             throw new UnsupportedOperationException("Not supported.");
164         }
165     }
166 }