1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/tree/DocCommentToplevelTest.java Wed Apr 27 01:34:52 2016 +0800 1.3 @@ -0,0 +1,197 @@ 1.4 +/* 1.5 + * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + */ 1.26 + 1.27 +/* 1.28 + * @test 1.29 + * @bug 7096014 1.30 + * @summary Javac tokens should retain state 1.31 + */ 1.32 + 1.33 +import com.sun.source.tree.*; 1.34 +import com.sun.source.util.*; 1.35 +import com.sun.tools.javac.tree.DocCommentTable; 1.36 +import com.sun.tools.javac.tree.JCTree; 1.37 + 1.38 +import java.net.URI; 1.39 +import java.util.*; 1.40 +import javax.tools.*; 1.41 + 1.42 + 1.43 +public class DocCommentToplevelTest { 1.44 + 1.45 + enum PackageKind { 1.46 + HAS_PKG("package pkg;"), 1.47 + NO_PKG(""); 1.48 + 1.49 + String pkgStr; 1.50 + 1.51 + PackageKind(String pkgStr) { 1.52 + this.pkgStr = pkgStr; 1.53 + } 1.54 + } 1.55 + 1.56 + enum ImportKind { 1.57 + ZERO(""), 1.58 + ONE("import java.lang.*;"), 1.59 + TWO("import java.lang.*; import java.util.*;"); 1.60 + 1.61 + String importStr; 1.62 + 1.63 + ImportKind(String importStr) { 1.64 + this.importStr = importStr; 1.65 + } 1.66 + } 1.67 + 1.68 + enum ModifierKind { 1.69 + DEFAULT(""), 1.70 + PUBLIC("public"); 1.71 + 1.72 + String modStr; 1.73 + 1.74 + ModifierKind(String modStr) { 1.75 + this.modStr = modStr; 1.76 + } 1.77 + } 1.78 + 1.79 + enum ToplevelDocKind { 1.80 + HAS_DOC("/** Toplevel! */"), 1.81 + NO_DOC(""); 1.82 + 1.83 + String docStr; 1.84 + 1.85 + ToplevelDocKind(String docStr) { 1.86 + this.docStr = docStr; 1.87 + } 1.88 + } 1.89 + 1.90 + static int errors; 1.91 + static int checks; 1.92 + 1.93 + public static void main(String... args) throws Exception { 1.94 + //create default shared JavaCompiler - reused across multiple compilations 1.95 + JavaCompiler comp = ToolProvider.getSystemJavaCompiler(); 1.96 + StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null); 1.97 + 1.98 + for (PackageKind pk : PackageKind.values()) { 1.99 + for (ImportKind ik : ImportKind.values()) { 1.100 + for (ModifierKind mk1 : ModifierKind.values()) { 1.101 + for (ModifierKind mk2 : ModifierKind.values()) { 1.102 + for (ToplevelDocKind tdk : ToplevelDocKind.values()) { 1.103 + new DocCommentToplevelTest(pk, ik, mk1, mk2, tdk).run(comp, fm); 1.104 + } 1.105 + } 1.106 + } 1.107 + } 1.108 + } 1.109 + 1.110 + if (errors > 0) 1.111 + throw new AssertionError(errors + " errors found"); 1.112 + 1.113 + System.out.println(checks + " checks were made"); 1.114 + } 1.115 + 1.116 + PackageKind pk; 1.117 + ImportKind ik; 1.118 + ModifierKind mk1; 1.119 + ModifierKind mk2; 1.120 + ToplevelDocKind tdk; 1.121 + JavaSource source; 1.122 + 1.123 + DocCommentToplevelTest(PackageKind pk, ImportKind ik, ModifierKind mk1, ModifierKind mk2, ToplevelDocKind tdk) { 1.124 + this.pk = pk; 1.125 + this.ik = ik; 1.126 + this.mk1 = mk1; 1.127 + this.mk2 = mk2; 1.128 + this.tdk = tdk; 1.129 + source = new JavaSource(); 1.130 + } 1.131 + 1.132 + void run(JavaCompiler comp, JavaFileManager fm) throws Exception { 1.133 + JavacTask task = (JavacTask)comp.getTask(null, fm, null, Arrays.asList("-printsource"), null, Arrays.asList(source)); 1.134 + for (CompilationUnitTree cu: task.parse()) { 1.135 + check(cu); 1.136 + } 1.137 + } 1.138 + 1.139 + void check(CompilationUnitTree cu) { 1.140 + checks++; 1.141 + 1.142 + new TreeScanner<ClassTree,Void>() { 1.143 + 1.144 + DocCommentTable docComments; 1.145 + 1.146 + @Override 1.147 + public ClassTree visitCompilationUnit(CompilationUnitTree node, Void unused) { 1.148 + docComments = ((JCTree.JCCompilationUnit)node).docComments; 1.149 + boolean expectedComment = tdk == ToplevelDocKind.HAS_DOC && 1.150 + (pk != PackageKind.NO_PKG || ik != ImportKind.ZERO); 1.151 + boolean foundComment = docComments.hasComment((JCTree) node); 1.152 + if (expectedComment != foundComment) { 1.153 + error("Unexpected comment " + docComments.getComment((JCTree) node) + " on toplevel"); 1.154 + } 1.155 + return super.visitCompilationUnit(node, null); 1.156 + } 1.157 + 1.158 + @Override 1.159 + public ClassTree visitClass(ClassTree node, Void unused) { 1.160 + boolean expectedComment = tdk == ToplevelDocKind.HAS_DOC && 1.161 + pk == PackageKind.NO_PKG && ik == ImportKind.ZERO && 1.162 + node.getSimpleName().toString().equals("First"); 1.163 + boolean foundComment = docComments.hasComment((JCTree) node); 1.164 + if (expectedComment != foundComment) { 1.165 + error("Unexpected comment " + docComments.getComment((JCTree) node) + " on class " + node.getSimpleName()); 1.166 + } 1.167 + return super.visitClass(node, unused); 1.168 + } 1.169 + }.scan(cu, null); 1.170 + } 1.171 + 1.172 + void error(String msg) { 1.173 + System.err.println("Error: " + msg); 1.174 + System.err.println("Source: " + source.source); 1.175 + errors++; 1.176 + } 1.177 + 1.178 + class JavaSource extends SimpleJavaFileObject { 1.179 + 1.180 + String template = "#D\n#P\n#I\n" + 1.181 + "#M1 class First { }\n" + 1.182 + "#M2 class Second { }\n"; 1.183 + 1.184 + String source; 1.185 + 1.186 + public JavaSource() { 1.187 + super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); 1.188 + source = template.replace("#P", pk.pkgStr) 1.189 + .replace("#I", ik.importStr) 1.190 + .replace("#M1", mk1.modStr) 1.191 + .replace("#M2", mk2.modStr) 1.192 + .replace("#D", tdk.docStr); 1.193 + } 1.194 + 1.195 + @Override 1.196 + public CharSequence getCharContent(boolean ignoreEncodingErrors) { 1.197 + return source; 1.198 + } 1.199 + } 1.200 +}