1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
|
/*
* Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 8013357
* @summary javac should correctly enforce binary comparison rules.
* @modules jdk.compiler
*/
import java.io.*;
public class TestComparisons {
private int errors = 0;
private int testnum = 0;
static final File testdir = new File("8013357");
private enum CompareType {
BYTE_PRIM("byte"),
SHORT_PRIM("short"),
CHAR_PRIM("char"),
INTEGER_PRIM("int"),
LONG_PRIM("long"),
FLOAT_PRIM("float"),
DOUBLE_PRIM("double"),
BOOLEAN_PRIM("boolean"),
BYTE("Byte"),
SHORT("Short"),
CHAR("Character"),
INTEGER("Integer"),
LONG("Long"),
FLOAT("Float"),
DOUBLE("Double"),
BOOLEAN("Boolean"),
BYTE_SUPER("List<? super Byte>", true),
SHORT_SUPER("List<? super Short>", true),
CHAR_SUPER("List<? super Character>", true),
INTEGER_SUPER("List<? super Integer>", true),
LONG_SUPER("List<? super Long>", true),
FLOAT_SUPER("List<? super Float>", true),
DOUBLE_SUPER("List<? super Double>", true),
BOOLEAN_SUPER("List<? super Boolean>", true),
OBJECT("Object"),
NUMBER("Number"),
STRING("String");
public final boolean isList;
public final String name;
private CompareType(final String name, final boolean isList) {
this.isList = isList;
this.name = name;
}
private CompareType(final String name) {
this(name, false);
}
}
// The integers here refer to which subsection of JLS 15.21 is in
// effect. 0 means no comparison is allowed.
private static final int truthtab[][] = {
// byte, comparable to itself, any numeric type, or any boxed
// numeric type.
{ 1, 1, 1, 1, 1, 1, 1, 0, // Primitives
1, 1, 1, 1, 1, 1, 1, 0, // Boxed primitives
0, 0, 0, 0, 0, 0, 0, 0, // Captures
0, 0, 0 // Reference types
},
// short, comparable to itself, any numeric type, or any boxed
// numeric type.
{ 1, 1, 1, 1, 1, 1, 1, 0, // Primitives
1, 1, 1, 1, 1, 1, 1, 0, // Boxed primitives
0, 0, 0, 0, 0, 0, 0, 0, // Captures
0, 0, 0 // Reference types
},
// char, comparable to itself, any numeric type, or any boxed
// numeric type.
{ 1, 1, 1, 1, 1, 1, 1, 0, // Primitives
1, 1, 1, 1, 1, 1, 1, 0, // Boxed primitives
0, 0, 0, 0, 0, 0, 0, 0, // Captures
0, 0, 0 // Reference types
},
// int, comparable to itself, any numeric type, or any boxed
// numeric type.
{ 1, 1, 1, 1, 1, 1, 1, 0, // Primitives
1, 1, 1, 1, 1, 1, 1, 0, // Boxed primitives
0, 0, 0, 0, 0, 0, 0, 0, // Captures
0, 0, 0 // Reference types
},
// long, comparable to itself, any numeric type, or any boxed
// numeric type.
{ 1, 1, 1, 1, 1, 1, 1, 0, // Primitives
1, 1, 1, 1, 1, 1, 1, 0, // Boxed primitives
0, 0, 0, 0, 0, 0, 0, 0, // Captures
0, 0, 0 // Reference types
},
// float, comparable to itself, any numeric type, or any boxed
// numeric type.
{ 1, 1, 1, 1, 1, 1, 1, 0, // Primitives
1, 1, 1, 1, 1, 1, 1, 0, // Boxed primitives
0, 0, 0, 0, 0, 0, 0, 0, // Captures
0, 0, 0 // Reference types
},
// double, comparable to itself, any numeric type, or any boxed
// numeric type.
{ 1, 1, 1, 1, 1, 1, 1, 0, // Primitives
1, 1, 1, 1, 1, 1, 1, 0, // Boxed primitives
0, 0, 0, 0, 0, 0, 0, 0, // Captures
0, 0, 0 // Reference types
},
// boolean, comparable only to itself and Boolean.
{ 0, 0, 0, 0, 0, 0, 0, 2, // Primitives
0, 0, 0, 0, 0, 0, 0, 2, // Boxed primitives
0, 0, 0, 0, 0, 0, 0, 0, // Captures
0, 0, 0 // Reference types
},
// Byte, comparable to itself, Number, Object, any numeric primitive,
// and any captures.
{ 1, 1, 1, 1, 1, 1, 1, 0, // Primitives
3, 0, 0, 0, 0, 0, 0, 0, // Boxed primitives
3, 3, 3, 3, 3, 3, 3, 3, // Captures
3, 3, 0 // Reference types
},
// Short, comparable to itself, Number, Object, any numeric primitive,
// and any captures.
{ 1, 1, 1, 1, 1, 1, 1, 0, // Primitives
0, 3, 0, 0, 0, 0, 0, 0, // Boxed primitives
3, 3, 3, 3, 3, 3, 3, 3, // Captures
3, 3, 0 // Reference types
},
// Character, comparable to itself, Object, any numeric primitive,
// and any captures.
{ 1, 1, 1, 1, 1, 1, 1, 0, // Primitives
0, 0, 3, 0, 0, 0, 0, 0, // Boxed primitives
3, 3, 3, 3, 3, 3, 3, 3, // Captures
3, 0, 0 // Reference types
},
// Int, comparable to itself, Number, Object, any numeric primitive,
// and any captures.
{ 1, 1, 1, 1, 1, 1, 1, 0, // Primitives
0, 0, 0, 3, 0, 0, 0, 0, // Boxed primitives
3, 3, 3, 3, 3, 3, 3, 3, // Captures
3, 3, 0 // Reference types
},
// Long, comparable to itself, Number, Object, any numeric primitive,
// and any captures.
{ 1, 1, 1, 1, 1, 1, 1, 0, // Primitives
0, 0, 0, 0, 3, 0, 0, 0, // Boxed primitives
3, 3, 3, 3, 3, 3, 3, 3, // Captures
3, 3, 0 // Reference types
},
// Float, comparable to itself, Number, Object, any numeric primitive,
// and any captures.
{ 1, 1, 1, 1, 1, 1, 1, 0, // Primitives
0, 0, 0, 0, 0, 3, 0, 0, // Boxed primitives
3, 3, 3, 3, 3, 3, 3, 3, // Captures
3, 3, 0 // Reference types
},
// Double, comparable to itself, Number, Object, any numeric primitive,
// and any captures.
{ 1, 1, 1, 1, 1, 1, 1, 0, // Primitives
0, 0, 0, 0, 0, 0, 3, 0, // Boxed primitives
3, 3, 3, 3, 3, 3, 3, 3, // Captures
3, 3, 0 // Reference types
},
// Boolean, to itself, any capture, Object, and boolean.
{ 0, 0, 0, 0, 0, 0, 0, 2, // Primitives
0, 0, 0, 0, 0, 0, 0, 2, // Boxed primitives
3, 3, 3, 3, 3, 3, 3, 3, // Captures
3, 0, 0 // Reference types
},
// Byte supertype wildcard, comparable to any reference type.
// and any captures.
{ 0, 0, 0, 0, 0, 0, 0, 0, // Primitives
3, 3, 3, 3, 3, 3, 3, 3, // Boxed primitives
3, 3, 3, 3, 3, 3, 3, 3, // Captures
3, 3, 3 // Reference types
},
// Short supertype wildcard, comparable to any reference type.
// and any captures.
{ 0, 0, 0, 0, 0, 0, 0, 0, // Primitives
3, 3, 3, 3, 3, 3, 3, 3, // Boxed primitives
3, 3, 3, 3, 3, 3, 3, 3, // Captures
3, 3, 3 // Reference types
},
// Character supertype wildcard, comparable to any reference type.
// and any captures.
{ 0, 0, 0, 0, 0, 0, 0, 0, // Primitives
3, 3, 3, 3, 3, 3, 3, 3, // Boxed primitives
3, 3, 3, 3, 3, 3, 3, 3, // Captures
3, 3, 3 // Reference types
},
// Integer supertype wildcard, comparable to any reference type.
// and any captures.
{ 0, 0, 0, 0, 0, 0, 0, 0, // Primitives
3, 3, 3, 3, 3, 3, 3, 3, // Boxed primitives
3, 3, 3, 3, 3, 3, 3, 3, // Captures
3, 3, 3 // Reference types
},
// Long supertype wildcard, comparable to any reference type.
// and any captures.
{ 0, 0, 0, 0, 0, 0, 0, 0, // Primitives
3, 3, 3, 3, 3, 3, 3, 3, // Boxed primitives
3, 3, 3, 3, 3, 3, 3, 3, // Captures
3, 3, 3 // Reference types
},
// Float supertype wildcard, comparable to any reference type.
// and any captures.
{ 0, 0, 0, 0, 0, 0, 0, 0, // Primitives
3, 3, 3, 3, 3, 3, 3, 3, // Boxed primitives
3, 3, 3, 3, 3, 3, 3, 3, // Captures
3, 3, 3 // Reference types
},
// Double supertype wildcard, comparable to any reference type.
// and any captures.
{ 0, 0, 0, 0, 0, 0, 0, 0, // Primitives
3, 3, 3, 3, 3, 3, 3, 3, // Boxed primitives
3, 3, 3, 3, 3, 3, 3, 3, // Captures
3, 3, 3 // Reference types
},
// Boolean supertype wildcard, comparable to any reference type.
// and any captures.
{ 0, 0, 0, 0, 0, 0, 0, 0, // Primitives
3, 3, 3, 3, 3, 3, 3, 3, // Boxed primitives
3, 3, 3, 3, 3, 3, 3, 3, // Captures
3, 3, 3 // Reference types
},
// Object, comparable to any reference type.
// and any captures.
{ 0, 0, 0, 0, 0, 0, 0, 0, // Primitives
3, 3, 3, 3, 3, 3, 3, 3, // Boxed primitives
3, 3, 3, 3, 3, 3, 3, 3, // Captures
3, 3, 3 // Reference types
},
// Number, comparable to Object, any of its subclasses.
// and any captures.
{ 0, 0, 0, 0, 0, 0, 0, 0, // Primitives
3, 3, 0, 3, 3, 3, 3, 0, // Boxed primitives
3, 3, 3, 3, 3, 3, 3, 3, // Captures
3, 3, 0 // Reference types
},
// String supertype wildcard, comparable to any reference type.
// and any captures.
{ 0, 0, 0, 0, 0, 0, 0, 0, // Primitives
0, 0, 0, 0, 0, 0, 0, 0, // Boxed primitives
3, 3, 3, 3, 3, 3, 3, 3, // Captures
3, 0, 3 // Reference types
}
};
private void assert_compile_fail(final File file, final String body) {
final String filename = file.getPath();
final String[] args = { filename };
final StringWriter sw = new StringWriter();
final PrintWriter pw = new PrintWriter(sw);
final int rc = com.sun.tools.javac.Main.compile(args, pw);
pw.close();
if (rc == 0) {
System.err.println("Compilation of " + file.getName() +
" didn't fail as expected.\nFile:\n" +
body + "\nOutput:\n" + sw.toString());
errors++;
}
}
private void assert_compile_succeed(final File file, final String body) {
final String filename = file.getPath();
final String[] args = { filename };
final StringWriter sw = new StringWriter();
final PrintWriter pw = new PrintWriter(sw);
final int rc = com.sun.tools.javac.Main.compile(args, pw);
pw.close();
if (rc != 0) {
System.err.println("Compilation of " + file.getName() +
" didn't succeed as expected.\nFile:\n" +
body + "\nOutput:\n" +
sw.toString());
errors++;
}
}
private String makeBody(final int num,
final CompareType left,
final CompareType right) {
return "import java.util.List;\n" +
"public class Test" + num + " {\n" +
" public boolean test(" + left.name +
" left, " + right.name + " right) {\n" +
" return left" + (left.isList ? ".get(0)" : "") +
" == right" + (right.isList ? ".get(0)" : "") + ";\n" +
" }\n" +
"}\n";
}
private File writeFile(final String filename,
final String body)
throws IOException {
final File f = new File(testdir, filename);
f.getParentFile().mkdirs();
final FileWriter out = new FileWriter(f);
out.write(body);
out.close();
return f;
}
private void test(final CompareType left, final CompareType right)
throws IOException {
final int num = testnum++;
final String filename = "Test" + num + ".java";
final String body = makeBody(num, left, right);
final File file = writeFile(filename, body);
if (truthtab[left.ordinal()][right.ordinal()] != 0)
assert_compile_succeed(file, body);
else
assert_compile_fail(file, body);
}
void run() throws Exception {
testdir.mkdir();
for(CompareType left : CompareType.values())
for(CompareType right : CompareType.values())
test(left, right);
if (errors != 0)
throw new Exception("ObjectZeroCompare test failed with " +
errors + " errors.");
}
public static void main(String... args) throws Exception {
new TestComparisons().run();
}
}
|