Sat, 22 Dec 2018 13:10:38 -0800
Added tag jdk8u202-b25 for changeset 15d8c732039b
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 */
24 /*
25 * @test
26 * @bug 7096014
27 * @summary Javac tokens should retain state
28 */
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;
35 import java.net.URI;
36 import java.util.*;
37 import javax.tools.*;
40 public class DocCommentToplevelTest {
42 enum PackageKind {
43 HAS_PKG("package pkg;"),
44 NO_PKG("");
46 String pkgStr;
48 PackageKind(String pkgStr) {
49 this.pkgStr = pkgStr;
50 }
51 }
53 enum ImportKind {
54 ZERO(""),
55 ONE("import java.lang.*;"),
56 TWO("import java.lang.*; import java.util.*;");
58 String importStr;
60 ImportKind(String importStr) {
61 this.importStr = importStr;
62 }
63 }
65 enum ModifierKind {
66 DEFAULT(""),
67 PUBLIC("public");
69 String modStr;
71 ModifierKind(String modStr) {
72 this.modStr = modStr;
73 }
74 }
76 enum ToplevelDocKind {
77 HAS_DOC("/** Toplevel! */"),
78 NO_DOC("");
80 String docStr;
82 ToplevelDocKind(String docStr) {
83 this.docStr = docStr;
84 }
85 }
87 static int errors;
88 static int checks;
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);
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 }
107 if (errors > 0)
108 throw new AssertionError(errors + " errors found");
110 System.out.println(checks + " checks were made");
111 }
113 PackageKind pk;
114 ImportKind ik;
115 ModifierKind mk1;
116 ModifierKind mk2;
117 ToplevelDocKind tdk;
118 JavaSource source;
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 }
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 }
136 void check(CompilationUnitTree cu) {
137 checks++;
139 new TreeScanner<ClassTree,Void>() {
141 DocCommentTable docComments;
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 }
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 }
169 void error(String msg) {
170 System.err.println("Error: " + msg);
171 System.err.println("Source: " + source.source);
172 errors++;
173 }
175 class JavaSource extends SimpleJavaFileObject {
177 String template = "#D\n#P\n#I\n" +
178 "#M1 class First { }\n" +
179 "#M2 class Second { }\n";
181 String source;
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 }
192 @Override
193 public CharSequence getCharContent(boolean ignoreEncodingErrors) {
194 return source;
195 }
196 }
197 }