繁体   English   中英

如何从java编译器树api生成注释生成的ast?

[英]How to access comments from the java compiler tree api generated ast?

我已经使用java编译器树api为java源文件生成ast。 但是,我无法访问源文件中的注释。

到目前为止,我一直无法找到从源文件中提取注释的方法..有没有办法使用编译器api或其他工具?

我们的SD Java前端是一个Java解析器,它构建AST(以及可选的符号表)。 它直接在树节点上捕获注释。

Java前端是一系列编译器语言前端(C,C ++,C#,COBOL,JavaScript,...)的成员,所有这些都由DMS Software Reengineering Toolkit支持。 DMS旨在处理语言以进行转换,因此可以捕获注释,布局和格式,以便能够重新生成尽可能保留原始布局的代码。

编辑3/29/2012 :(与使用ANTLR执行此操作的答案形成鲜明对比)

要在DMS中的AST节点上发表评论,可以调用DMS(类似lisp)函数

  (AST:GetComments <node>)

它提供对与AST节点相关的注释数组的访问。 可以查询此数组的长度(可能为null),或者对于每个数组元素,请求以下任何属性:(AST:Get ... FileIndex,Line,Column,EndLine,EndColumn,String(确切的Unicode注释)内容)。

通过CompilationUnit的 getCommentList方法获得的注释将不具有注释主体。 在AST访问期间也不会访问评论。 为了访问评论,我们在评论列表中为每个评论调用了accept方法。

for (Comment comment : (List<Comment>) compilationUnit.getCommentList()) {

    comment.accept(new CommentVisitor(compilationUnit, classSource.split("\n")));
}

可以使用一些简单的逻辑来获得注释的主体。 在下面的AST Visitor中,我们需要在初始化期间指定Complied类单元和类的源代码。

import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.BlockComment;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.LineComment;

public class CommentVisitor extends ASTVisitor {

    CompilationUnit compilationUnit;

    private String[] source;

    public CommentVisitor(CompilationUnit compilationUnit, String[] source) {

        super();
        this.compilationUnit = compilationUnit;
        this.source = source;
    }

    public boolean visit(LineComment node) {

        int startLineNumber = compilationUnit.getLineNumber(node.getStartPosition()) - 1;
        String lineComment = source[startLineNumber].trim();

        System.out.println(lineComment);

        return true;
    }

    public boolean visit(BlockComment node) {

        int startLineNumber = compilationUnit.getLineNumber(node.getStartPosition()) - 1;
        int endLineNumber = compilationUnit.getLineNumber(node.getStartPosition() + node.getLength()) - 1;

        StringBuffer blockComment = new StringBuffer();

        for (int lineCount = startLineNumber ; lineCount<= endLineNumber; lineCount++) {

            String blockCommentLine = source[lineCount].trim();
            blockComment.append(blockCommentLine);
            if (lineCount != endLineNumber) {
                blockComment.append("\n");
            }
        }

        System.out.println(blockComment.toString());

        return true;
    }

    public void preVisit(ASTNode node) {

    }
}

编辑:将源分离移出访问者。

仅供记录。 现在使用Java 8,您可以在此处使用完整的界面来阅读注释和文档详细信息。

您可能会使用其他工具,例如ANTLR的Java语法。 javac没有用于评论,并且很可能完全丢弃它们。 构建IDE等工具的解析器更有可能在其AST中保留注释。

通过使用getsourceposition()和一些字符串操作(不需要正则表达式)来解决问题

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM