Thu, 07 Jun 2012 20:30:57 -0700
7170079: Adjustments to build-infra makefiles
Reviewed-by: ohair, ohrstrom, ihse, jonas
Contributed-by: jonas <jonas.oreland@oracle.com>, erikj <erik.joelsson@oracle.com>, ihse <magnus.ihse.bursie@oracle.com>, tgranat <torbjorn.granat@oracle.com>, ykantser <yekaterina.kantserova@oracle.com>
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 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.JCTree;
34 import java.net.URI;
35 import java.util.*;
36 import javax.tools.*;
39 public class DocCommentToplevelTest {
41 enum PackageKind {
42 HAS_PKG("package pkg;"),
43 NO_PKG("");
45 String pkgStr;
47 PackageKind(String pkgStr) {
48 this.pkgStr = pkgStr;
49 }
50 }
52 enum ImportKind {
53 ZERO(""),
54 ONE("import java.lang.*;"),
55 TWO("import java.lang.*; import java.util.*;");
57 String importStr;
59 ImportKind(String importStr) {
60 this.importStr = importStr;
61 }
62 }
64 enum ModifierKind {
65 DEFAULT(""),
66 PUBLIC("public");
68 String modStr;
70 ModifierKind(String modStr) {
71 this.modStr = modStr;
72 }
73 }
75 enum ToplevelDocKind {
76 HAS_DOC("/** Toplevel! */"),
77 NO_DOC("");
79 String docStr;
81 ToplevelDocKind(String docStr) {
82 this.docStr = docStr;
83 }
84 }
86 static int errors;
87 static int checks;
89 public static void main(String... args) throws Exception {
90 //create default shared JavaCompiler - reused across multiple compilations
91 JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
92 StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null);
94 for (PackageKind pk : PackageKind.values()) {
95 for (ImportKind ik : ImportKind.values()) {
96 for (ModifierKind mk1 : ModifierKind.values()) {
97 for (ModifierKind mk2 : ModifierKind.values()) {
98 for (ToplevelDocKind tdk : ToplevelDocKind.values()) {
99 new DocCommentToplevelTest(pk, ik, mk1, mk2, tdk).run(comp, fm);
100 }
101 }
102 }
103 }
104 }
106 if (errors > 0)
107 throw new AssertionError(errors + " errors found");
109 System.out.println(checks + " checks were made");
110 }
112 PackageKind pk;
113 ImportKind ik;
114 ModifierKind mk1;
115 ModifierKind mk2;
116 ToplevelDocKind tdk;
117 JavaSource source;
119 DocCommentToplevelTest(PackageKind pk, ImportKind ik, ModifierKind mk1, ModifierKind mk2, ToplevelDocKind tdk) {
120 this.pk = pk;
121 this.ik = ik;
122 this.mk1 = mk1;
123 this.mk2 = mk2;
124 this.tdk = tdk;
125 source = new JavaSource();
126 }
128 void run(JavaCompiler comp, JavaFileManager fm) throws Exception {
129 JavacTask task = (JavacTask)comp.getTask(null, fm, null, Arrays.asList("-printsource"), null, Arrays.asList(source));
130 for (CompilationUnitTree cu: task.parse()) {
131 check(cu);
132 }
133 }
135 void check(CompilationUnitTree cu) {
136 checks++;
138 new TreeScanner<ClassTree,Void>() {
140 Map<JCTree, String> docComments;
142 @Override
143 public ClassTree visitCompilationUnit(CompilationUnitTree node, Void unused) {
144 docComments = ((JCTree.JCCompilationUnit)node).docComments;
145 boolean expectedComment = tdk == ToplevelDocKind.HAS_DOC &&
146 (pk != PackageKind.NO_PKG || ik != ImportKind.ZERO);
147 boolean foundComment = docComments.get(node) != null;
148 if (expectedComment != foundComment) {
149 error("Unexpected comment " + docComments.get(node) + " on toplevel");
150 }
151 return super.visitCompilationUnit(node, null);
152 }
154 @Override
155 public ClassTree visitClass(ClassTree node, Void unused) {
156 boolean expectedComment = tdk == ToplevelDocKind.HAS_DOC &&
157 pk == PackageKind.NO_PKG && ik == ImportKind.ZERO &&
158 node.getSimpleName().toString().equals("First");
159 boolean foundComment = docComments.get(node) != null;
160 if (expectedComment != foundComment) {
161 error("Unexpected comment " + docComments.get(node) + " on class " + node.getSimpleName());
162 }
163 return super.visitClass(node, unused);
164 }
165 }.scan(cu, null);
166 }
168 void error(String msg) {
169 System.err.println("Error: " + msg);
170 System.err.println("Source: " + source.source);
171 errors++;
172 }
174 class JavaSource extends SimpleJavaFileObject {
176 String template = "#D\n#P\n#I\n" +
177 "#M1 class First { }\n" +
178 "#M2 class Second { }\n";
180 String source;
182 public JavaSource() {
183 super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
184 source = template.replace("#P", pk.pkgStr)
185 .replace("#I", ik.importStr)
186 .replace("#M1", mk1.modStr)
187 .replace("#M2", mk2.modStr)
188 .replace("#D", tdk.docStr);
189 }
191 @Override
192 public CharSequence getCharContent(boolean ignoreEncodingErrors) {
193 return source;
194 }
195 }
196 }