Fri, 23 Dec 2011 22:30:33 +0000
7124605: typos in javac comments
Reviewed-by: ksrini
mcimadamore@1059 | 1 | /* |
mcimadamore@1059 | 2 | * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. |
mcimadamore@1059 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
mcimadamore@1059 | 4 | * |
mcimadamore@1059 | 5 | * This code is free software; you can redistribute it and/or modify it |
mcimadamore@1059 | 6 | * under the terms of the GNU General Public License version 2 only, as |
mcimadamore@1059 | 7 | * published by the Free Software Foundation. |
mcimadamore@1059 | 8 | * |
mcimadamore@1059 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
mcimadamore@1059 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
mcimadamore@1059 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
mcimadamore@1059 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
mcimadamore@1059 | 13 | * accompanied this code). |
mcimadamore@1059 | 14 | * |
mcimadamore@1059 | 15 | * You should have received a copy of the GNU General Public License version |
mcimadamore@1059 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
mcimadamore@1059 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
mcimadamore@1059 | 18 | * |
mcimadamore@1059 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
mcimadamore@1059 | 20 | * or visit www.oracle.com if you need additional information or have any |
mcimadamore@1059 | 21 | * questions. |
mcimadamore@1059 | 22 | */ |
mcimadamore@1059 | 23 | |
mcimadamore@1059 | 24 | /* |
mcimadamore@1059 | 25 | * @test |
mcimadamore@1059 | 26 | * @bug 7062745 |
mcimadamore@1059 | 27 | * @summary Regression: difference in overload resolution when two methods are maximally specific |
mcimadamore@1059 | 28 | */ |
mcimadamore@1059 | 29 | |
mcimadamore@1059 | 30 | import com.sun.source.util.JavacTask; |
mcimadamore@1059 | 31 | import java.net.URI; |
mcimadamore@1059 | 32 | import java.util.Arrays; |
mcimadamore@1059 | 33 | import javax.tools.Diagnostic; |
mcimadamore@1059 | 34 | import javax.tools.JavaCompiler; |
mcimadamore@1059 | 35 | import javax.tools.JavaFileObject; |
mcimadamore@1059 | 36 | import javax.tools.SimpleJavaFileObject; |
mcimadamore@1059 | 37 | import javax.tools.StandardJavaFileManager; |
mcimadamore@1059 | 38 | import javax.tools.ToolProvider; |
mcimadamore@1059 | 39 | |
mcimadamore@1059 | 40 | public class GenericOverrideTest { |
mcimadamore@1059 | 41 | |
mcimadamore@1059 | 42 | static int checkCount = 0; |
mcimadamore@1059 | 43 | |
mcimadamore@1059 | 44 | enum SignatureKind { |
mcimadamore@1059 | 45 | NON_GENERIC(""), |
mcimadamore@1059 | 46 | GENERIC("<X>"); |
mcimadamore@1059 | 47 | |
mcimadamore@1059 | 48 | String paramStr; |
mcimadamore@1059 | 49 | |
mcimadamore@1059 | 50 | private SignatureKind(String paramStr) { |
mcimadamore@1059 | 51 | this.paramStr = paramStr; |
mcimadamore@1059 | 52 | } |
mcimadamore@1059 | 53 | } |
mcimadamore@1059 | 54 | |
mcimadamore@1059 | 55 | enum ReturnTypeKind { |
mcimadamore@1059 | 56 | LIST("List"), |
mcimadamore@1059 | 57 | ARRAYLIST("ArrayList"); |
mcimadamore@1059 | 58 | |
mcimadamore@1059 | 59 | String retStr; |
mcimadamore@1059 | 60 | |
mcimadamore@1059 | 61 | private ReturnTypeKind(String retStr) { |
mcimadamore@1059 | 62 | this.retStr = retStr; |
mcimadamore@1059 | 63 | } |
mcimadamore@1059 | 64 | |
mcimadamore@1059 | 65 | boolean moreSpecificThan(ReturnTypeKind that) { |
mcimadamore@1059 | 66 | switch (this) { |
mcimadamore@1059 | 67 | case LIST: |
mcimadamore@1059 | 68 | return that == this; |
mcimadamore@1059 | 69 | case ARRAYLIST: |
mcimadamore@1059 | 70 | return that == LIST || that == ARRAYLIST; |
mcimadamore@1059 | 71 | default: throw new AssertionError("Unexpected ret kind: " + this); |
mcimadamore@1059 | 72 | } |
mcimadamore@1059 | 73 | } |
mcimadamore@1059 | 74 | } |
mcimadamore@1059 | 75 | |
mcimadamore@1059 | 76 | enum TypeArgumentKind { |
mcimadamore@1059 | 77 | NONE(""), |
mcimadamore@1059 | 78 | UNBOUND("<?>"), |
mcimadamore@1059 | 79 | INTEGER("<Number>"), |
mcimadamore@1059 | 80 | NUMBER("<Integer>"), |
mcimadamore@1059 | 81 | TYPEVAR("<X>"); |
mcimadamore@1059 | 82 | |
mcimadamore@1059 | 83 | String typeargStr; |
mcimadamore@1059 | 84 | |
mcimadamore@1059 | 85 | private TypeArgumentKind(String typeargStr) { |
mcimadamore@1059 | 86 | this.typeargStr = typeargStr; |
mcimadamore@1059 | 87 | } |
mcimadamore@1059 | 88 | |
mcimadamore@1059 | 89 | boolean compatibleWith(SignatureKind sig) { |
mcimadamore@1059 | 90 | switch (this) { |
mcimadamore@1059 | 91 | case TYPEVAR: return sig != SignatureKind.NON_GENERIC; |
mcimadamore@1059 | 92 | default: return true; |
mcimadamore@1059 | 93 | } |
mcimadamore@1059 | 94 | } |
mcimadamore@1059 | 95 | |
mcimadamore@1059 | 96 | boolean moreSpecificThan(TypeArgumentKind that, boolean strict) { |
mcimadamore@1059 | 97 | switch (this) { |
mcimadamore@1059 | 98 | case NONE: |
mcimadamore@1059 | 99 | return that == this || !strict; |
mcimadamore@1059 | 100 | case UNBOUND: |
mcimadamore@1059 | 101 | return that == this || that == NONE; |
mcimadamore@1059 | 102 | case INTEGER: |
mcimadamore@1059 | 103 | case NUMBER: |
mcimadamore@1059 | 104 | case TYPEVAR: |
mcimadamore@1059 | 105 | return that == this || that == NONE || that == UNBOUND; |
mcimadamore@1059 | 106 | default: throw new AssertionError("Unexpected typearg kind: " + this); |
mcimadamore@1059 | 107 | } |
mcimadamore@1059 | 108 | } |
mcimadamore@1059 | 109 | |
mcimadamore@1059 | 110 | boolean assignableTo(TypeArgumentKind that, SignatureKind sig) { |
mcimadamore@1059 | 111 | switch (this) { |
mcimadamore@1059 | 112 | case NONE: |
mcimadamore@1059 | 113 | //this case needs to workaround to javac's impl of 15.12.2.8 being too strict |
mcimadamore@1059 | 114 | //ideally should be just 'return true' (see 7067746) |
mcimadamore@1059 | 115 | return sig == SignatureKind.NON_GENERIC || that == NONE; |
mcimadamore@1059 | 116 | case UNBOUND: |
mcimadamore@1059 | 117 | return that == this || that == NONE; |
mcimadamore@1059 | 118 | case INTEGER: |
mcimadamore@1059 | 119 | case NUMBER: |
mcimadamore@1059 | 120 | return that == this || that == NONE || that == UNBOUND; |
mcimadamore@1059 | 121 | case TYPEVAR: |
mcimadamore@1059 | 122 | return true; |
mcimadamore@1059 | 123 | default: throw new AssertionError("Unexpected typearg kind: " + this); |
mcimadamore@1059 | 124 | } |
mcimadamore@1059 | 125 | } |
mcimadamore@1059 | 126 | } |
mcimadamore@1059 | 127 | |
mcimadamore@1059 | 128 | public static void main(String... args) throws Exception { |
mcimadamore@1059 | 129 | |
mcimadamore@1059 | 130 | //create default shared JavaCompiler - reused across multiple compilations |
mcimadamore@1059 | 131 | JavaCompiler comp = ToolProvider.getSystemJavaCompiler(); |
mcimadamore@1059 | 132 | StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null); |
mcimadamore@1059 | 133 | |
mcimadamore@1059 | 134 | for (SignatureKind sig1 : SignatureKind.values()) { |
mcimadamore@1059 | 135 | for (ReturnTypeKind rt1 : ReturnTypeKind.values()) { |
mcimadamore@1059 | 136 | for (TypeArgumentKind ta1 : TypeArgumentKind.values()) { |
mcimadamore@1059 | 137 | if (!ta1.compatibleWith(sig1)) continue; |
mcimadamore@1059 | 138 | for (SignatureKind sig2 : SignatureKind.values()) { |
mcimadamore@1059 | 139 | for (ReturnTypeKind rt2 : ReturnTypeKind.values()) { |
mcimadamore@1059 | 140 | for (TypeArgumentKind ta2 : TypeArgumentKind.values()) { |
mcimadamore@1059 | 141 | if (!ta2.compatibleWith(sig2)) continue; |
mcimadamore@1059 | 142 | for (ReturnTypeKind rt3 : ReturnTypeKind.values()) { |
mcimadamore@1059 | 143 | for (TypeArgumentKind ta3 : TypeArgumentKind.values()) { |
mcimadamore@1059 | 144 | if (!ta3.compatibleWith(SignatureKind.NON_GENERIC)) continue; |
mcimadamore@1059 | 145 | new GenericOverrideTest(sig1, rt1, ta1, sig2, rt2, ta2, rt3, ta3).run(comp, fm); |
mcimadamore@1059 | 146 | } |
mcimadamore@1059 | 147 | } |
mcimadamore@1059 | 148 | } |
mcimadamore@1059 | 149 | } |
mcimadamore@1059 | 150 | } |
mcimadamore@1059 | 151 | } |
mcimadamore@1059 | 152 | } |
mcimadamore@1059 | 153 | } |
mcimadamore@1059 | 154 | System.out.println("Total check executed: " + checkCount); |
mcimadamore@1059 | 155 | } |
mcimadamore@1059 | 156 | |
mcimadamore@1059 | 157 | SignatureKind sig1, sig2; |
mcimadamore@1059 | 158 | ReturnTypeKind rt1, rt2, rt3; |
mcimadamore@1059 | 159 | TypeArgumentKind ta1, ta2, ta3; |
mcimadamore@1059 | 160 | JavaSource source; |
mcimadamore@1059 | 161 | DiagnosticChecker diagChecker; |
mcimadamore@1059 | 162 | |
mcimadamore@1059 | 163 | GenericOverrideTest(SignatureKind sig1, ReturnTypeKind rt1, TypeArgumentKind ta1, |
mcimadamore@1059 | 164 | SignatureKind sig2, ReturnTypeKind rt2, TypeArgumentKind ta2, ReturnTypeKind rt3, TypeArgumentKind ta3) { |
mcimadamore@1059 | 165 | this.sig1 = sig1; |
mcimadamore@1059 | 166 | this.sig2 = sig2; |
mcimadamore@1059 | 167 | this.rt1 = rt1; |
mcimadamore@1059 | 168 | this.rt2 = rt2; |
mcimadamore@1059 | 169 | this.rt3 = rt3; |
mcimadamore@1059 | 170 | this.ta1 = ta1; |
mcimadamore@1059 | 171 | this.ta2 = ta2; |
mcimadamore@1059 | 172 | this.ta3 = ta3; |
mcimadamore@1059 | 173 | this.source = new JavaSource(); |
mcimadamore@1059 | 174 | this.diagChecker = new DiagnosticChecker(); |
mcimadamore@1059 | 175 | } |
mcimadamore@1059 | 176 | |
mcimadamore@1059 | 177 | class JavaSource extends SimpleJavaFileObject { |
mcimadamore@1059 | 178 | |
mcimadamore@1059 | 179 | String template = "import java.util.*;\n" + |
mcimadamore@1059 | 180 | "interface A { #S1 #R1#TA1 m(); }\n" + |
mcimadamore@1059 | 181 | "interface B { #S2 #R2#TA2 m(); }\n" + |
mcimadamore@1059 | 182 | "interface AB extends A, B {}\n" + |
mcimadamore@1059 | 183 | "class Test {\n" + |
mcimadamore@1059 | 184 | " void test(AB ab) { #R3#TA3 n = ab.m(); }\n" + |
mcimadamore@1059 | 185 | "}"; |
mcimadamore@1059 | 186 | |
mcimadamore@1059 | 187 | String source; |
mcimadamore@1059 | 188 | |
mcimadamore@1059 | 189 | public JavaSource() { |
mcimadamore@1059 | 190 | super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); |
mcimadamore@1059 | 191 | source = template.replace("#S1", sig1.paramStr). |
mcimadamore@1059 | 192 | replace("#S2", sig2.paramStr). |
mcimadamore@1059 | 193 | replace("#R1", rt1.retStr). |
mcimadamore@1059 | 194 | replace("#R2", rt2.retStr). |
mcimadamore@1059 | 195 | replace("#R3", rt3.retStr). |
mcimadamore@1059 | 196 | replace("#TA1", ta1.typeargStr). |
mcimadamore@1059 | 197 | replace("#TA2", ta2.typeargStr). |
mcimadamore@1059 | 198 | replace("#TA3", ta3.typeargStr); |
mcimadamore@1059 | 199 | } |
mcimadamore@1059 | 200 | |
mcimadamore@1059 | 201 | @Override |
mcimadamore@1059 | 202 | public CharSequence getCharContent(boolean ignoreEncodingErrors) { |
mcimadamore@1059 | 203 | return source; |
mcimadamore@1059 | 204 | } |
mcimadamore@1059 | 205 | } |
mcimadamore@1059 | 206 | |
mcimadamore@1059 | 207 | void run(JavaCompiler tool, StandardJavaFileManager fm) throws Exception { |
mcimadamore@1059 | 208 | JavacTask ct = (JavacTask)tool.getTask(null, fm, diagChecker, |
mcimadamore@1059 | 209 | null, null, Arrays.asList(source)); |
mcimadamore@1059 | 210 | try { |
mcimadamore@1059 | 211 | ct.analyze(); |
mcimadamore@1059 | 212 | } catch (Throwable ex) { |
jjg@1169 | 213 | throw new AssertionError("Error thrown when compiling the following code:\n" + source.getCharContent(true)); |
mcimadamore@1059 | 214 | } |
mcimadamore@1059 | 215 | check(); |
mcimadamore@1059 | 216 | } |
mcimadamore@1059 | 217 | |
mcimadamore@1059 | 218 | void check() { |
mcimadamore@1059 | 219 | checkCount++; |
mcimadamore@1059 | 220 | |
mcimadamore@1059 | 221 | boolean errorExpected = false; |
mcimadamore@1059 | 222 | int mostSpecific = 0; |
mcimadamore@1059 | 223 | |
mcimadamore@1059 | 224 | //first check that either |R1| <: |R2| or |R2| <: |R1| |
mcimadamore@1059 | 225 | if (rt1 != rt2) { |
mcimadamore@1059 | 226 | if (!rt1.moreSpecificThan(rt2) && |
mcimadamore@1059 | 227 | !rt2.moreSpecificThan(rt1)) { |
mcimadamore@1059 | 228 | errorExpected = true; |
mcimadamore@1059 | 229 | } else { |
mcimadamore@1059 | 230 | mostSpecific = rt1.moreSpecificThan(rt2) ? 1 : 2; |
mcimadamore@1059 | 231 | } |
mcimadamore@1059 | 232 | } |
mcimadamore@1059 | 233 | |
mcimadamore@1059 | 234 | //check that either TA1 <= TA2 or TA2 <= TA1 (unless most specific return found above is raw) |
mcimadamore@1059 | 235 | if (!errorExpected) { |
mcimadamore@1059 | 236 | if (ta1 != ta2) { |
mcimadamore@1059 | 237 | boolean useStrictCheck = ta1.moreSpecificThan(ta2, true) || ta2.moreSpecificThan(ta1, true); |
mcimadamore@1059 | 238 | if (!ta1.moreSpecificThan(ta2, useStrictCheck) && |
mcimadamore@1059 | 239 | !ta2.moreSpecificThan(ta1, useStrictCheck)) { |
mcimadamore@1059 | 240 | errorExpected = true; |
mcimadamore@1059 | 241 | } else { |
mcimadamore@1059 | 242 | int mostSpecific2 = ta1.moreSpecificThan(ta2, useStrictCheck) ? 1 : 2; |
mcimadamore@1059 | 243 | if (mostSpecific != 0 && mostSpecific2 != mostSpecific) { |
mcimadamore@1059 | 244 | errorExpected = mostSpecific == 1 ? ta1 != TypeArgumentKind.NONE : ta2 != TypeArgumentKind.NONE; |
mcimadamore@1059 | 245 | } else { |
mcimadamore@1059 | 246 | mostSpecific = mostSpecific2; |
mcimadamore@1059 | 247 | } |
mcimadamore@1059 | 248 | } |
mcimadamore@1059 | 249 | } else if (mostSpecific == 0) { |
mcimadamore@1059 | 250 | //when no signature is better than the other, an arbitrary choice |
mcimadamore@1059 | 251 | //must be made - javac always picks the second signature |
mcimadamore@1059 | 252 | mostSpecific = 2; |
mcimadamore@1059 | 253 | } |
mcimadamore@1059 | 254 | } |
mcimadamore@1059 | 255 | |
mcimadamore@1059 | 256 | //finally, check that most specific return type is compatible with expected type |
mcimadamore@1059 | 257 | if (!errorExpected) { |
mcimadamore@1059 | 258 | ReturnTypeKind msrt = mostSpecific == 1 ? rt1 : rt2; |
mcimadamore@1059 | 259 | TypeArgumentKind msta = mostSpecific == 1 ? ta1 : ta2; |
mcimadamore@1059 | 260 | SignatureKind mssig = mostSpecific == 1 ? sig1 : sig2; |
mcimadamore@1059 | 261 | |
mcimadamore@1059 | 262 | if (!msrt.moreSpecificThan(rt3) || |
mcimadamore@1059 | 263 | !msta.assignableTo(ta3, mssig)) { |
mcimadamore@1059 | 264 | errorExpected = true; |
mcimadamore@1059 | 265 | } |
mcimadamore@1059 | 266 | } |
mcimadamore@1059 | 267 | |
mcimadamore@1059 | 268 | if (errorExpected != diagChecker.errorFound) { |
mcimadamore@1059 | 269 | throw new Error("invalid diagnostics for source:\n" + |
mcimadamore@1059 | 270 | source.getCharContent(true) + |
mcimadamore@1059 | 271 | "\nFound error: " + diagChecker.errorFound + |
mcimadamore@1059 | 272 | "\nExpected error: " + errorExpected); |
mcimadamore@1059 | 273 | } |
mcimadamore@1059 | 274 | } |
mcimadamore@1059 | 275 | |
mcimadamore@1059 | 276 | static class DiagnosticChecker implements javax.tools.DiagnosticListener<JavaFileObject> { |
mcimadamore@1059 | 277 | |
mcimadamore@1059 | 278 | boolean errorFound; |
mcimadamore@1059 | 279 | |
mcimadamore@1059 | 280 | public void report(Diagnostic<? extends JavaFileObject> diagnostic) { |
mcimadamore@1059 | 281 | if (diagnostic.getKind() == Diagnostic.Kind.ERROR) { |
mcimadamore@1059 | 282 | errorFound = true; |
mcimadamore@1059 | 283 | } |
mcimadamore@1059 | 284 | } |
mcimadamore@1059 | 285 | } |
mcimadamore@1059 | 286 | } |