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