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 7046778
27 * @summary Project Coin: problem with diamond and member inner classes
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 DiamondAndInnerClassTest {
42 static int checkCount = 0;
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 {
155 //create default shared JavaCompiler - reused across multiple compilations
156 JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
157 StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null);
159 for (InnerClassDeclArity innerClassDeclArity : InnerClassDeclArity.values()) {
160 for (TypeQualifierArity declType : TypeQualifierArity.values()) {
161 if (!declType.matches(innerClassDeclArity)) continue;
162 for (TypeQualifierArity newClassType : TypeQualifierArity.values()) {
163 if (!newClassType.matches(innerClassDeclArity)) continue;
164 for (ArgumentListArity argList : ArgumentListArity.values()) {
165 if (!argList.matches(innerClassDeclArity)) continue;
166 for (TypeArgumentKind taDecl1 : TypeArgumentKind.values()) {
167 boolean isDeclRaw = taDecl1 == TypeArgumentKind.NONE;
168 //no diamond on decl site
169 if (taDecl1 == TypeArgumentKind.DIAMOND) continue;
170 for (TypeArgumentKind taSite1 : TypeArgumentKind.values()) {
171 boolean isSiteRaw = taSite1 == TypeArgumentKind.NONE;
172 //diamond only allowed on the last type qualifier
173 if (taSite1 == TypeArgumentKind.DIAMOND &&
174 innerClassDeclArity != InnerClassDeclArity.ONE) continue;
175 for (ArgumentKind arg1 : ArgumentKind.values()) {
176 if (innerClassDeclArity == innerClassDeclArity.ONE) {
177 new DiamondAndInnerClassTest(innerClassDeclArity, declType, newClassType,
178 argList, new TypeArgumentKind[] {taDecl1},
179 new TypeArgumentKind[] {taSite1}, new ArgumentKind[] {arg1}).run(comp, fm);
180 continue;
181 }
182 for (TypeArgumentKind taDecl2 : TypeArgumentKind.values()) {
183 //no rare types
184 if (isDeclRaw != (taDecl2 == TypeArgumentKind.NONE)) continue;
185 //no diamond on decl site
186 if (taDecl2 == TypeArgumentKind.DIAMOND) continue;
187 for (TypeArgumentKind taSite2 : TypeArgumentKind.values()) {
188 //no rare types
189 if (isSiteRaw != (taSite2 == TypeArgumentKind.NONE)) continue;
190 //diamond only allowed on the last type qualifier
191 if (taSite2 == TypeArgumentKind.DIAMOND &&
192 innerClassDeclArity != InnerClassDeclArity.TWO) continue;
193 for (ArgumentKind arg2 : ArgumentKind.values()) {
194 if (innerClassDeclArity == innerClassDeclArity.TWO) {
195 new DiamondAndInnerClassTest(innerClassDeclArity, declType, newClassType,
196 argList, new TypeArgumentKind[] {taDecl1, taDecl2},
197 new TypeArgumentKind[] {taSite1, taSite2},
198 new ArgumentKind[] {arg1, arg2}).run(comp, fm);
199 continue;
200 }
201 for (TypeArgumentKind taDecl3 : TypeArgumentKind.values()) {
202 //no rare types
203 if (isDeclRaw != (taDecl3 == TypeArgumentKind.NONE)) continue;
204 //no diamond on decl site
205 if (taDecl3 == TypeArgumentKind.DIAMOND) continue;
206 for (TypeArgumentKind taSite3 : TypeArgumentKind.values()) {
207 //no rare types
208 if (isSiteRaw != (taSite3 == TypeArgumentKind.NONE)) continue;
209 //diamond only allowed on the last type qualifier
210 if (taSite3 == TypeArgumentKind.DIAMOND &&
211 innerClassDeclArity != InnerClassDeclArity.THREE) continue;
212 for (ArgumentKind arg3 : ArgumentKind.values()) {
213 if (innerClassDeclArity == innerClassDeclArity.THREE) {
214 new DiamondAndInnerClassTest(innerClassDeclArity, declType, newClassType,
215 argList, new TypeArgumentKind[] {taDecl1, taDecl2, taDecl3},
216 new TypeArgumentKind[] {taSite1, taSite2, taSite3},
217 new ArgumentKind[] {arg1, arg2, arg3}).run(comp, fm);
218 continue;
219 }
220 }
221 }
222 }
223 }
224 }
225 }
226 }
227 }
228 }
229 }
230 }
231 }
232 }
233 System.out.println("Total check executed: " + checkCount);
234 }
236 InnerClassDeclArity innerClassDeclArity;
237 TypeQualifierArity declType;
238 TypeQualifierArity siteType;
239 ArgumentListArity argList;
240 TypeArgumentKind[] declTypeArgumentKinds;
241 TypeArgumentKind[] siteTypeArgumentKinds;
242 ArgumentKind[] argumentKinds;
243 JavaSource source;
244 DiagnosticChecker diagChecker;
246 DiamondAndInnerClassTest(InnerClassDeclArity innerClassDeclArity,
247 TypeQualifierArity declType, TypeQualifierArity siteType, ArgumentListArity argList,
248 TypeArgumentKind[] declTypeArgumentKinds, TypeArgumentKind[] siteTypeArgumentKinds,
249 ArgumentKind[] argumentKinds) {
250 this.innerClassDeclArity = innerClassDeclArity;
251 this.declType = declType;
252 this.siteType = siteType;
253 this.argList = argList;
254 this.declTypeArgumentKinds = declTypeArgumentKinds;
255 this.siteTypeArgumentKinds = siteTypeArgumentKinds;
256 this.argumentKinds = argumentKinds;
257 this.source = new JavaSource();
258 this.diagChecker = new DiagnosticChecker();
259 }
261 class JavaSource extends SimpleJavaFileObject {
263 String bodyTemplate = "#D res = new #S#AL;";
265 String source;
267 public JavaSource() {
268 super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
269 source = innerClassDeclArity.classDeclStr.replace("#B", bodyTemplate)
270 .replace("#D", declType.getType(declTypeArgumentKinds))
271 .replace("#S", siteType.getType(siteTypeArgumentKinds))
272 .replace("#AL", argList.getArgs(argumentKinds));
273 }
275 @Override
276 public CharSequence getCharContent(boolean ignoreEncodingErrors) {
277 return source;
278 }
279 }
281 void run(JavaCompiler tool, StandardJavaFileManager fm) throws Exception {
282 JavacTask ct = (JavacTask)tool.getTask(null, fm, diagChecker,
283 null, null, Arrays.asList(source));
284 try {
285 ct.analyze();
286 } catch (Throwable ex) {
287 throw new AssertionError("Error thrown when compiling the following code:\n" + source.getCharContent(true));
288 }
289 check();
290 }
292 void check() {
293 checkCount++;
295 boolean errorExpected = false;
297 TypeArgumentKind[] expectedArgKinds = new TypeArgumentKind[innerClassDeclArity.n];
299 for (int i = 0 ; i < innerClassDeclArity.n ; i++) {
300 if (!declTypeArgumentKinds[i].compatible(siteTypeArgumentKinds[i])) {
301 errorExpected = true;
302 break;
303 }
304 expectedArgKinds[i] = siteTypeArgumentKinds[i] == TypeArgumentKind.DIAMOND ?
305 declTypeArgumentKinds[i] : siteTypeArgumentKinds[i];
306 }
308 if (!errorExpected) {
309 for (int i = 0 ; i < innerClassDeclArity.n ; i++) {
310 //System.out.println("check " + expectedArgKinds[i] + " against " + argumentKinds[i]);
311 if (!expectedArgKinds[i].compatible(argumentKinds[i])) {
312 errorExpected = true;
313 break;
314 }
315 }
316 }
318 if (errorExpected != diagChecker.errorFound) {
319 throw new Error("invalid diagnostics for source:\n" +
320 source.getCharContent(true) +
321 "\nFound error: " + diagChecker.errorFound +
322 "\nExpected error: " + errorExpected);
323 }
324 }
326 static class DiagnosticChecker implements javax.tools.DiagnosticListener<JavaFileObject> {
328 boolean errorFound;
330 public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
331 if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
332 errorFound = true;
333 }
334 }
335 }
336 }