test/tools/javac/generics/rawOverride/7062745/GenericOverrideTest.java

Sat, 07 Nov 2020 10:30:02 +0800

author
aoqi
date
Sat, 07 Nov 2020 10:30:02 +0800
changeset 3938
93012e2a5d1d
parent 2525
2eb010b6cb22
permissions
-rw-r--r--

Added tag mips-jdk8u275-b01 for changeset eb6ee6a5f2fe

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2011, 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 7062745 8006694
aoqi@0 27 * @summary Regression: difference in overload resolution when two methods
aoqi@0 28 * are maximally specific
aoqi@0 29 * temporarily workaround combo tests are causing time out in several platforms
aoqi@0 30 * @library ../../../lib
aoqi@0 31 * @build JavacTestingAbstractThreadedTest
aoqi@0 32 * @run main/othervm GenericOverrideTest
aoqi@0 33 */
aoqi@0 34
aoqi@0 35 // use /othervm to avoid jtreg timeout issues (CODETOOLS-7900047)
aoqi@0 36 // see JDK-8006746
aoqi@0 37
aoqi@0 38 import java.net.URI;
aoqi@0 39 import java.util.Arrays;
aoqi@0 40 import javax.tools.Diagnostic;
aoqi@0 41 import javax.tools.JavaFileObject;
aoqi@0 42 import javax.tools.SimpleJavaFileObject;
aoqi@0 43 import com.sun.source.util.JavacTask;
aoqi@0 44
aoqi@0 45 public class GenericOverrideTest
aoqi@0 46 extends JavacTestingAbstractThreadedTest
aoqi@0 47 implements Runnable {
aoqi@0 48
aoqi@0 49 enum SourceLevel {
aoqi@0 50 SOURCE_7("-source", "7"),
aoqi@0 51 SOURCE_DEFAULT();
aoqi@0 52
aoqi@0 53 String[] opts;
aoqi@0 54
aoqi@0 55 SourceLevel(String... opts) {
aoqi@0 56 this.opts = opts;
aoqi@0 57 }
aoqi@0 58 }
aoqi@0 59
aoqi@0 60 enum SignatureKind {
aoqi@0 61 NON_GENERIC(""),
aoqi@0 62 GENERIC("<X>");
aoqi@0 63
aoqi@0 64 String paramStr;
aoqi@0 65
aoqi@0 66 private SignatureKind(String paramStr) {
aoqi@0 67 this.paramStr = paramStr;
aoqi@0 68 }
aoqi@0 69 }
aoqi@0 70
aoqi@0 71 enum ReturnTypeKind {
aoqi@0 72 LIST("List"),
aoqi@0 73 ARRAYLIST("ArrayList");
aoqi@0 74
aoqi@0 75 String retStr;
aoqi@0 76
aoqi@0 77 private ReturnTypeKind(String retStr) {
aoqi@0 78 this.retStr = retStr;
aoqi@0 79 }
aoqi@0 80
aoqi@0 81 boolean moreSpecificThan(ReturnTypeKind that) {
aoqi@0 82 switch (this) {
aoqi@0 83 case LIST:
aoqi@0 84 return that == this;
aoqi@0 85 case ARRAYLIST:
aoqi@0 86 return that == LIST || that == ARRAYLIST;
aoqi@0 87 default: throw new AssertionError("Unexpected ret kind: " + this);
aoqi@0 88 }
aoqi@0 89 }
aoqi@0 90 }
aoqi@0 91
aoqi@0 92 enum TypeArgumentKind {
aoqi@0 93 NONE(""),
aoqi@0 94 UNBOUND("<?>"),
aoqi@0 95 INTEGER("<Number>"),
aoqi@0 96 NUMBER("<Integer>"),
aoqi@0 97 TYPEVAR("<X>");
aoqi@0 98
aoqi@0 99 String typeargStr;
aoqi@0 100
aoqi@0 101 private TypeArgumentKind(String typeargStr) {
aoqi@0 102 this.typeargStr = typeargStr;
aoqi@0 103 }
aoqi@0 104
aoqi@0 105 boolean compatibleWith(SignatureKind sig) {
aoqi@0 106 switch (this) {
aoqi@0 107 case TYPEVAR: return sig != SignatureKind.NON_GENERIC;
aoqi@0 108 default: return true;
aoqi@0 109 }
aoqi@0 110 }
aoqi@0 111
aoqi@0 112 boolean moreSpecificThan(TypeArgumentKind that, boolean strict) {
aoqi@0 113 switch (this) {
aoqi@0 114 case NONE:
aoqi@0 115 return that == this || !strict;
aoqi@0 116 case UNBOUND:
aoqi@0 117 return that == this || that == NONE;
aoqi@0 118 case INTEGER:
aoqi@0 119 case NUMBER:
aoqi@0 120 case TYPEVAR:
aoqi@0 121 return that == this || that == NONE || that == UNBOUND;
aoqi@0 122 default: throw new AssertionError("Unexpected typearg kind: " + this);
aoqi@0 123 }
aoqi@0 124 }
aoqi@0 125
aoqi@0 126 boolean assignableTo(TypeArgumentKind that, SignatureKind sig, SourceLevel level) {
aoqi@0 127 switch (this) {
aoqi@0 128 case NONE:
aoqi@0 129 //this case needs to workaround to javac's impl of 15.12.2.8 being too strict
aoqi@0 130 //ideally should be just 'return true' (see 7067746/8015505)
aoqi@0 131 return level == SourceLevel.SOURCE_DEFAULT ||
aoqi@0 132 sig == SignatureKind.NON_GENERIC || that == NONE;
aoqi@0 133 case UNBOUND:
aoqi@0 134 return that == this || that == NONE;
aoqi@0 135 case INTEGER:
aoqi@0 136 case NUMBER:
aoqi@0 137 return that == this || that == NONE || that == UNBOUND;
aoqi@0 138 case TYPEVAR:
aoqi@0 139 return true;
aoqi@0 140 default: throw new AssertionError("Unexpected typearg kind: " + this);
aoqi@0 141 }
aoqi@0 142 }
aoqi@0 143 }
aoqi@0 144
aoqi@0 145 public static void main(String... args) throws Exception {
aoqi@0 146 for (SignatureKind sig1 : SignatureKind.values()) {
aoqi@0 147 for (ReturnTypeKind rt1 : ReturnTypeKind.values()) {
aoqi@0 148 for (TypeArgumentKind ta1 : TypeArgumentKind.values()) {
aoqi@0 149 if (!ta1.compatibleWith(sig1)) continue;
aoqi@0 150 for (SignatureKind sig2 : SignatureKind.values()) {
aoqi@0 151 for (ReturnTypeKind rt2 : ReturnTypeKind.values()) {
aoqi@0 152 for (TypeArgumentKind ta2 : TypeArgumentKind.values()) {
aoqi@0 153 if (!ta2.compatibleWith(sig2)) continue;
aoqi@0 154 for (ReturnTypeKind rt3 : ReturnTypeKind.values()) {
aoqi@0 155 for (TypeArgumentKind ta3 : TypeArgumentKind.values()) {
aoqi@0 156 if (!ta3.compatibleWith(SignatureKind.NON_GENERIC))
aoqi@0 157 continue;
aoqi@0 158 for (SourceLevel level : SourceLevel.values()) {
aoqi@0 159 pool.execute(
aoqi@0 160 new GenericOverrideTest(sig1,
aoqi@0 161 rt1, ta1, sig2, rt2,
aoqi@0 162 ta2, rt3, ta3, level));
aoqi@0 163 }
aoqi@0 164 }
aoqi@0 165 }
aoqi@0 166 }
aoqi@0 167 }
aoqi@0 168 }
aoqi@0 169 }
aoqi@0 170 }
aoqi@0 171 }
aoqi@0 172
aoqi@0 173 checkAfterExec();
aoqi@0 174 }
aoqi@0 175
aoqi@0 176 SignatureKind sig1, sig2;
aoqi@0 177 ReturnTypeKind rt1, rt2, rt3;
aoqi@0 178 TypeArgumentKind ta1, ta2, ta3;
aoqi@0 179 SourceLevel level;
aoqi@0 180 JavaSource source;
aoqi@0 181 DiagnosticChecker diagChecker;
aoqi@0 182
aoqi@0 183 GenericOverrideTest(SignatureKind sig1, ReturnTypeKind rt1, TypeArgumentKind ta1,
aoqi@0 184 SignatureKind sig2, ReturnTypeKind rt2, TypeArgumentKind ta2,
aoqi@0 185 ReturnTypeKind rt3, TypeArgumentKind ta3, SourceLevel level) {
aoqi@0 186 this.sig1 = sig1;
aoqi@0 187 this.sig2 = sig2;
aoqi@0 188 this.rt1 = rt1;
aoqi@0 189 this.rt2 = rt2;
aoqi@0 190 this.rt3 = rt3;
aoqi@0 191 this.ta1 = ta1;
aoqi@0 192 this.ta2 = ta2;
aoqi@0 193 this.ta3 = ta3;
aoqi@0 194 this.level = level;
aoqi@0 195 this.source = new JavaSource();
aoqi@0 196 this.diagChecker = new DiagnosticChecker();
aoqi@0 197 }
aoqi@0 198
aoqi@0 199 class JavaSource extends SimpleJavaFileObject {
aoqi@0 200
aoqi@0 201 String template = "import java.util.*;\n" +
aoqi@0 202 "interface A { #S1 #R1#TA1 m(); }\n" +
aoqi@0 203 "interface B { #S2 #R2#TA2 m(); }\n" +
aoqi@0 204 "interface AB extends A, B {}\n" +
aoqi@0 205 "class Test {\n" +
aoqi@0 206 " void test(AB ab) { #R3#TA3 n = ab.m(); }\n" +
aoqi@0 207 "}";
aoqi@0 208
aoqi@0 209 String source;
aoqi@0 210
aoqi@0 211 public JavaSource() {
aoqi@0 212 super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
aoqi@0 213 source = template.replace("#S1", sig1.paramStr).
aoqi@0 214 replace("#S2", sig2.paramStr).
aoqi@0 215 replace("#R1", rt1.retStr).
aoqi@0 216 replace("#R2", rt2.retStr).
aoqi@0 217 replace("#R3", rt3.retStr).
aoqi@0 218 replace("#TA1", ta1.typeargStr).
aoqi@0 219 replace("#TA2", ta2.typeargStr).
aoqi@0 220 replace("#TA3", ta3.typeargStr);
aoqi@0 221 }
aoqi@0 222
aoqi@0 223 @Override
aoqi@0 224 public CharSequence getCharContent(boolean ignoreEncodingErrors) {
aoqi@0 225 return source;
aoqi@0 226 }
aoqi@0 227 }
aoqi@0 228
aoqi@0 229 @Override
aoqi@0 230 public void run() {
aoqi@0 231 JavacTask ct = (JavacTask)comp.getTask(null, fm.get(), diagChecker,
aoqi@0 232 level.opts != null ? Arrays.asList(level.opts) : null,
aoqi@0 233 null, Arrays.asList(source));
aoqi@0 234 try {
aoqi@0 235 ct.analyze();
aoqi@0 236 } catch (Throwable ex) {
aoqi@0 237 throw new AssertionError("Error thrown when compiling the following code:\n" +
aoqi@0 238 source.getCharContent(true));
aoqi@0 239 }
aoqi@0 240 check();
aoqi@0 241 }
aoqi@0 242
aoqi@0 243 void check() {
aoqi@0 244 checkCount.incrementAndGet();
aoqi@0 245
aoqi@0 246 boolean errorExpected = false;
aoqi@0 247 int mostSpecific = 0;
aoqi@0 248
aoqi@0 249 //first check that either |R1| <: |R2| or |R2| <: |R1|
aoqi@0 250 if (rt1 != rt2) {
aoqi@0 251 if (!rt1.moreSpecificThan(rt2) &&
aoqi@0 252 !rt2.moreSpecificThan(rt1)) {
aoqi@0 253 errorExpected = true;
aoqi@0 254 } else {
aoqi@0 255 mostSpecific = rt1.moreSpecificThan(rt2) ? 1 : 2;
aoqi@0 256 }
aoqi@0 257 }
aoqi@0 258
aoqi@0 259 //check that either TA1 <= TA2 or TA2 <= TA1 (unless most specific return found above is raw)
aoqi@0 260 if (!errorExpected) {
aoqi@0 261 if (ta1 != ta2) {
aoqi@0 262 boolean useStrictCheck = ta1.moreSpecificThan(ta2, true) ||
aoqi@0 263 ta2.moreSpecificThan(ta1, true);
aoqi@0 264 if (!ta1.moreSpecificThan(ta2, useStrictCheck) &&
aoqi@0 265 !ta2.moreSpecificThan(ta1, useStrictCheck)) {
aoqi@0 266 errorExpected = true;
aoqi@0 267 } else {
aoqi@0 268 int mostSpecific2 = ta1.moreSpecificThan(ta2, useStrictCheck) ? 1 : 2;
aoqi@0 269 if (mostSpecific != 0 && mostSpecific2 != mostSpecific) {
aoqi@0 270 errorExpected = mostSpecific == 1 ?
aoqi@0 271 ta1 != TypeArgumentKind.NONE :
aoqi@0 272 ta2 != TypeArgumentKind.NONE;
aoqi@0 273 } else {
aoqi@0 274 mostSpecific = mostSpecific2;
aoqi@0 275 }
aoqi@0 276 }
aoqi@0 277 } else if (mostSpecific == 0) {
aoqi@0 278 //when no signature is better than the other, an arbitrary choice
aoqi@0 279 //must be made - javac always picks the second signature
aoqi@0 280 mostSpecific = 2;
aoqi@0 281 }
aoqi@0 282 }
aoqi@0 283
aoqi@0 284 //finally, check that most specific return type is compatible with expected type
aoqi@0 285 if (!errorExpected) {
aoqi@0 286 ReturnTypeKind msrt = mostSpecific == 1 ? rt1 : rt2;
aoqi@0 287 TypeArgumentKind msta = mostSpecific == 1 ? ta1 : ta2;
aoqi@0 288 SignatureKind mssig = mostSpecific == 1 ? sig1 : sig2;
aoqi@0 289
aoqi@0 290 if (!msrt.moreSpecificThan(rt3) ||
aoqi@0 291 !msta.assignableTo(ta3, mssig, level)) {
aoqi@0 292 errorExpected = true;
aoqi@0 293 }
aoqi@0 294 }
aoqi@0 295
aoqi@0 296 if (errorExpected != diagChecker.errorFound) {
aoqi@0 297 throw new Error("invalid diagnostics for source:\n" +
aoqi@0 298 source.getCharContent(true) +
aoqi@0 299 "\nFound error: " + diagChecker.errorFound +
aoqi@0 300 "\nExpected error: " + errorExpected);
aoqi@0 301 }
aoqi@0 302 }
aoqi@0 303
aoqi@0 304 static class DiagnosticChecker
aoqi@0 305 implements javax.tools.DiagnosticListener<JavaFileObject> {
aoqi@0 306
aoqi@0 307 boolean errorFound;
aoqi@0 308
aoqi@0 309 public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
aoqi@0 310 if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
aoqi@0 311 errorFound = true;
aoqi@0 312 }
aoqi@0 313 }
aoqi@0 314 }
aoqi@0 315
aoqi@0 316 }

mercurial