31114
|
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 |
/**@test
|
|
25 |
* @bug 8082311
|
|
26 |
* @summary Verify that bitwise operators don't allow to mix numeric and boolean operands.
|
|
27 |
* @library ../lib
|
|
28 |
*/
|
|
29 |
|
|
30 |
import com.sun.tools.javac.util.StringUtils;
|
|
31 |
import java.net.URI;
|
|
32 |
import java.util.Arrays;
|
|
33 |
import java.util.List;
|
|
34 |
import javax.tools.DiagnosticCollector;
|
|
35 |
import javax.tools.JavaFileObject;
|
|
36 |
import javax.tools.SimpleJavaFileObject;
|
|
37 |
|
|
38 |
public class BitWiseOperators extends JavacTestingAbstractThreadedTest {
|
|
39 |
public static void main(String... args) {
|
|
40 |
new BitWiseOperators().run();
|
|
41 |
}
|
|
42 |
|
|
43 |
void run() {
|
|
44 |
for (TYPE type1 : TYPE.values()) {
|
|
45 |
for (OPERATION op : OPERATION.values()) {
|
|
46 |
for (TYPE type2 : TYPE.values()) {
|
|
47 |
runTest(type1, op, type2);
|
|
48 |
}
|
|
49 |
}
|
|
50 |
}
|
|
51 |
}
|
|
52 |
|
|
53 |
void runTest(TYPE type1, OPERATION op, TYPE type2) {
|
|
54 |
DiagnosticCollector<JavaFileObject> dc = new DiagnosticCollector<>();
|
|
55 |
List<JavaSource> files = Arrays.asList(new JavaSource(type1, op, type2));
|
|
56 |
comp.getTask(null, null, dc, null, null, files).call();
|
|
57 |
if (dc.getDiagnostics().isEmpty() ^ TYPE.compatible(type1, type2)) {
|
|
58 |
throw new AssertionError("Unexpected behavior. Type1: " + type1 +
|
|
59 |
"; type2: " + type2 +
|
|
60 |
"; diagnostics: " + dc.getDiagnostics());
|
|
61 |
}
|
|
62 |
}
|
|
63 |
|
|
64 |
enum TYPE {
|
|
65 |
BYTE,
|
|
66 |
CHAR,
|
|
67 |
SHORT,
|
|
68 |
INT,
|
|
69 |
LONG,
|
|
70 |
BOOLEAN;
|
|
71 |
|
|
72 |
public static boolean compatible(TYPE op1, TYPE op2) {
|
|
73 |
return !(op1 == BOOLEAN ^ op2 == BOOLEAN);
|
|
74 |
}
|
|
75 |
}
|
|
76 |
|
|
77 |
enum OPERATION {
|
|
78 |
BITAND("&"),
|
|
79 |
BITOR("|"),
|
|
80 |
BITXOR("^");
|
|
81 |
|
|
82 |
String op;
|
|
83 |
|
|
84 |
private OPERATION(String op) {
|
|
85 |
this.op = op;
|
|
86 |
}
|
|
87 |
|
|
88 |
}
|
|
89 |
|
|
90 |
class JavaSource extends SimpleJavaFileObject {
|
|
91 |
|
|
92 |
String template = "class Test {\n" +
|
|
93 |
" public Object test(#TYPE1 var1, #TYPE2 var2) {\n" +
|
|
94 |
" return var1 #OP var2;\n" +
|
|
95 |
" }\n" +
|
|
96 |
"}";
|
|
97 |
|
|
98 |
String source;
|
|
99 |
|
|
100 |
public JavaSource(TYPE type1, OPERATION op, TYPE type2) {
|
|
101 |
super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
|
|
102 |
source = template.replaceAll("#TYPE1", StringUtils.toLowerCase(type1.name()))
|
|
103 |
.replaceAll("#OP", StringUtils.toLowerCase(op.op))
|
|
104 |
.replaceAll("#TYPE2", StringUtils.toLowerCase(type2.name()));
|
|
105 |
}
|
|
106 |
|
|
107 |
@Override
|
|
108 |
public CharSequence getCharContent(boolean ignoreEncodingErrors) {
|
|
109 |
return source;
|
|
110 |
}
|
|
111 |
}
|
|
112 |
|
|
113 |
}
|