简体   繁体   English

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

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

I've used the java compiler tree api to generate the ast for java source files. 我已经使用java编译器树api为java源文件生成ast。 However, i'm unable to access th comments in the source files. 但是,我无法访问源文件中的注释。

So far, i've been unable to find a way to extract comments from source file .. is there a way using the compiler api or some other tool ? 到目前为止,我一直无法找到从源文件中提取注释的方法..有没有办法使用编译器api或其他工具?

Our SD Java Front End is a Java parser that builds ASTs (and optionally symbol tables). 我们的SD Java前端是一个Java解析器,它构建AST(以及可选的符号表)。 It captures comments directly on tree nodes. 它直接在树节点上捕获注释。

The Java Front End is a member of a family of compiler langauge front ends (C, C++, C#, COBOL, JavaScript, ...) all of which are supported by DMS Software Reengineering Toolkit . Java前端是一系列编译器语言前端(C,C ++,C#,COBOL,JavaScript,...)的成员,所有这些都由DMS Software Reengineering Toolkit支持。 DMS is designed to process languages for the purposes of transformation, and thus can capture comments, layout and formats to enable regeneration of code preserving the original layout as much as possible. DMS旨在处理语言以进行转换,因此可以捕获注释,布局和格式,以便能够重新生成尽可能保留原始布局的代码。

EDIT 3/29/2012: (in contrast to answer posted for doing this with ANTLR) 编辑3/29/2012 :(与使用ANTLR执行此操作的答案形成鲜明对比)

To get a comment on an AST node in DMS, one calls the DMS (lisp-like) function 要在DMS中的AST节点上发表评论,可以调用DMS(类似lisp)函数

  (AST:GetComments <node>)

which provide access to the array of comments associated with the AST node. 它提供对与AST节点相关的注释数组的访问。 One can inquire about the length of this array (may be null), or for each array element, ask for any of these properties: (AST:Get... FileIndex, Line, Column, EndLine, EndColumn, String (exact Unicode comment content). 可以查询此数组的长度(可能为null),或者对于每个数组元素,请求以下任何属性:(AST:Get ... FileIndex,Line,Column,EndLine,EndColumn,String(确切的Unicode注释)内容)。

The comments obtained through getCommentList method of CompilationUnit will not have the comment body. 通过CompilationUnit的 getCommentList方法获得的注释将不具有注释主体。 Also the comments will not be visited, during and AST Visit. 在AST访问期间也不会访问评论。 Inorder to visit the comments we have call accept method for each comment in the Comment List. 为了访问评论,我们在评论列表中为每个评论调用了accept方法。

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

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

The body of the comments can be obtained using some simple logic. 可以使用一些简单的逻辑来获得注释的主体。 In the below AST Visitor for comments, we need to specify the Complied class unit and the source code of the class during initialization. 在下面的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) {

    }
}

Edit: Moved splitting of source out of the visitor. 编辑:将源分离移出访问者。

Just for the record. 仅供记录。 Now with Java 8 you have a whole interface to play with comments and documentation details here . 现在使用Java 8,您可以在此处使用完整的界面来阅读注释和文档详细信息。

You might to use a different tool, like ANTLR's Java grammar. 您可能会使用其他工具,例如ANTLR的Java语法。 javac has no use for comments, and is likely to discard them completely. javac没有用于评论,并且很可能完全丢弃它们。 The parsers upon which tools like IDEs are built are more likely to retain comments in their AST. 构建IDE等工具的解析器更有可能在其AST中保留注释。

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

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

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