mcimadamore@1113: /* jjg@1280: * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved. mcimadamore@1113: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. mcimadamore@1113: * mcimadamore@1113: * This code is free software; you can redistribute it and/or modify it mcimadamore@1113: * under the terms of the GNU General Public License version 2 only, as mcimadamore@1113: * published by the Free Software Foundation. mcimadamore@1113: * mcimadamore@1113: * This code is distributed in the hope that it will be useful, but WITHOUT mcimadamore@1113: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or mcimadamore@1113: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License mcimadamore@1113: * version 2 for more details (a copy is included in the LICENSE file that mcimadamore@1113: * accompanied this code). mcimadamore@1113: * mcimadamore@1113: * You should have received a copy of the GNU General Public License version mcimadamore@1113: * 2 along with this work; if not, write to the Free Software Foundation, mcimadamore@1113: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. mcimadamore@1113: * mcimadamore@1113: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA mcimadamore@1113: * or visit www.oracle.com if you need additional information or have any mcimadamore@1113: * questions. mcimadamore@1113: */ mcimadamore@1113: mcimadamore@1113: /* mcimadamore@1113: * @test mcimadamore@1113: * @bug 7096014 mcimadamore@1113: * @summary Javac tokens should retain state mcimadamore@1113: */ mcimadamore@1113: mcimadamore@1113: import com.sun.source.tree.*; mcimadamore@1113: import com.sun.source.util.*; jjg@1280: import com.sun.tools.javac.tree.DocCommentTable; mcimadamore@1113: import com.sun.tools.javac.tree.JCTree; mcimadamore@1113: mcimadamore@1113: import java.net.URI; mcimadamore@1113: import java.util.*; mcimadamore@1113: import javax.tools.*; mcimadamore@1113: mcimadamore@1113: mcimadamore@1113: public class DocCommentToplevelTest { mcimadamore@1113: mcimadamore@1113: enum PackageKind { mcimadamore@1113: HAS_PKG("package pkg;"), mcimadamore@1113: NO_PKG(""); mcimadamore@1113: mcimadamore@1113: String pkgStr; mcimadamore@1113: mcimadamore@1113: PackageKind(String pkgStr) { mcimadamore@1113: this.pkgStr = pkgStr; mcimadamore@1113: } mcimadamore@1113: } mcimadamore@1113: mcimadamore@1113: enum ImportKind { mcimadamore@1113: ZERO(""), mcimadamore@1113: ONE("import java.lang.*;"), mcimadamore@1113: TWO("import java.lang.*; import java.util.*;"); mcimadamore@1113: mcimadamore@1113: String importStr; mcimadamore@1113: mcimadamore@1113: ImportKind(String importStr) { mcimadamore@1113: this.importStr = importStr; mcimadamore@1113: } mcimadamore@1113: } mcimadamore@1113: mcimadamore@1113: enum ModifierKind { mcimadamore@1113: DEFAULT(""), mcimadamore@1113: PUBLIC("public"); mcimadamore@1113: mcimadamore@1113: String modStr; mcimadamore@1113: mcimadamore@1113: ModifierKind(String modStr) { mcimadamore@1113: this.modStr = modStr; mcimadamore@1113: } mcimadamore@1113: } mcimadamore@1113: mcimadamore@1113: enum ToplevelDocKind { mcimadamore@1113: HAS_DOC("/** Toplevel! */"), mcimadamore@1113: NO_DOC(""); mcimadamore@1113: mcimadamore@1113: String docStr; mcimadamore@1113: mcimadamore@1113: ToplevelDocKind(String docStr) { mcimadamore@1113: this.docStr = docStr; mcimadamore@1113: } mcimadamore@1113: } mcimadamore@1113: mcimadamore@1113: static int errors; mcimadamore@1113: static int checks; mcimadamore@1113: mcimadamore@1113: public static void main(String... args) throws Exception { mcimadamore@1113: //create default shared JavaCompiler - reused across multiple compilations mcimadamore@1113: JavaCompiler comp = ToolProvider.getSystemJavaCompiler(); mcimadamore@1113: StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null); mcimadamore@1113: mcimadamore@1113: for (PackageKind pk : PackageKind.values()) { mcimadamore@1113: for (ImportKind ik : ImportKind.values()) { mcimadamore@1113: for (ModifierKind mk1 : ModifierKind.values()) { mcimadamore@1113: for (ModifierKind mk2 : ModifierKind.values()) { mcimadamore@1113: for (ToplevelDocKind tdk : ToplevelDocKind.values()) { mcimadamore@1113: new DocCommentToplevelTest(pk, ik, mk1, mk2, tdk).run(comp, fm); mcimadamore@1113: } mcimadamore@1113: } mcimadamore@1113: } mcimadamore@1113: } mcimadamore@1113: } mcimadamore@1113: mcimadamore@1113: if (errors > 0) mcimadamore@1113: throw new AssertionError(errors + " errors found"); mcimadamore@1113: mcimadamore@1113: System.out.println(checks + " checks were made"); mcimadamore@1113: } mcimadamore@1113: mcimadamore@1113: PackageKind pk; mcimadamore@1113: ImportKind ik; mcimadamore@1113: ModifierKind mk1; mcimadamore@1113: ModifierKind mk2; mcimadamore@1113: ToplevelDocKind tdk; mcimadamore@1113: JavaSource source; mcimadamore@1113: mcimadamore@1113: DocCommentToplevelTest(PackageKind pk, ImportKind ik, ModifierKind mk1, ModifierKind mk2, ToplevelDocKind tdk) { mcimadamore@1113: this.pk = pk; mcimadamore@1113: this.ik = ik; mcimadamore@1113: this.mk1 = mk1; mcimadamore@1113: this.mk2 = mk2; mcimadamore@1113: this.tdk = tdk; mcimadamore@1113: source = new JavaSource(); mcimadamore@1113: } mcimadamore@1113: mcimadamore@1113: void run(JavaCompiler comp, JavaFileManager fm) throws Exception { mcimadamore@1113: JavacTask task = (JavacTask)comp.getTask(null, fm, null, Arrays.asList("-printsource"), null, Arrays.asList(source)); mcimadamore@1113: for (CompilationUnitTree cu: task.parse()) { mcimadamore@1113: check(cu); mcimadamore@1113: } mcimadamore@1113: } mcimadamore@1113: mcimadamore@1113: void check(CompilationUnitTree cu) { mcimadamore@1113: checks++; mcimadamore@1113: mcimadamore@1113: new TreeScanner() { mcimadamore@1113: jjg@1280: DocCommentTable docComments; mcimadamore@1113: mcimadamore@1113: @Override mcimadamore@1113: public ClassTree visitCompilationUnit(CompilationUnitTree node, Void unused) { mcimadamore@1113: docComments = ((JCTree.JCCompilationUnit)node).docComments; mcimadamore@1113: boolean expectedComment = tdk == ToplevelDocKind.HAS_DOC && mcimadamore@1113: (pk != PackageKind.NO_PKG || ik != ImportKind.ZERO); jjg@1280: boolean foundComment = docComments.hasComment((JCTree) node); mcimadamore@1113: if (expectedComment != foundComment) { jjg@1280: error("Unexpected comment " + docComments.getComment((JCTree) node) + " on toplevel"); mcimadamore@1113: } mcimadamore@1113: return super.visitCompilationUnit(node, null); mcimadamore@1113: } mcimadamore@1113: mcimadamore@1113: @Override mcimadamore@1113: public ClassTree visitClass(ClassTree node, Void unused) { mcimadamore@1113: boolean expectedComment = tdk == ToplevelDocKind.HAS_DOC && mcimadamore@1113: pk == PackageKind.NO_PKG && ik == ImportKind.ZERO && mcimadamore@1113: node.getSimpleName().toString().equals("First"); jjg@1280: boolean foundComment = docComments.hasComment((JCTree) node); mcimadamore@1113: if (expectedComment != foundComment) { jjg@1280: error("Unexpected comment " + docComments.getComment((JCTree) node) + " on class " + node.getSimpleName()); mcimadamore@1113: } mcimadamore@1113: return super.visitClass(node, unused); mcimadamore@1113: } mcimadamore@1113: }.scan(cu, null); mcimadamore@1113: } mcimadamore@1113: mcimadamore@1113: void error(String msg) { mcimadamore@1113: System.err.println("Error: " + msg); mcimadamore@1113: System.err.println("Source: " + source.source); mcimadamore@1113: errors++; mcimadamore@1113: } mcimadamore@1113: mcimadamore@1113: class JavaSource extends SimpleJavaFileObject { mcimadamore@1113: mcimadamore@1113: String template = "#D\n#P\n#I\n" + mcimadamore@1113: "#M1 class First { }\n" + mcimadamore@1113: "#M2 class Second { }\n"; mcimadamore@1113: mcimadamore@1113: String source; mcimadamore@1113: mcimadamore@1113: public JavaSource() { mcimadamore@1113: super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); mcimadamore@1113: source = template.replace("#P", pk.pkgStr) mcimadamore@1113: .replace("#I", ik.importStr) mcimadamore@1113: .replace("#M1", mk1.modStr) mcimadamore@1113: .replace("#M2", mk2.modStr) mcimadamore@1113: .replace("#D", tdk.docStr); mcimadamore@1113: } mcimadamore@1113: mcimadamore@1113: @Override mcimadamore@1113: public CharSequence getCharContent(boolean ignoreEncodingErrors) { mcimadamore@1113: return source; mcimadamore@1113: } mcimadamore@1113: } mcimadamore@1113: }