Skip to content

Commit

Permalink
Set up javac javadoc conversion, cleaned up convertBodyDeclaration us…
Browse files Browse the repository at this point in the history
…e, fixed javadoc test, dump javadoc nodes.

	Change on 2016/12/16 by tball <tball@google.com>

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=142267033
  • Loading branch information
tomball committed Dec 16, 2016
1 parent 1ec3c84 commit 6c7912b
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 24 deletions.
Expand Up @@ -126,6 +126,11 @@ public boolean visit(IntersectionType node) {
return true;
}

@Override
public boolean visit(Javadoc node) {
return true;
}

@Override
public boolean visit(MemberValuePair node) {
printName(node.getName());
Expand Down Expand Up @@ -216,6 +221,21 @@ public boolean visit(SuperMethodReference node) {
return true;
}

@Override
public boolean visit(TagElement node) {
String tagName = node.getTagName();
sb.print(' ');
sb.print(tagName != null ? tagName : "null");
return true;
}

@Override
public boolean visit(TextElement node) {
sb.print(' ');
sb.print(node.getText());
return true;
}

@Override
public boolean visit(TypeDeclaration node) {
printName(node.getName());
Expand Down
Expand Up @@ -148,13 +148,14 @@
*/
public class TreeConverter {
private final JCTree.JCCompilationUnit unit;
private final JavacEnvironment env;
private CompilationUnit newUnit;

public static CompilationUnit convertCompilationUnit(
Options options, JavacEnvironment env, JCTree.JCCompilationUnit javacUnit) {
String sourceFilePath = getPath(javacUnit.getSourceFile());
try {
TreeConverter converter = new TreeConverter(javacUnit);
TreeConverter converter = new TreeConverter(javacUnit, env);
JavaFileObject sourceFile = javacUnit.getSourceFile();
String source = sourceFile.getCharContent(false).toString();
String mainTypeName = FileUtil.getMainTypeName(sourceFile);
Expand All @@ -174,8 +175,9 @@ public static CompilationUnit convertCompilationUnit(
}
}

private TreeConverter(JCTree.JCCompilationUnit javacUnit) {
private TreeConverter(JCTree.JCCompilationUnit javacUnit, JavacEnvironment javacEnv) {
unit = javacUnit;
env = javacEnv;
}

private TreeNode convert(Object obj) {
Expand Down Expand Up @@ -365,7 +367,7 @@ private TreeNode convertInner(JCTree javacNode) {

private TreeNode convertAbstractTypeDeclaration(
JCTree.JCClassDecl node, AbstractTypeDeclaration newNode) {
convertBodyDeclaration(node, newNode);
convertBodyDeclaration(node, node.getModifiers(), newNode, node.sym);
List<BodyDeclaration> bodyDeclarations = new ArrayList<>();
for (JCTree bodyDecl : node.getMembers()) {
// Skip synthetic methods. Synthetic default constructors are not marked
Expand Down Expand Up @@ -425,7 +427,7 @@ private TreeNode convertAnnotation(JCTree.JCAnnotation node) {

private TreeNode convertAnnotationTypeDeclaration(JCTree.JCClassDecl node) {
AnnotationTypeDeclaration newNode = new AnnotationTypeDeclaration();
convertBodyDeclaration(node, newNode);
convertBodyDeclaration(node, node.getModifiers(), newNode, node.sym);
for (JCTree bodyDecl : node.getMembers()) {
if (bodyDecl.getKind() == Kind.METHOD) {
JCTree.JCMethodDecl methodDecl = (JCTree.JCMethodDecl) bodyDecl;
Expand All @@ -442,7 +444,7 @@ private TreeNode convertAnnotationTypeDeclaration(JCTree.JCClassDecl node) {
newMember
.setModifiers((int) methodDecl.getModifiers().flags)
.setAnnotations(annotations)
.setJavadoc((Javadoc) getAssociatedJavaDoc(methodDecl));
.setJavadoc((Javadoc) getAssociatedJavaDoc(methodDecl, methodDecl.sym));
newNode.addBodyDeclaration(newMember);
} else {
newNode.addBodyDeclaration((BodyDeclaration) convert(bodyDecl));
Expand Down Expand Up @@ -536,15 +538,16 @@ private TreeNode convertBlock(JCTree.JCBlock node) {
return newNode;
}

private TreeNode convertBodyDeclaration(JCTree.JCClassDecl node, BodyDeclaration newNode) {
private TreeNode convertBodyDeclaration(JCTree node, JCTree.JCModifiers modifiers,
BodyDeclaration newNode, Element element) {
List<Annotation> annotations = new ArrayList<>();
for (AnnotationTree annotation : node.getModifiers().getAnnotations()) {
for (AnnotationTree annotation : modifiers.getAnnotations()) {
annotations.add((Annotation) convert(annotation));
}
return newNode
.setModifiers((int) node.getModifiers().flags)
.setModifiers((int) modifiers.flags)
.setAnnotations(annotations)
.setJavadoc((Javadoc) getAssociatedJavaDoc(node));
.setJavadoc((Javadoc) getAssociatedJavaDoc(node, element));
}

private TreeNode convertBooleanLiteral(JCTree.JCLiteral node) {
Expand Down Expand Up @@ -864,10 +867,7 @@ private TreeNode convertMemberReference(JCTree.JCMemberReference node) {

private TreeNode convertMethodDeclaration(JCTree.JCMethodDecl node) {
MethodDeclaration newNode = new MethodDeclaration();
List<Annotation> annotations = new ArrayList<>();
for (AnnotationTree annotation : node.getModifiers().getAnnotations()) {
annotations.add((Annotation) convert(annotation));
}
convertBodyDeclaration(node, node.getModifiers(), newNode, node.sym);
for (JCTree.JCVariableDecl param : node.getParameters()) {
newNode.addParameter((SingleVariableDeclaration) convert(param));
}
Expand All @@ -886,10 +886,7 @@ private TreeNode convertMethodDeclaration(JCTree.JCMethodDecl node) {
}
return newNode
.setExecutableElement(node.sym)
.setBody((Block) convert(node.getBody()))
.setModifiers((int) node.getModifiers().flags)
.setAnnotations(annotations)
.setJavadoc((Javadoc) getAssociatedJavaDoc(node));
.setBody((Block) convert(node.getBody()));
}

private TreeNode convertMethodInvocation(JCTree.JCMethodInvocation node) {
Expand Down Expand Up @@ -1120,7 +1117,10 @@ private TreeNode convertVariableDeclaration(JCTree.JCVariableDecl node) {
VarSymbol var = node.sym;
SourcePosition pos = getPosition(node);
if (var.getKind() == ElementKind.FIELD) {
return new FieldDeclaration(var, (Expression) convert(node.getInitializer()));
FieldDeclaration newNode = new FieldDeclaration(var,
(Expression) convert(node.getInitializer()));
convertBodyDeclaration(node, node.getModifiers(), newNode, var);
return newNode;
}
if (var.getKind() == ElementKind.LOCAL_VARIABLE) {
return new VariableDeclarationStatement(var, (Expression) convert(node.getInitializer()))
Expand All @@ -1130,6 +1130,7 @@ private TreeNode convertVariableDeclaration(JCTree.JCVariableDecl node) {
EnumConstantDeclaration newNode = new EnumConstantDeclaration()
.setName(convertSimpleName(var, node.type, getNamePosition(node)))
.setVariableElement(var);
convertBodyDeclaration(node, node.getModifiers(), newNode, var);
ClassInstanceCreation init = (ClassInstanceCreation) convert(node.getInitializer());
TreeUtil.copyList(init.getArguments(), newNode.getArguments());
if (init.getAnonymousClassDeclaration() != null) {
Expand Down Expand Up @@ -1191,12 +1192,12 @@ private TreeNode convertWhileLoop(JCTree.JCWhileLoop node) {
.setBody((Statement) convert(node.getStatement()));
}

private TreeNode getAssociatedJavaDoc(JCTree node) {
Comment comment = convertAssociatedComment(node);
private TreeNode getAssociatedJavaDoc(JCTree node, Element element) {
Comment comment = convertAssociatedComment(node, element);
return comment != null && comment.isDocComment() ? comment : null;
}

private Comment convertAssociatedComment(JCTree node) {
private Comment convertAssociatedComment(JCTree node, Element element) {
DocCommentTable docComments = unit.docComments;
if (docComments == null || !docComments.hasComment(node)) {
return null;
Expand All @@ -1208,7 +1209,7 @@ private Comment convertAssociatedComment(JCTree node) {
comment = new BlockComment();
break;
case JAVADOC:
comment = new Javadoc();
comment = convertJavadocComment(element);
break;
case LINE:
comment = new LineComment();
Expand All @@ -1219,9 +1220,15 @@ private Comment convertAssociatedComment(JCTree node) {
int startPos = javacComment.getSourcePos(0);
int endPos = startPos + javacComment.getText().length();
comment.setSourceRange(startPos, endPos);
comment.setLineNumber(unit.getLineMap().getLineNumber(startPos));
return comment;
}

private Javadoc convertJavadocComment(Element element) {
// TODO(tball): replace with a call to the future JavadocConverter.
return new Javadoc();
}

private static void addOcniComments(CompilationUnit unit) {
// Can't use a regex because it will greedily include everything between
// the first and last closing pattern, resulting in a single comment node.
Expand Down
Expand Up @@ -84,6 +84,7 @@ public void testTypeParamTagRemoval() throws IOException {
+ " * @param <T> the type to be returned.\n"
+ " */ T test() { return null; }}", "Test", "Test.h");
assertTranslation(translation, "@brief Class javadoc for Test.");
assertTranslation(translation, "@brief Method javadoc.");
assertNotInTranslation(translation, "@param");
assertNotInTranslation(translation, "<T>");
}
Expand Down Expand Up @@ -227,11 +228,12 @@ public void testStyleTagsSkipped() throws IOException {
public void testSeeTag() throws IOException {
String translation = translateSourceFile(
"/** Class javadoc for Test.\n"
+ " * @see {@link http://developers.facebook.com/docs/reference/javascript/FB.init/}\n"
+ " * @see <a href=\"http://developers.facebook.com/docs/reference/javascript/FB.init/\">"
+ "FB.init</a>\n"
+ " */ class Test {}", "Test", "Test.h");
assertTranslation(translation, "@brief Class javadoc for Test.");
assertTranslation(translation,
"- seealso: "
+ "<code>http://developers.facebook.com/docs/reference/javascript/FB.init/</code>");
+ "<a href=\"http://developers.facebook.com/docs/reference/javascript/FB.init/\">");
}
}

0 comments on commit 6c7912b

Please sign in to comment.