1 /* 2 * Copyright (c) 2015, 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 8082311 8129962 27 * @summary Verify that bitwise operators don't allow to mix numeric and boolean operands. 28 * @enablePreview 29 * @library ../lib 30 * @modules jdk.compiler/com.sun.tools.javac.api 31 * jdk.compiler/com.sun.tools.javac.file 32 * jdk.compiler/com.sun.tools.javac.util 33 * @build combo.ComboTestHelper 34 * @run main BitWiseOperators 35 */ 36 37 import com.sun.tools.javac.util.StringUtils; 38 39 import java.io.IOException; 40 41 import combo.ComboInstance; 42 import combo.ComboParameter; 43 import combo.ComboTask.Result; 44 import combo.ComboTestHelper; 45 46 47 public class BitWiseOperators extends ComboInstance<BitWiseOperators> { 48 49 enum OperandType implements ComboParameter { 50 BYTE, 51 CHAR, 52 SHORT, 53 INT, 54 LONG, 55 BOOLEAN; 56 57 public static boolean compatible(OperandType op1, OperandType op2) { 58 return !(op1 == BOOLEAN ^ op2 == BOOLEAN); 59 } 60 61 @Override 62 public String expand(String optParameter) { 63 return StringUtils.toLowerCase(name()); 64 } 65 } 66 67 enum OperatorKind implements ComboParameter { 68 BITAND("&"), 69 BITOR("|"), 70 BITXOR("^"); 71 72 String op; 73 74 OperatorKind(String op) { 75 this.op = op; 76 } 77 78 @Override 79 public String expand(String optParameter) { 80 return op; 81 } 82 } 83 84 public static void main(String... args) { 85 new ComboTestHelper<BitWiseOperators>() 86 .withArrayDimension("TYPE", (x, type, idx) -> x.opTypes[idx] = type, 2, OperandType.values()) 87 .withDimension("OP", OperatorKind.values()) 88 .run(BitWiseOperators::new); 89 } 90 91 OperandType[] opTypes = new OperandType[2]; 92 93 String template = "class Test {\n" + 94 " public Object test(#{TYPE[0]} var1, #{TYPE[1]} var2) {\n" + 95 " return var1 #{OP} var2;\n" + 96 " }\n" + 97 "}"; 98 99 @Override 100 public void doWork() throws IOException { 101 newCompilationTask() 102 .withSourceFromTemplate(template) 103 .analyze(res -> { 104 if (res.hasErrors() == OperandType.compatible(opTypes[0], opTypes[1])) { 105 fail("Unexpected behavior. Type1: " + opTypes[0] + 106 "; type2: " + opTypes[1] + 107 "; " + res.compilationInfo()); 108 } 109 }); 110 } 111 }