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