langtools/test/tools/javac/resolve/BitWiseOperators.java
author jlahoda
Tue, 09 Jun 2015 11:52:13 +0200
changeset 31114 d4045e8aaf1e
child 32454 b0ac04e0fefe
permissions -rw-r--r--
8082311: NPE when compiling expression with \"^\" Summary: Using BinaryNumericOperator for numeric bitwise operators and BinaryBooleanOperator for boolean bitwise operators, as the common BinaryBitwiseOperator allowed to incorrectly combine numeric and boolean operands Reviewed-by: mcimadamore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
31114
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
     1
/*
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
     4
 *
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
     7
 * published by the Free Software Foundation.
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
     8
 *
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    13
 * accompanied this code).
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    14
 *
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    18
 *
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    21
 * questions.
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    22
 */
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    23
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    24
/**@test
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    25
 * @bug 8082311
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    26
 * @summary Verify that bitwise operators don't allow to mix numeric and boolean operands.
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    27
 * @library ../lib
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    28
 */
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    29
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    30
import com.sun.tools.javac.util.StringUtils;
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    31
import java.net.URI;
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    32
import java.util.Arrays;
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    33
import java.util.List;
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    34
import javax.tools.DiagnosticCollector;
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    35
import javax.tools.JavaFileObject;
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    36
import javax.tools.SimpleJavaFileObject;
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    37
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    38
public class BitWiseOperators extends JavacTestingAbstractThreadedTest {
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    39
    public static void main(String... args) {
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    40
        new BitWiseOperators().run();
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    41
    }
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    42
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    43
    void run() {
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    44
        for (TYPE type1 : TYPE.values()) {
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    45
            for (OPERATION op : OPERATION.values()) {
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    46
                for (TYPE type2 : TYPE.values()) {
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    47
                    runTest(type1, op, type2);
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    48
                }
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    49
            }
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    50
        }
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    51
    }
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    52
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    53
    void runTest(TYPE type1, OPERATION op, TYPE type2) {
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    54
        DiagnosticCollector<JavaFileObject> dc = new DiagnosticCollector<>();
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    55
        List<JavaSource> files = Arrays.asList(new JavaSource(type1, op, type2));
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    56
        comp.getTask(null, null, dc, null, null, files).call();
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    57
        if (dc.getDiagnostics().isEmpty() ^ TYPE.compatible(type1, type2)) {
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    58
            throw new AssertionError("Unexpected behavior. Type1: " + type1 +
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    59
                                                        "; type2: " + type2 +
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    60
                                                        "; diagnostics: " + dc.getDiagnostics());
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    61
        }
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    62
    }
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    63
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    64
    enum TYPE {
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    65
        BYTE,
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    66
        CHAR,
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    67
        SHORT,
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    68
        INT,
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    69
        LONG,
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    70
        BOOLEAN;
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    71
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    72
        public static boolean compatible(TYPE op1, TYPE op2) {
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    73
            return !(op1 == BOOLEAN ^ op2 == BOOLEAN);
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    74
        }
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    75
    }
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    76
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    77
    enum OPERATION {
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    78
        BITAND("&"),
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    79
        BITOR("|"),
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    80
        BITXOR("^");
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    81
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    82
        String op;
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    83
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    84
        private OPERATION(String op) {
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    85
            this.op = op;
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    86
        }
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    87
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    88
    }
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    89
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    90
    class JavaSource extends SimpleJavaFileObject {
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    91
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    92
        String template = "class Test {\n" +
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    93
                          "    public Object test(#TYPE1 var1, #TYPE2 var2) {\n" +
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    94
                          "        return var1 #OP var2;\n" +
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    95
                          "    }\n" +
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    96
                          "}";
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    97
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    98
        String source;
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
    99
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
   100
        public JavaSource(TYPE type1, OPERATION op, TYPE type2) {
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
   101
            super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
   102
            source = template.replaceAll("#TYPE1", StringUtils.toLowerCase(type1.name()))
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
   103
                             .replaceAll("#OP", StringUtils.toLowerCase(op.op))
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
   104
                             .replaceAll("#TYPE2", StringUtils.toLowerCase(type2.name()));
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
   105
        }
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
   106
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
   107
        @Override
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
   108
        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
   109
            return source;
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
   110
        }
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
   111
    }
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
   112
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents:
diff changeset
   113
}