aoqi@0: /* aoqi@0: * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: import java.io.File; aoqi@0: import java.io.FileWriter; aoqi@0: import java.io.IOException; aoqi@0: import java.io.PrintWriter; aoqi@0: import java.io.StringWriter; aoqi@0: import java.io.Writer; aoqi@0: import java.util.ArrayList; aoqi@0: import java.util.List; aoqi@0: import java.util.regex.Matcher; aoqi@0: import java.util.regex.Pattern; aoqi@0: aoqi@0: import javax.lang.model.element.Name; aoqi@0: import javax.tools.JavaFileObject; aoqi@0: import javax.tools.StandardJavaFileManager; aoqi@0: aoqi@0: import com.sun.source.doctree.*; aoqi@0: import com.sun.source.tree.ClassTree; aoqi@0: import com.sun.source.tree.CompilationUnitTree; aoqi@0: import com.sun.source.tree.MethodTree; aoqi@0: import com.sun.source.tree.Tree; aoqi@0: import com.sun.source.tree.VariableTree; aoqi@0: import com.sun.source.util.DocTreeScanner; aoqi@0: import com.sun.source.util.DocTrees; aoqi@0: import com.sun.source.util.JavacTask; aoqi@0: import com.sun.source.util.TreePath; aoqi@0: import com.sun.source.util.TreePathScanner; aoqi@0: import com.sun.tools.javac.api.JavacTool; aoqi@0: import com.sun.tools.javac.tree.DCTree; aoqi@0: import com.sun.tools.javac.tree.DCTree.DCDocComment; aoqi@0: import com.sun.tools.javac.tree.DCTree.DCErroneous; aoqi@0: import com.sun.tools.javac.tree.DocPretty; aoqi@0: aoqi@0: public class DocCommentTester { aoqi@0: aoqi@0: public static void main(String... args) throws Exception { aoqi@0: new DocCommentTester().run(args); aoqi@0: } aoqi@0: aoqi@0: public void run(String... args) throws Exception { aoqi@0: String testSrc = System.getProperty("test.src"); aoqi@0: aoqi@0: List files = new ArrayList(); aoqi@0: for (String arg: args) aoqi@0: files.add(new File(testSrc, arg)); aoqi@0: aoqi@0: JavacTool javac = JavacTool.create(); aoqi@0: StandardJavaFileManager fm = javac.getStandardFileManager(null, null, null); aoqi@0: aoqi@0: Iterable fos = fm.getJavaFileObjectsFromFiles(files); aoqi@0: aoqi@0: JavacTask t = javac.getTask(null, fm, null, null, null, fos); aoqi@0: final DocTrees trees = DocTrees.instance(t); aoqi@0: aoqi@0: final Checker[] checkers = { aoqi@0: new ASTChecker(this, trees), aoqi@0: new PosChecker(this, trees), aoqi@0: new PrettyChecker(this, trees) aoqi@0: }; aoqi@0: aoqi@0: DeclScanner d = new DeclScanner() { aoqi@0: @Override aoqi@0: public Void visitCompilationUnit(CompilationUnitTree tree, Void ignore) { aoqi@0: for (Checker c: checkers) aoqi@0: c.visitCompilationUnit(tree); aoqi@0: return super.visitCompilationUnit(tree, ignore); aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: void visitDecl(Tree tree, Name name) { aoqi@0: TreePath path = getCurrentPath(); aoqi@0: String dc = trees.getDocComment(path); aoqi@0: if (dc != null) { aoqi@0: for (Checker c : checkers) { aoqi@0: try { aoqi@0: System.err.println(path.getLeaf().getKind() aoqi@0: + " " + name aoqi@0: + " " + c.getClass().getSimpleName()); aoqi@0: aoqi@0: c.check(path, name); aoqi@0: aoqi@0: System.err.println(); aoqi@0: } catch (Exception e) { aoqi@0: error("Exception " + e); aoqi@0: e.printStackTrace(System.err); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: Iterable units = t.parse(); aoqi@0: for (CompilationUnitTree unit: units) { aoqi@0: d.scan(unit, null); aoqi@0: } aoqi@0: aoqi@0: if (errors > 0) aoqi@0: throw new Exception(errors + " errors occurred"); aoqi@0: } aoqi@0: aoqi@0: static abstract class DeclScanner extends TreePathScanner { aoqi@0: abstract void visitDecl(Tree tree, Name name); aoqi@0: aoqi@0: @Override aoqi@0: public Void visitClass(ClassTree tree, Void ignore) { aoqi@0: super.visitClass(tree, ignore); aoqi@0: visitDecl(tree, tree.getSimpleName()); aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public Void visitMethod(MethodTree tree, Void ignore) { aoqi@0: super.visitMethod(tree, ignore); aoqi@0: visitDecl(tree, tree.getName()); aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public Void visitVariable(VariableTree tree, Void ignore) { aoqi@0: super.visitVariable(tree, ignore); aoqi@0: visitDecl(tree, tree.getName()); aoqi@0: return null; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Base class for checkers to check the doc comment on a declaration aoqi@0: * (when present.) aoqi@0: */ aoqi@0: abstract class Checker { aoqi@0: final DocTrees trees; aoqi@0: aoqi@0: Checker(DocTrees trees) { aoqi@0: this.trees = trees; aoqi@0: } aoqi@0: aoqi@0: void visitCompilationUnit(CompilationUnitTree tree) { } aoqi@0: aoqi@0: abstract void check(TreePath tree, Name name) throws Exception; aoqi@0: aoqi@0: void error(String msg) { aoqi@0: DocCommentTester.this.error(msg); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void error(String msg) { aoqi@0: System.err.println("Error: " + msg); aoqi@0: errors++; aoqi@0: } aoqi@0: aoqi@0: int errors; aoqi@0: aoqi@0: /** aoqi@0: * Verify the structure of the DocTree AST by comparing it against golden text. aoqi@0: */ aoqi@0: static class ASTChecker extends Checker { aoqi@0: static final String NEWLINE = System.getProperty("line.separator"); aoqi@0: Printer printer = new Printer(); aoqi@0: String source; aoqi@0: aoqi@0: ASTChecker(DocCommentTester test, DocTrees t) { aoqi@0: test.super(t); aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: void visitCompilationUnit(CompilationUnitTree tree) { aoqi@0: try { aoqi@0: source = tree.getSourceFile().getCharContent(true).toString(); aoqi@0: } catch (IOException e) { aoqi@0: source = ""; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void check(TreePath path, Name name) { aoqi@0: StringWriter out = new StringWriter(); aoqi@0: DocCommentTree dc = trees.getDocCommentTree(path); aoqi@0: printer.print(dc, out); aoqi@0: out.flush(); aoqi@0: String found = out.toString().replace(NEWLINE, "\n"); aoqi@0: aoqi@0: // Look for the first block comment after the first occurrence of name aoqi@0: int start = source.indexOf("\n/*\n", findName(source, name)); aoqi@0: int end = source.indexOf("\n*/\n", start); aoqi@0: String expect = source.substring(start + 4, end + 1); aoqi@0: if (!found.equals(expect)) { aoqi@0: System.err.println("Expect:\n" + expect); aoqi@0: System.err.println("Found:\n" + found); aoqi@0: error("AST mismatch for " + name); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * This main program is to set up the golden comments used by this aoqi@0: * checker. aoqi@0: * Usage: aoqi@0: * java DocCommentTester$ASTChecker -o dir file... aoqi@0: * The given files are written to the output directory with their aoqi@0: * golden comments updated. The intent is that the files should aoqi@0: * then be compared with the originals, e.g. with meld, and if the aoqi@0: * changes are approved, the new files can be used to replace the old. aoqi@0: */ aoqi@0: public static void main(String... args) throws Exception { aoqi@0: List files = new ArrayList(); aoqi@0: File o = null; aoqi@0: for (int i = 0; i < args.length; i++) { aoqi@0: String arg = args[i]; aoqi@0: if (arg.equals("-o")) aoqi@0: o = new File(args[++i]); aoqi@0: else if (arg.startsWith("-")) aoqi@0: throw new IllegalArgumentException(arg); aoqi@0: else { aoqi@0: files.add(new File(arg)); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: if (o == null) aoqi@0: throw new IllegalArgumentException("no output dir specified"); aoqi@0: final File outDir = o; aoqi@0: aoqi@0: JavacTool javac = JavacTool.create(); aoqi@0: StandardJavaFileManager fm = javac.getStandardFileManager(null, null, null); aoqi@0: Iterable fos = fm.getJavaFileObjectsFromFiles(files); aoqi@0: aoqi@0: JavacTask t = javac.getTask(null, fm, null, null, null, fos); aoqi@0: final DocTrees trees = DocTrees.instance(t); aoqi@0: aoqi@0: DeclScanner d = new DeclScanner() { aoqi@0: Printer p = new Printer(); aoqi@0: String source; aoqi@0: aoqi@0: @Override aoqi@0: public Void visitCompilationUnit(CompilationUnitTree tree, Void ignore) { aoqi@0: System.err.println("processing " + tree.getSourceFile().getName()); aoqi@0: try { aoqi@0: source = tree.getSourceFile().getCharContent(true).toString(); aoqi@0: } catch (IOException e) { aoqi@0: source = ""; aoqi@0: } aoqi@0: aoqi@0: // remove existing gold by removing all block comments after the first '{'. aoqi@0: int start = source.indexOf("{"); aoqi@0: while ((start = source.indexOf("\n/*\n", start)) != -1) { aoqi@0: int end = source.indexOf("\n*/\n"); aoqi@0: source = source.substring(0, start + 1) + source.substring(end + 4); aoqi@0: } aoqi@0: aoqi@0: // process decls in compilation unit aoqi@0: super.visitCompilationUnit(tree, ignore); aoqi@0: aoqi@0: // write the modified source aoqi@0: File f = new File(tree.getSourceFile().getName()); aoqi@0: File outFile = new File(outDir, f.getName()); aoqi@0: try { aoqi@0: FileWriter out = new FileWriter(outFile); aoqi@0: try { aoqi@0: out.write(source); aoqi@0: } finally { aoqi@0: out.close(); aoqi@0: } aoqi@0: } catch (IOException e) { aoqi@0: System.err.println("Can't write " + tree.getSourceFile().getName() aoqi@0: + " to " + outFile + ": " + e); aoqi@0: } aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: void visitDecl(Tree tree, Name name) { aoqi@0: DocTree dc = trees.getDocCommentTree(getCurrentPath()); aoqi@0: if (dc != null) { aoqi@0: StringWriter out = new StringWriter(); aoqi@0: p.print(dc, out); aoqi@0: String found = out.toString(); aoqi@0: aoqi@0: // Look for the empty line after the first occurrence of name aoqi@0: int pos = source.indexOf("\n\n", findName(source, name)); aoqi@0: aoqi@0: // Insert the golden comment aoqi@0: source = source.substring(0, pos) aoqi@0: + "\n/*\n" aoqi@0: + found aoqi@0: + "*/" aoqi@0: + source.substring(pos); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: }; aoqi@0: aoqi@0: Iterable units = t.parse(); aoqi@0: for (CompilationUnitTree unit: units) { aoqi@0: d.scan(unit, null); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: static int findName(String source, Name name) { aoqi@0: Pattern p = Pattern.compile("\\s" + name + "[(;]"); aoqi@0: Matcher m = p.matcher(source); aoqi@0: if (!m.find()) aoqi@0: throw new Error("cannot find " + name); aoqi@0: return m.start(); aoqi@0: } aoqi@0: aoqi@0: static class Printer implements DocTreeVisitor { aoqi@0: PrintWriter out; aoqi@0: aoqi@0: void print(DocTree tree, Writer out) { aoqi@0: this.out = (out instanceof PrintWriter) aoqi@0: ? (PrintWriter) out : new PrintWriter(out); aoqi@0: tree.accept(this, null); aoqi@0: this.out.flush(); aoqi@0: } aoqi@0: aoqi@0: public Void visitAttribute(AttributeTree node, Void p) { aoqi@0: header(node); aoqi@0: indent(+1); aoqi@0: print("name", node.getName().toString()); aoqi@0: print("vkind", node.getValueKind().toString()); aoqi@0: print("value", node.getValue()); aoqi@0: indent(-1); aoqi@0: indent(); aoqi@0: out.println("]"); aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: public Void visitAuthor(AuthorTree node, Void p) { aoqi@0: header(node); aoqi@0: indent(+1); aoqi@0: print("name", node.getName()); aoqi@0: indent(-1); aoqi@0: indent(); aoqi@0: out.println("]"); aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: public Void visitComment(CommentTree node, Void p) { aoqi@0: header(node, compress(node.getBody())); aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: public Void visitDeprecated(DeprecatedTree node, Void p) { aoqi@0: header(node); aoqi@0: indent(+1); aoqi@0: print("body", node.getBody()); aoqi@0: indent(-1); aoqi@0: indent(); aoqi@0: out.println("]"); aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: public Void visitDocComment(DocCommentTree node, Void p) { aoqi@0: header(node); aoqi@0: indent(+1); aoqi@0: print("firstSentence", node.getFirstSentence()); aoqi@0: print("body", node.getBody()); aoqi@0: print("block tags", node.getBlockTags()); aoqi@0: indent(-1); aoqi@0: indent(); aoqi@0: out.println("]"); aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: public Void visitDocRoot(DocRootTree node, Void p) { aoqi@0: header(node, ""); aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: public Void visitEndElement(EndElementTree node, Void p) { aoqi@0: header(node, node.getName().toString()); aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: public Void visitEntity(EntityTree node, Void p) { aoqi@0: header(node, node.getName().toString()); aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: public Void visitErroneous(ErroneousTree node, Void p) { aoqi@0: header(node); aoqi@0: indent(+1); aoqi@0: print("code", ((DCErroneous) node).diag.getCode()); aoqi@0: print("body", compress(node.getBody())); aoqi@0: indent(-1); aoqi@0: indent(); aoqi@0: out.println("]"); aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: public Void visitIdentifier(IdentifierTree node, Void p) { aoqi@0: header(node, compress(node.getName().toString())); aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: public Void visitInheritDoc(InheritDocTree node, Void p) { aoqi@0: header(node, ""); aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: public Void visitLink(LinkTree node, Void p) { aoqi@0: header(node); aoqi@0: indent(+1); aoqi@0: print("reference", node.getReference()); aoqi@0: print("body", node.getLabel()); aoqi@0: indent(-1); aoqi@0: indent(); aoqi@0: out.println("]"); aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: public Void visitLiteral(LiteralTree node, Void p) { aoqi@0: header(node, compress(node.getBody().getBody())); aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: public Void visitParam(ParamTree node, Void p) { aoqi@0: header(node); aoqi@0: indent(+1); aoqi@0: print("name", node.getName()); aoqi@0: print("description", node.getDescription()); aoqi@0: indent(-1); aoqi@0: indent(); aoqi@0: out.println("]"); aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: public Void visitReference(ReferenceTree node, Void p) { aoqi@0: header(node, compress(node.getSignature())); aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: public Void visitReturn(ReturnTree node, Void p) { aoqi@0: header(node); aoqi@0: indent(+1); aoqi@0: print("description", node.getDescription()); aoqi@0: indent(-1); aoqi@0: indent(); aoqi@0: out.println("]"); aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: public Void visitSee(SeeTree node, Void p) { aoqi@0: header(node); aoqi@0: indent(+1); aoqi@0: print("reference", node.getReference()); aoqi@0: indent(-1); aoqi@0: indent(); aoqi@0: out.println("]"); aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: public Void visitSerial(SerialTree node, Void p) { aoqi@0: header(node); aoqi@0: indent(+1); aoqi@0: print("description", node.getDescription()); aoqi@0: indent(-1); aoqi@0: indent(); aoqi@0: out.println("]"); aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: public Void visitSerialData(SerialDataTree node, Void p) { aoqi@0: header(node); aoqi@0: indent(+1); aoqi@0: print("description", node.getDescription()); aoqi@0: indent(-1); aoqi@0: indent(); aoqi@0: out.println("]"); aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: public Void visitSerialField(SerialFieldTree node, Void p) { aoqi@0: header(node); aoqi@0: indent(+1); aoqi@0: print("name", node.getName()); aoqi@0: print("type", node.getType()); aoqi@0: print("description", node.getDescription()); aoqi@0: indent(-1); aoqi@0: indent(); aoqi@0: out.println("]"); aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: public Void visitSince(SinceTree node, Void p) { aoqi@0: header(node); aoqi@0: indent(+1); aoqi@0: print("body", node.getBody()); aoqi@0: indent(-1); aoqi@0: indent(); aoqi@0: out.println("]"); aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: public Void visitStartElement(StartElementTree node, Void p) { aoqi@0: header(node); aoqi@0: indent(+1); aoqi@0: indent(); aoqi@0: out.println("name:" + node.getName()); aoqi@0: print("attributes", node.getAttributes()); aoqi@0: indent(-1); aoqi@0: indent(); aoqi@0: out.println("]"); aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: public Void visitText(TextTree node, Void p) { aoqi@0: header(node, compress(node.getBody())); aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: public Void visitThrows(ThrowsTree node, Void p) { aoqi@0: header(node); aoqi@0: indent(+1); aoqi@0: print("exceptionName", node.getExceptionName()); aoqi@0: print("description", node.getDescription()); aoqi@0: indent(-1); aoqi@0: indent(); aoqi@0: out.println("]"); aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: public Void visitUnknownBlockTag(UnknownBlockTagTree node, Void p) { aoqi@0: header(node); aoqi@0: indent(+1); aoqi@0: indent(); aoqi@0: out.println("tag:" + node.getTagName()); aoqi@0: print("content", node.getContent()); aoqi@0: indent(-1); aoqi@0: indent(); aoqi@0: out.println("]"); aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: public Void visitUnknownInlineTag(UnknownInlineTagTree node, Void p) { aoqi@0: header(node); aoqi@0: indent(+1); aoqi@0: indent(); aoqi@0: out.println("tag:" + node.getTagName()); aoqi@0: print("content", node.getContent()); aoqi@0: indent(-1); aoqi@0: indent(); aoqi@0: out.println("]"); aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: public Void visitValue(ValueTree node, Void p) { aoqi@0: header(node); aoqi@0: indent(+1); aoqi@0: print("reference", node.getReference()); aoqi@0: indent(-1); aoqi@0: indent(); aoqi@0: out.println("]"); aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: public Void visitVersion(VersionTree node, Void p) { aoqi@0: header(node); aoqi@0: indent(+1); aoqi@0: print("body", node.getBody()); aoqi@0: indent(-1); aoqi@0: indent(); aoqi@0: out.println("]"); aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: public Void visitOther(DocTree node, Void p) { aoqi@0: throw new UnsupportedOperationException("Not supported yet."); aoqi@0: } aoqi@0: aoqi@0: void header(DocTree node) { aoqi@0: indent(); aoqi@0: out.println(simpleClassName(node) + "[" + node.getKind() + ", pos:" + ((DCTree) node).pos); aoqi@0: } aoqi@0: aoqi@0: void header(DocTree node, String rest) { aoqi@0: indent(); aoqi@0: out.println(simpleClassName(node) + "[" + node.getKind() + ", pos:" + ((DCTree) node).pos aoqi@0: + (rest.isEmpty() ? "" : ", " + rest) aoqi@0: + "]"); aoqi@0: } aoqi@0: aoqi@0: String simpleClassName(DocTree node) { aoqi@0: return node.getClass().getSimpleName().replaceAll("DC(.*)", "$1"); aoqi@0: } aoqi@0: aoqi@0: void print(String name, DocTree item) { aoqi@0: indent(); aoqi@0: if (item == null) aoqi@0: out.println(name + ": null"); aoqi@0: else { aoqi@0: out.println(name + ":"); aoqi@0: indent(+1); aoqi@0: item.accept(this, null); aoqi@0: indent(-1); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void print(String name, String s) { aoqi@0: indent(); aoqi@0: out.println(name + ": " + s); aoqi@0: } aoqi@0: aoqi@0: void print(String name, List list) { aoqi@0: indent(); aoqi@0: if (list == null) aoqi@0: out.println(name + ": null"); aoqi@0: else if (list.isEmpty()) aoqi@0: out.println(name + ": empty"); aoqi@0: else { aoqi@0: out.println(name + ": " + list.size()); aoqi@0: indent(+1); aoqi@0: for (DocTree tree: list) { aoqi@0: tree.accept(this, null); aoqi@0: } aoqi@0: indent(-1); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: int indent = 0; aoqi@0: aoqi@0: void indent() { aoqi@0: for (int i = 0; i < indent; i++) { aoqi@0: out.print(" "); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void indent(int n) { aoqi@0: indent += n; aoqi@0: } aoqi@0: aoqi@0: String compress(String s) { aoqi@0: s = s.replace("\n", "|").replace(" ", "_"); aoqi@0: return (s.length() < 32) aoqi@0: ? s aoqi@0: : s.substring(0, 16) + "..." + s.substring(16); aoqi@0: } aoqi@0: aoqi@0: String quote(String s) { aoqi@0: if (s.contains("\"")) aoqi@0: return "'" + s + "'"; aoqi@0: else if (s.contains("'") || s.contains(" ")) aoqi@0: return '"' + s + '"'; aoqi@0: else aoqi@0: return s; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Verify the reported tree positions by comparing the characters found aoqi@0: * at and after the reported position with the beginning of the pretty- aoqi@0: * printed text. aoqi@0: */ aoqi@0: static class PosChecker extends Checker { aoqi@0: PosChecker(DocCommentTester test, DocTrees t) { aoqi@0: test.super(t); aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: void check(TreePath path, Name name) throws Exception { aoqi@0: JavaFileObject fo = path.getCompilationUnit().getSourceFile(); aoqi@0: final CharSequence cs = fo.getCharContent(true); aoqi@0: aoqi@0: final DCDocComment dc = (DCDocComment) trees.getDocCommentTree(path); aoqi@0: DCTree t = (DCTree) trees.getDocCommentTree(path); aoqi@0: aoqi@0: DocTreeScanner scanner = new DocTreeScanner() { aoqi@0: @Override aoqi@0: public Void scan(DocTree node, Void ignore) { aoqi@0: if (node != null) { aoqi@0: try { aoqi@0: String expect = getExpectText(node); aoqi@0: long pos = ((DCTree) node).getSourcePosition(dc); aoqi@0: String found = getFoundText(cs, (int) pos, expect.length()); aoqi@0: if (!found.equals(expect)) { aoqi@0: System.err.println("expect: " + expect); aoqi@0: System.err.println("found: " + found); aoqi@0: error("mismatch"); aoqi@0: } aoqi@0: aoqi@0: } catch (StringIndexOutOfBoundsException e) { aoqi@0: error(node.getClass() + ": " + e.toString()); aoqi@0: e.printStackTrace(); aoqi@0: } aoqi@0: } aoqi@0: return super.scan(node, ignore); aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: scanner.scan(t, null); aoqi@0: } aoqi@0: aoqi@0: String getExpectText(DocTree t) { aoqi@0: StringWriter sw = new StringWriter(); aoqi@0: DocPretty p = new DocPretty(sw); aoqi@0: try { p.print(t); } catch (IOException never) { } aoqi@0: String s = sw.toString(); aoqi@0: if (s.length() <= 1) aoqi@0: return s; aoqi@0: int ws = s.replaceAll("\\s+", " ").indexOf(" "); aoqi@0: if (ws != -1) s = s.substring(0, ws); aoqi@0: return (s.length() < 5) ? s : s.substring(0, 5); aoqi@0: } aoqi@0: aoqi@0: String getFoundText(CharSequence cs, int pos, int len) { aoqi@0: return (pos == -1) ? "" : cs.subSequence(pos, Math.min(pos + len, cs.length())).toString(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Verify the pretty printed text against a normalized form of the aoqi@0: * original doc comment. aoqi@0: */ aoqi@0: static class PrettyChecker extends Checker { aoqi@0: aoqi@0: PrettyChecker(DocCommentTester test, DocTrees t) { aoqi@0: test.super(t); aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: void check(TreePath path, Name name) throws Exception { aoqi@0: String raw = trees.getDocComment(path); aoqi@0: String normRaw = normalize(raw); aoqi@0: aoqi@0: StringWriter out = new StringWriter(); aoqi@0: DocPretty dp = new DocPretty(out); aoqi@0: dp.print(trees.getDocCommentTree(path)); aoqi@0: String pretty = out.toString(); aoqi@0: aoqi@0: if (!pretty.equals(normRaw)) { aoqi@0: error("mismatch"); aoqi@0: System.err.println("*** expected:"); aoqi@0: System.err.println(normRaw.replace(" ", "_")); aoqi@0: System.err.println("*** found:"); aoqi@0: System.err.println(pretty.replace(" ", "_")); aoqi@0: // throw new Error(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Normalize white space in places where the tree does not preserve it. aoqi@0: */ aoqi@0: String normalize(String s) { aoqi@0: return s.trim() aoqi@0: .replaceFirst("\\.\\s++([^@])", ". $1") aoqi@0: .replaceFirst("\\.\\s*\\n *@", ".\n@") aoqi@0: .replaceFirst("\\s+<(/?p|pre|h[1-6])>", " <$1>") aoqi@0: .replaceAll("\\{@docRoot\\s+\\}", "{@docRoot}") aoqi@0: .replaceAll("\\{@inheritDoc\\s+\\}", "{@inheritDoc}") aoqi@0: .replaceAll("(\\{@value\\s+[^}]+)\\s+(\\})", "$1$2") aoqi@0: .replaceAll("\n[ \t]+@", "\n@"); aoqi@0: } aoqi@0: aoqi@0: } aoqi@0: aoqi@0: } aoqi@0: