Tue, 08 Jan 2013 13:47:57 +0000
8005167: execution time of combo tests in javac should be improved
Reviewed-by: jjg, jjh
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 */
24 /*
25 * @test
26 * @bug 7046778
27 * @summary Project Coin: problem with diamond and member inner classes
28 * @library ../../../lib
29 * @build JavacTestingAbstractThreadedTest
30 * @run main DiamondAndInnerClassTest
31 */
33 import com.sun.source.util.JavacTask;
34 import java.net.URI;
35 import java.util.Arrays;
36 import javax.tools.Diagnostic;
37 import javax.tools.JavaFileObject;
38 import javax.tools.SimpleJavaFileObject;
40 public class DiamondAndInnerClassTest
41 extends JavacTestingAbstractThreadedTest
42 implements Runnable {
44 enum TypeArgumentKind {
45 NONE(""),
46 STRING("<String>"),
47 INTEGER("<Integer>"),
48 DIAMOND("<>");
50 String typeargStr;
52 private TypeArgumentKind(String typeargStr) {
53 this.typeargStr = typeargStr;
54 }
56 boolean compatible(TypeArgumentKind that) {
57 switch (this) {
58 case NONE: return true;
59 case STRING: return that != INTEGER;
60 case INTEGER: return that != STRING;
61 default: throw new AssertionError("Unexpected decl kind: " + this);
62 }
63 }
65 boolean compatible(ArgumentKind that) {
66 switch (this) {
67 case NONE: return true;
68 case STRING: return that == ArgumentKind.STRING;
69 case INTEGER: return that == ArgumentKind.INTEGER;
70 default: throw new AssertionError("Unexpected decl kind: " + this);
71 }
72 }
73 }
75 enum ArgumentKind {
76 OBJECT("(Object)null"),
77 STRING("(String)null"),
78 INTEGER("(Integer)null");
80 String argStr;
82 private ArgumentKind(String argStr) {
83 this.argStr = argStr;
84 }
85 }
87 enum TypeQualifierArity {
88 ONE(1, "A1#TA1"),
89 TWO(2, "A1#TA1.A2#TA2"),
90 THREE(3, "A1#TA1.A2#TA2.A3#TA3");
92 int n;
93 String qualifierStr;
95 private TypeQualifierArity(int n, String qualifierStr) {
96 this.n = n;
97 this.qualifierStr = qualifierStr;
98 }
100 String getType(TypeArgumentKind... typeArgumentKinds) {
101 String res = qualifierStr;
102 for (int i = 1 ; i <= typeArgumentKinds.length ; i++) {
103 res = res.replace("#TA" + i, typeArgumentKinds[i-1].typeargStr);
104 }
105 return res;
106 }
108 boolean matches(InnerClassDeclArity innerClassDeclArity) {
109 return n ==innerClassDeclArity.n;
110 }
111 }
113 enum InnerClassDeclArity {
114 ONE(1, "class A1<X> { A1(X x1) { } #B }"),
115 TWO(2, "class A1<X1> { class A2<X2> { A2(X1 x1, X2 x2) { } #B } }"),
116 THREE(3, "class A1<X1> { class A2<X2> { class A3<X3> { A3(X1 x1, X2 x2, X3 x3) { } #B } } }");
118 int n;
119 String classDeclStr;
121 private InnerClassDeclArity(int n, String classDeclStr) {
122 this.n = n;
123 this.classDeclStr = classDeclStr;
124 }
125 }
127 enum ArgumentListArity {
128 ONE(1, "(#A1)"),
129 TWO(2, "(#A1,#A2)"),
130 THREE(3, "(#A1,#A2,#A3)");
132 int n;
133 String argListStr;
135 private ArgumentListArity(int n, String argListStr) {
136 this.n = n;
137 this.argListStr = argListStr;
138 }
140 String getArgs(ArgumentKind... argumentKinds) {
141 String res = argListStr;
142 for (int i = 1 ; i <= argumentKinds.length ; i++) {
143 res = res.replace("#A" + i, argumentKinds[i-1].argStr);
144 }
145 return res;
146 }
148 boolean matches(InnerClassDeclArity innerClassDeclArity) {
149 return n ==innerClassDeclArity.n;
150 }
151 }
153 public static void main(String... args) throws Exception {
154 for (InnerClassDeclArity innerClassDeclArity : InnerClassDeclArity.values()) {
155 for (TypeQualifierArity declType : TypeQualifierArity.values()) {
156 if (!declType.matches(innerClassDeclArity)) continue;
157 for (TypeQualifierArity newClassType : TypeQualifierArity.values()) {
158 if (!newClassType.matches(innerClassDeclArity)) continue;
159 for (ArgumentListArity argList : ArgumentListArity.values()) {
160 if (!argList.matches(innerClassDeclArity)) continue;
161 for (TypeArgumentKind taDecl1 : TypeArgumentKind.values()) {
162 boolean isDeclRaw = taDecl1 == TypeArgumentKind.NONE;
163 //no diamond on decl site
164 if (taDecl1 == TypeArgumentKind.DIAMOND) continue;
165 for (TypeArgumentKind taSite1 : TypeArgumentKind.values()) {
166 boolean isSiteRaw =
167 taSite1 == TypeArgumentKind.NONE;
168 //diamond only allowed on the last type qualifier
169 if (taSite1 == TypeArgumentKind.DIAMOND &&
170 innerClassDeclArity !=
171 InnerClassDeclArity.ONE)
172 continue;
173 for (ArgumentKind arg1 : ArgumentKind.values()) {
174 if (innerClassDeclArity == innerClassDeclArity.ONE) {
175 pool.execute(
176 new DiamondAndInnerClassTest(
177 innerClassDeclArity, declType,
178 newClassType, argList,
179 new TypeArgumentKind[] {taDecl1},
180 new TypeArgumentKind[] {taSite1},
181 new ArgumentKind[] {arg1}));
182 continue;
183 }
184 for (TypeArgumentKind taDecl2 : TypeArgumentKind.values()) {
185 //no rare types
186 if (isDeclRaw != (taDecl2 == TypeArgumentKind.NONE))
187 continue;
188 //no diamond on decl site
189 if (taDecl2 == TypeArgumentKind.DIAMOND)
190 continue;
191 for (TypeArgumentKind taSite2 : TypeArgumentKind.values()) {
192 //no rare types
193 if (isSiteRaw != (taSite2 == TypeArgumentKind.NONE))
194 continue;
195 //diamond only allowed on the last type qualifier
196 if (taSite2 == TypeArgumentKind.DIAMOND &&
197 innerClassDeclArity != InnerClassDeclArity.TWO)
198 continue;
199 for (ArgumentKind arg2 : ArgumentKind.values()) {
200 if (innerClassDeclArity == innerClassDeclArity.TWO) {
201 pool.execute(
202 new DiamondAndInnerClassTest(
203 innerClassDeclArity,
204 declType,
205 newClassType,
206 argList,
207 new TypeArgumentKind[] {taDecl1, taDecl2},
208 new TypeArgumentKind[] {taSite1, taSite2},
209 new ArgumentKind[] {arg1, arg2}));
210 continue;
211 }
212 for (TypeArgumentKind taDecl3 : TypeArgumentKind.values()) {
213 //no rare types
214 if (isDeclRaw != (taDecl3 == TypeArgumentKind.NONE))
215 continue;
216 //no diamond on decl site
217 if (taDecl3 == TypeArgumentKind.DIAMOND)
218 continue;
219 for (TypeArgumentKind taSite3 : TypeArgumentKind.values()) {
220 //no rare types
221 if (isSiteRaw != (taSite3 == TypeArgumentKind.NONE))
222 continue;
223 //diamond only allowed on the last type qualifier
224 if (taSite3 == TypeArgumentKind.DIAMOND &&
225 innerClassDeclArity != InnerClassDeclArity.THREE)
226 continue;
227 for (ArgumentKind arg3 : ArgumentKind.values()) {
228 if (innerClassDeclArity ==
229 innerClassDeclArity.THREE) {
230 pool.execute(
231 new DiamondAndInnerClassTest(
232 innerClassDeclArity,
233 declType,
234 newClassType,
235 argList,
236 new TypeArgumentKind[] {taDecl1, taDecl2, taDecl3},
237 new TypeArgumentKind[] {taSite1, taSite2, taSite3},
238 new ArgumentKind[] {arg1, arg2, arg3}));
239 continue;
240 }
241 }
242 }
243 }
244 }
245 }
246 }
247 }
248 }
249 }
250 }
251 }
252 }
253 }
255 checkAfterExec();
256 }
258 InnerClassDeclArity innerClassDeclArity;
259 TypeQualifierArity declType;
260 TypeQualifierArity siteType;
261 ArgumentListArity argList;
262 TypeArgumentKind[] declTypeArgumentKinds;
263 TypeArgumentKind[] siteTypeArgumentKinds;
264 ArgumentKind[] argumentKinds;
265 JavaSource source;
266 DiagnosticChecker diagChecker;
268 DiamondAndInnerClassTest(InnerClassDeclArity innerClassDeclArity,
269 TypeQualifierArity declType, TypeQualifierArity siteType,
270 ArgumentListArity argList, TypeArgumentKind[] declTypeArgumentKinds,
271 TypeArgumentKind[] siteTypeArgumentKinds, ArgumentKind[] argumentKinds) {
272 this.innerClassDeclArity = innerClassDeclArity;
273 this.declType = declType;
274 this.siteType = siteType;
275 this.argList = argList;
276 this.declTypeArgumentKinds = declTypeArgumentKinds;
277 this.siteTypeArgumentKinds = siteTypeArgumentKinds;
278 this.argumentKinds = argumentKinds;
279 this.source = new JavaSource();
280 this.diagChecker = new DiagnosticChecker();
281 }
283 class JavaSource extends SimpleJavaFileObject {
285 String bodyTemplate = "#D res = new #S#AL;";
287 String source;
289 public JavaSource() {
290 super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
291 source = innerClassDeclArity.classDeclStr.replace("#B", bodyTemplate)
292 .replace("#D", declType.getType(declTypeArgumentKinds))
293 .replace("#S", siteType.getType(siteTypeArgumentKinds))
294 .replace("#AL", argList.getArgs(argumentKinds));
295 }
297 @Override
298 public CharSequence getCharContent(boolean ignoreEncodingErrors) {
299 return source;
300 }
301 }
303 @Override
304 public void run() {
305 JavacTask ct = (JavacTask)comp.getTask(null, fm.get(), diagChecker,
306 null, null, Arrays.asList(source));
307 try {
308 ct.analyze();
309 } catch (Throwable ex) {
310 throw new AssertionError("Error thrown when compiling the following code:\n" +
311 source.getCharContent(true));
312 }
313 check();
314 }
316 void check() {
317 checkCount.incrementAndGet();
319 boolean errorExpected = false;
321 TypeArgumentKind[] expectedArgKinds =
322 new TypeArgumentKind[innerClassDeclArity.n];
324 for (int i = 0 ; i < innerClassDeclArity.n ; i++) {
325 if (!declTypeArgumentKinds[i].compatible(siteTypeArgumentKinds[i])) {
326 errorExpected = true;
327 break;
328 }
329 expectedArgKinds[i] = siteTypeArgumentKinds[i] ==
330 TypeArgumentKind.DIAMOND ?
331 declTypeArgumentKinds[i] : siteTypeArgumentKinds[i];
332 }
334 if (!errorExpected) {
335 for (int i = 0 ; i < innerClassDeclArity.n ; i++) {
336 if (!expectedArgKinds[i].compatible(argumentKinds[i])) {
337 errorExpected = true;
338 break;
339 }
340 }
341 }
343 if (errorExpected != diagChecker.errorFound) {
344 throw new Error("invalid diagnostics for source:\n" +
345 source.getCharContent(true) +
346 "\nFound error: " + diagChecker.errorFound +
347 "\nExpected error: " + errorExpected);
348 }
349 }
351 static class DiagnosticChecker
352 implements javax.tools.DiagnosticListener<JavaFileObject> {
354 boolean errorFound;
356 public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
357 if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
358 errorFound = true;
359 }
360 }
361 }
363 }