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