mirror of
https://github.com/openjdk/jdk.git
synced 2025-09-20 02:54:35 +02:00
8007307: DPrinter: support the DocTree API
Reviewed-by: mcimadamore
This commit is contained in:
parent
bd97e7d5ec
commit
47c543e53b
2 changed files with 271 additions and 29 deletions
|
@ -126,6 +126,9 @@ public class JavacTaskImpl extends BasicJavacTask {
|
||||||
initContext();
|
initContext();
|
||||||
notYetEntered = new HashMap<>();
|
notYetEntered = new HashMap<>();
|
||||||
compilerMain.setAPIMode(true);
|
compilerMain.setAPIMode(true);
|
||||||
|
compiler = JavaCompiler.instance(context);
|
||||||
|
compiler.keepComments = true;
|
||||||
|
compiler.genEndPos = true;
|
||||||
result = compilerMain.compile(args, classNames, context, fileObjects, processors);
|
result = compilerMain.compile(args, classNames, context, fileObjects, processors);
|
||||||
cleanup();
|
cleanup();
|
||||||
return result;
|
return result;
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
|
@ -22,7 +22,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* @test
|
/* @test
|
||||||
* @bug 8043484
|
* @bug 8043484 8007307
|
||||||
* @summary Make sure DPrinter.java compiles
|
* @summary Make sure DPrinter.java compiles
|
||||||
* @compile DPrinter.java
|
* @compile DPrinter.java
|
||||||
*/
|
*/
|
||||||
|
@ -50,6 +50,7 @@ import javax.tools.StandardJavaFileManager;
|
||||||
import javax.tools.StandardLocation;
|
import javax.tools.StandardLocation;
|
||||||
import javax.tools.ToolProvider;
|
import javax.tools.ToolProvider;
|
||||||
|
|
||||||
|
import com.sun.source.doctree.*;
|
||||||
import com.sun.source.util.JavacTask;
|
import com.sun.source.util.JavacTask;
|
||||||
import com.sun.source.util.TaskEvent;
|
import com.sun.source.util.TaskEvent;
|
||||||
import com.sun.source.util.TaskListener;
|
import com.sun.source.util.TaskListener;
|
||||||
|
@ -74,6 +75,7 @@ import com.sun.tools.javac.tree.TreeInfo;
|
||||||
import com.sun.tools.javac.tree.TreeScanner;
|
import com.sun.tools.javac.tree.TreeScanner;
|
||||||
import com.sun.tools.javac.util.Assert;
|
import com.sun.tools.javac.util.Assert;
|
||||||
import com.sun.tools.javac.util.Context;
|
import com.sun.tools.javac.util.Context;
|
||||||
|
import com.sun.tools.javac.util.Convert;
|
||||||
import com.sun.tools.javac.util.Log;
|
import com.sun.tools.javac.util.Log;
|
||||||
|
|
||||||
|
|
||||||
|
@ -248,6 +250,20 @@ public class DPrinter {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void printDocTree(String label, DocTree tree) {
|
||||||
|
if (tree == null) {
|
||||||
|
printNull(label);
|
||||||
|
} else {
|
||||||
|
indent();
|
||||||
|
out.print(label);
|
||||||
|
out.println(": " + tree.getClass().getSimpleName() + "," + tree.getKind());
|
||||||
|
|
||||||
|
indent(+1);
|
||||||
|
tree.accept(docTreeVisitor, null);
|
||||||
|
indent(-1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void printFileObject(String label, FileObject fo) {
|
public void printFileObject(String label, FileObject fo) {
|
||||||
if (fo == null) {
|
if (fo == null) {
|
||||||
printNull(label);
|
printNull(label);
|
||||||
|
@ -265,6 +281,17 @@ public class DPrinter {
|
||||||
printString(label, String.valueOf(i));
|
printString(label, String.valueOf(i));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void printLimitedEscapedString(String label, String text) {
|
||||||
|
String s = Convert.quote(text);
|
||||||
|
if (s.length() > maxSrcLength) {
|
||||||
|
String trim = "[...]";
|
||||||
|
int head = (maxSrcLength - trim.length()) * 2 / 3;
|
||||||
|
int tail = maxSrcLength - trim.length() - head;
|
||||||
|
s = s.substring(0, head) + trim + s.substring(s.length() - tail);
|
||||||
|
}
|
||||||
|
printString(label, s);
|
||||||
|
}
|
||||||
|
|
||||||
public void printList(String label, List<?> list) {
|
public void printList(String label, List<?> list) {
|
||||||
if (list == null) {
|
if (list == null) {
|
||||||
printNull(label);
|
printNull(label);
|
||||||
|
@ -304,6 +331,8 @@ public class DPrinter {
|
||||||
printType(label, (Type) item, details);
|
printType(label, (Type) item, details);
|
||||||
} else if (item instanceof JCTree) {
|
} else if (item instanceof JCTree) {
|
||||||
printTree(label, (JCTree) item);
|
printTree(label, (JCTree) item);
|
||||||
|
} else if (item instanceof DocTree) {
|
||||||
|
printDocTree(label, (DocTree) item);
|
||||||
} else if (item instanceof List) {
|
} else if (item instanceof List) {
|
||||||
printList(label, (List) item);
|
printList(label, (List) item);
|
||||||
} else if (item instanceof Name) {
|
} else if (item instanceof Name) {
|
||||||
|
@ -877,6 +906,191 @@ public class DPrinter {
|
||||||
|
|
||||||
// </editor-fold>
|
// </editor-fold>
|
||||||
|
|
||||||
|
// <editor-fold defaultstate="collapsed" desc="DocTree visitor">
|
||||||
|
|
||||||
|
protected DocTreeVisitor<Void,Void> docTreeVisitor = new DefaultDocTreeVisitor();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default visitor class for DocTree objects.
|
||||||
|
* Note: each visitXYZ method ends by calling the corresponding
|
||||||
|
* visit method for its superclass.
|
||||||
|
*/
|
||||||
|
class DefaultDocTreeVisitor implements DocTreeVisitor<Void,Void> {
|
||||||
|
|
||||||
|
public Void visitAttribute(AttributeTree node, Void p) {
|
||||||
|
printName("name", node.getName());
|
||||||
|
printString("vkind", node.getValueKind().name());
|
||||||
|
printList("value", node.getValue());
|
||||||
|
return visitTree(node, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Void visitAuthor(AuthorTree node, Void p) {
|
||||||
|
printList("name", node.getName());
|
||||||
|
return visitBlockTag(node, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Void visitComment(CommentTree node, Void p) {
|
||||||
|
printLimitedEscapedString("body", node.getBody());
|
||||||
|
return visitTree(node, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Void visitDeprecated(DeprecatedTree node, Void p) {
|
||||||
|
printList("body", node.getBody());
|
||||||
|
return visitBlockTag(node, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Void visitDocComment(DocCommentTree node, Void p) {
|
||||||
|
printList("firstSentence", node.getFirstSentence());
|
||||||
|
printList("body", node.getBody());
|
||||||
|
printList("tags", node.getBlockTags());
|
||||||
|
return visitTree(node, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Void visitDocRoot(DocRootTree node, Void p) {
|
||||||
|
return visitInlineTag(node, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Void visitEndElement(EndElementTree node, Void p) {
|
||||||
|
printName("name", node.getName());
|
||||||
|
return visitTree(node, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Void visitEntity(EntityTree node, Void p) {
|
||||||
|
printName("name", node.getName());
|
||||||
|
return visitTree(node, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Void visitErroneous(ErroneousTree node, Void p) {
|
||||||
|
printLimitedEscapedString("body", node.getBody());
|
||||||
|
printString("diag", node.getDiagnostic().getMessage(Locale.getDefault()));
|
||||||
|
return visitTree(node, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Void visitIdentifier(IdentifierTree node, Void p) {
|
||||||
|
printName("name", node.getName());
|
||||||
|
return visitTree(node, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Void visitInheritDoc(InheritDocTree node, Void p) {
|
||||||
|
return visitInlineTag(node, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Void visitLink(LinkTree node, Void p) {
|
||||||
|
printString("kind", node.getKind().name());
|
||||||
|
printDocTree("ref", node.getReference());
|
||||||
|
printList("list", node.getLabel());
|
||||||
|
return visitInlineTag(node, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Void visitLiteral(LiteralTree node, Void p) {
|
||||||
|
printString("kind", node.getKind().name());
|
||||||
|
printDocTree("body", node.getBody());
|
||||||
|
return visitInlineTag(node, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Void visitParam(ParamTree node, Void p) {
|
||||||
|
printString("isTypeParameter", String.valueOf(node.isTypeParameter()));
|
||||||
|
printString("kind", node.getKind().name());
|
||||||
|
printList("desc", node.getDescription());
|
||||||
|
return visitBlockTag(node, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Void visitReference(ReferenceTree node, Void p) {
|
||||||
|
printString("signature", node.getSignature());
|
||||||
|
return visitTree(node, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Void visitReturn(ReturnTree node, Void p) {
|
||||||
|
printList("desc", node.getDescription());
|
||||||
|
return visitBlockTag(node, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Void visitSee(SeeTree node, Void p) {
|
||||||
|
printList("ref", node.getReference());
|
||||||
|
return visitBlockTag(node, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Void visitSerial(SerialTree node, Void p) {
|
||||||
|
printList("desc", node.getDescription());
|
||||||
|
return visitBlockTag(node, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Void visitSerialData(SerialDataTree node, Void p) {
|
||||||
|
printList("desc", node.getDescription());
|
||||||
|
return visitBlockTag(node, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Void visitSerialField(SerialFieldTree node, Void p) {
|
||||||
|
printDocTree("name", node.getName());
|
||||||
|
printDocTree("type", node.getType());
|
||||||
|
printList("desc", node.getDescription());
|
||||||
|
return visitBlockTag(node, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Void visitSince(SinceTree node, Void p) {
|
||||||
|
printList("body", node.getBody());
|
||||||
|
return visitBlockTag(node, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Void visitStartElement(StartElementTree node, Void p) {
|
||||||
|
printName("name", node.getName());
|
||||||
|
printList("attrs", node.getAttributes());
|
||||||
|
printString("selfClosing", String.valueOf(node.isSelfClosing()));
|
||||||
|
return visitBlockTag(node, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Void visitText(TextTree node, Void p) {
|
||||||
|
printLimitedEscapedString("body", node.getBody());
|
||||||
|
return visitTree(node, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Void visitThrows(ThrowsTree node, Void p) {
|
||||||
|
printDocTree("name", node.getExceptionName());
|
||||||
|
printList("desc", node.getDescription());
|
||||||
|
return visitBlockTag(node, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Void visitUnknownBlockTag(UnknownBlockTagTree node, Void p) {
|
||||||
|
printString("name", node.getTagName());
|
||||||
|
printList("content", node.getContent());
|
||||||
|
return visitBlockTag(node, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Void visitUnknownInlineTag(UnknownInlineTagTree node, Void p) {
|
||||||
|
printString("name", node.getTagName());
|
||||||
|
printList("content", node.getContent());
|
||||||
|
return visitInlineTag(node, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Void visitValue(ValueTree node, Void p) {
|
||||||
|
printDocTree("value", node.getReference());
|
||||||
|
return visitInlineTag(node, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Void visitVersion(VersionTree node, Void p) {
|
||||||
|
printList("body", node.getBody());
|
||||||
|
return visitBlockTag(node, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Void visitOther(DocTree node, Void p) {
|
||||||
|
return visitTree(node, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Void visitBlockTag(DocTree node, Void p) {
|
||||||
|
return visitTree(node, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Void visitInlineTag(DocTree node, Void p) {
|
||||||
|
return visitTree(node, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Void visitTree(DocTree node, Void p) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// </editor-fold>
|
||||||
|
|
||||||
// <editor-fold defaultstate="collapsed" desc="Symbol visitor">
|
// <editor-fold defaultstate="collapsed" desc="Symbol visitor">
|
||||||
|
|
||||||
protected Symbol.Visitor<Void,Void> symVisitor = new SymbolVisitor();
|
protected Symbol.Visitor<Void,Void> symVisitor = new SymbolVisitor();
|
||||||
|
@ -1228,18 +1442,19 @@ public class DPrinter {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void handle(TaskEvent e) {
|
private void handle(TaskEvent e) {
|
||||||
|
JCCompilationUnit unit = (JCCompilationUnit) e.getCompilationUnit();
|
||||||
switch (e.getKind()) {
|
switch (e.getKind()) {
|
||||||
case PARSE:
|
case PARSE:
|
||||||
case ENTER:
|
case ENTER:
|
||||||
h.handle(e.getSourceFile().getName(),
|
h.handle(e.getSourceFile().getName(),
|
||||||
(JCTree) e.getCompilationUnit(),
|
unit, unit,
|
||||||
dprinter);
|
dprinter);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
TypeElement elem = e.getTypeElement();
|
TypeElement elem = e.getTypeElement();
|
||||||
h.handle(elem.toString(),
|
h.handle(elem.toString(),
|
||||||
(JCTree) trees.getTree(elem),
|
unit, (JCTree) trees.getTree(elem),
|
||||||
dprinter);
|
dprinter);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -1258,7 +1473,9 @@ public class DPrinter {
|
||||||
Handler(String name) {
|
Handler(String name) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
abstract void handle(String label, JCTree tree, DPrinter dprinter);
|
abstract void handle(String label,
|
||||||
|
JCCompilationUnit unit, JCTree tree,
|
||||||
|
DPrinter dprinter);
|
||||||
}
|
}
|
||||||
|
|
||||||
Map<String,Handler> getHandlers() {
|
Map<String,Handler> getHandlers() {
|
||||||
|
@ -1272,36 +1489,36 @@ public class DPrinter {
|
||||||
protected final Handler[] defaultHandlers = {
|
protected final Handler[] defaultHandlers = {
|
||||||
new Handler("trees") {
|
new Handler("trees") {
|
||||||
@Override
|
@Override
|
||||||
void handle(String name, JCTree tree, DPrinter dprinter) {
|
void handle(String name, JCCompilationUnit unit, JCTree tree, DPrinter dprinter) {
|
||||||
dprinter.printTree(name, tree);
|
dprinter.printTree(name, tree);
|
||||||
dprinter.out.println();
|
dprinter.out.println();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
new Handler("doctrees") {
|
||||||
|
@Override
|
||||||
|
void handle(final String name, final JCCompilationUnit unit, JCTree tree, final DPrinter dprinter) {
|
||||||
|
TreeScanner ds = new DeclScanner() {
|
||||||
|
public void visitDecl(JCTree tree, Symbol sym) {
|
||||||
|
DocTree dt = unit.docComments.getCommentTree(tree);
|
||||||
|
if (dt != null) {
|
||||||
|
String label = (sym == null) ? Pretty.toSimpleString(tree) : sym.name.toString();
|
||||||
|
dprinter.printDocTree(label, dt);
|
||||||
|
dprinter.out.println();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
ds.scan(tree);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
new Handler("symbols") {
|
new Handler("symbols") {
|
||||||
@Override
|
@Override
|
||||||
void handle(String name, JCTree tree, final DPrinter dprinter) {
|
void handle(String name, JCCompilationUnit unit, JCTree tree, final DPrinter dprinter) {
|
||||||
TreeScanner ds = new TreeScanner() {
|
TreeScanner ds = new DeclScanner() {
|
||||||
@Override
|
public void visitDecl(JCTree tree, Symbol sym) {
|
||||||
public void visitClassDef(JCClassDecl tree) {
|
String label = (sym == null) ? Pretty.toSimpleString(tree) : sym.name.toString();
|
||||||
visitDecl(tree, tree.sym);
|
dprinter.printSymbol(label, sym);
|
||||||
super.visitClassDef(tree);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void visitMethodDef(JCMethodDecl tree) {
|
|
||||||
visitDecl(tree, tree.sym);
|
|
||||||
super.visitMethodDef(tree);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void visitVarDef(JCVariableDecl tree) {
|
|
||||||
visitDecl(tree, tree.sym);
|
|
||||||
super.visitVarDef(tree);
|
|
||||||
}
|
|
||||||
|
|
||||||
void visitDecl(JCTree tree, Symbol sym) {
|
|
||||||
dprinter.printSymbol(sym.name.toString(), sym);
|
|
||||||
dprinter.out.println();
|
dprinter.out.println();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -1311,7 +1528,7 @@ public class DPrinter {
|
||||||
|
|
||||||
new Handler("types") {
|
new Handler("types") {
|
||||||
@Override
|
@Override
|
||||||
void handle(String name, JCTree tree, final DPrinter dprinter) {
|
void handle(String name, JCCompilationUnit unit, JCTree tree, final DPrinter dprinter) {
|
||||||
TreeScanner ts = new TreeScanner() {
|
TreeScanner ts = new TreeScanner() {
|
||||||
@Override
|
@Override
|
||||||
public void scan(JCTree tree) {
|
public void scan(JCTree tree) {
|
||||||
|
@ -1332,6 +1549,28 @@ public class DPrinter {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected static abstract class DeclScanner extends TreeScanner {
|
||||||
|
@Override
|
||||||
|
public void visitClassDef(JCClassDecl tree) {
|
||||||
|
visitDecl(tree, tree.sym);
|
||||||
|
super.visitClassDef(tree);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visitMethodDef(JCMethodDecl tree) {
|
||||||
|
visitDecl(tree, tree.sym);
|
||||||
|
super.visitMethodDef(tree);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visitVarDef(JCVariableDecl tree) {
|
||||||
|
visitDecl(tree, tree.sym);
|
||||||
|
super.visitVarDef(tree);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract void visitDecl(JCTree tree, Symbol sym);
|
||||||
|
}
|
||||||
|
|
||||||
// </editor-fold>
|
// </editor-fold>
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue