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