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