test/tools/javac/types/TestComparisons.java

Thu, 31 Aug 2017 15:17:03 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:17:03 +0800
changeset 2525
2eb010b6cb22
parent 0
959103a6100f
permissions
-rw-r--r--

merge

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

mercurial