test/tools/javac/tree/DocCommentToplevelTest.java

changeset 0
959103a6100f
child 2525
2eb010b6cb22
equal deleted inserted replaced
-1:000000000000 0:959103a6100f
1 /*
2 * Copyright (c) 2011, 2012, 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 7096014
27 * @summary Javac tokens should retain state
28 */
29
30 import com.sun.source.tree.*;
31 import com.sun.source.util.*;
32 import com.sun.tools.javac.tree.DocCommentTable;
33 import com.sun.tools.javac.tree.JCTree;
34
35 import java.net.URI;
36 import java.util.*;
37 import javax.tools.*;
38
39
40 public class DocCommentToplevelTest {
41
42 enum PackageKind {
43 HAS_PKG("package pkg;"),
44 NO_PKG("");
45
46 String pkgStr;
47
48 PackageKind(String pkgStr) {
49 this.pkgStr = pkgStr;
50 }
51 }
52
53 enum ImportKind {
54 ZERO(""),
55 ONE("import java.lang.*;"),
56 TWO("import java.lang.*; import java.util.*;");
57
58 String importStr;
59
60 ImportKind(String importStr) {
61 this.importStr = importStr;
62 }
63 }
64
65 enum ModifierKind {
66 DEFAULT(""),
67 PUBLIC("public");
68
69 String modStr;
70
71 ModifierKind(String modStr) {
72 this.modStr = modStr;
73 }
74 }
75
76 enum ToplevelDocKind {
77 HAS_DOC("/** Toplevel! */"),
78 NO_DOC("");
79
80 String docStr;
81
82 ToplevelDocKind(String docStr) {
83 this.docStr = docStr;
84 }
85 }
86
87 static int errors;
88 static int checks;
89
90 public static void main(String... args) throws Exception {
91 //create default shared JavaCompiler - reused across multiple compilations
92 JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
93 StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null);
94
95 for (PackageKind pk : PackageKind.values()) {
96 for (ImportKind ik : ImportKind.values()) {
97 for (ModifierKind mk1 : ModifierKind.values()) {
98 for (ModifierKind mk2 : ModifierKind.values()) {
99 for (ToplevelDocKind tdk : ToplevelDocKind.values()) {
100 new DocCommentToplevelTest(pk, ik, mk1, mk2, tdk).run(comp, fm);
101 }
102 }
103 }
104 }
105 }
106
107 if (errors > 0)
108 throw new AssertionError(errors + " errors found");
109
110 System.out.println(checks + " checks were made");
111 }
112
113 PackageKind pk;
114 ImportKind ik;
115 ModifierKind mk1;
116 ModifierKind mk2;
117 ToplevelDocKind tdk;
118 JavaSource source;
119
120 DocCommentToplevelTest(PackageKind pk, ImportKind ik, ModifierKind mk1, ModifierKind mk2, ToplevelDocKind tdk) {
121 this.pk = pk;
122 this.ik = ik;
123 this.mk1 = mk1;
124 this.mk2 = mk2;
125 this.tdk = tdk;
126 source = new JavaSource();
127 }
128
129 void run(JavaCompiler comp, JavaFileManager fm) throws Exception {
130 JavacTask task = (JavacTask)comp.getTask(null, fm, null, Arrays.asList("-printsource"), null, Arrays.asList(source));
131 for (CompilationUnitTree cu: task.parse()) {
132 check(cu);
133 }
134 }
135
136 void check(CompilationUnitTree cu) {
137 checks++;
138
139 new TreeScanner<ClassTree,Void>() {
140
141 DocCommentTable docComments;
142
143 @Override
144 public ClassTree visitCompilationUnit(CompilationUnitTree node, Void unused) {
145 docComments = ((JCTree.JCCompilationUnit)node).docComments;
146 boolean expectedComment = tdk == ToplevelDocKind.HAS_DOC &&
147 (pk != PackageKind.NO_PKG || ik != ImportKind.ZERO);
148 boolean foundComment = docComments.hasComment((JCTree) node);
149 if (expectedComment != foundComment) {
150 error("Unexpected comment " + docComments.getComment((JCTree) node) + " on toplevel");
151 }
152 return super.visitCompilationUnit(node, null);
153 }
154
155 @Override
156 public ClassTree visitClass(ClassTree node, Void unused) {
157 boolean expectedComment = tdk == ToplevelDocKind.HAS_DOC &&
158 pk == PackageKind.NO_PKG && ik == ImportKind.ZERO &&
159 node.getSimpleName().toString().equals("First");
160 boolean foundComment = docComments.hasComment((JCTree) node);
161 if (expectedComment != foundComment) {
162 error("Unexpected comment " + docComments.getComment((JCTree) node) + " on class " + node.getSimpleName());
163 }
164 return super.visitClass(node, unused);
165 }
166 }.scan(cu, null);
167 }
168
169 void error(String msg) {
170 System.err.println("Error: " + msg);
171 System.err.println("Source: " + source.source);
172 errors++;
173 }
174
175 class JavaSource extends SimpleJavaFileObject {
176
177 String template = "#D\n#P\n#I\n" +
178 "#M1 class First { }\n" +
179 "#M2 class Second { }\n";
180
181 String source;
182
183 public JavaSource() {
184 super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
185 source = template.replace("#P", pk.pkgStr)
186 .replace("#I", ik.importStr)
187 .replace("#M1", mk1.modStr)
188 .replace("#M2", mk2.modStr)
189 .replace("#D", tdk.docStr);
190 }
191
192 @Override
193 public CharSequence getCharContent(boolean ignoreEncodingErrors) {
194 return source;
195 }
196 }
197 }

mercurial