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