简体   繁体   English

(Eclipse) AST 可以捕获 LineCommet 和 BlockComment 内容吗?

[英]Can (Eclipse) AST capture the LineCommet and BlockComment content?

I have recently tried the Eclipse AST (ie, org.eclipse.jdt.core.dom.AST and etc) to analyze java source codes.我最近尝试了 Eclipse AST(即 org.eclipse.jdt.core.dom.AST 等)来分析 java 源代码。 I encountered a problem when trying to capture the content of BlockComment and LineComment.我在尝试捕获 BlockComment 和 LineComment 的内容时遇到了问题。 Concretely, I used getCommentList function to obtain the node information of these two types of comment nodes.具体来说,我使用getCommentList函数来获取这两种评论节点的节点信息。 Although their node types are recognized, the comment.toString failed to return any meaningful content except "/* */" and "//".尽管它们的节点类型被识别,comment.toString 未能返回任何有意义的内容,除了“/* */”和“//”。

The codes I used are provided as below:我使用的代码提供如下:

import java.io.File;
import java.io.IOException;
import java.util.List;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.BlockComment;
import org.eclipse.jdt.core.dom.LineComment;
import org.eclipse.jdt.core.dom.Comment;
import org.apache.commons.io.FileUtils;


public class run_visitor {
    public run_visitor(String path) throws IOException {
        // file content
        File fd = new File(path);
        String content = FileUtils.readFileToString(fd);
        
        // parser 
        ASTParser parser = ASTParser.newParser(AST.JLS3);
        parser.setBindingsRecovery(true);
        parser.setKind(ASTParser.K_COMPILATION_UNIT);
        parser.setSource(content.toCharArray());
        
        // cu creation
        CompilationUnit cu = (CompilationUnit) parser.createAST(null);
        
        kaixin_visitor visitor = new kaixin_visitor();
        cu.accept(visitor);
        
        List<Comment> commentLists = cu.getCommentList();
        for (Comment comment : commentLists) {
            if (comment instanceof BlockComment) {
                System.out.println("This is Block comment:" + comment.toString());
            } else if (comment instanceof LineComment) {
                System.out.println("This is Line comment:" + comment.toString());
            }
        }
    }
    
    public static void main(String[] args) throws IOException {
        String path = "demo.java";
        run_visitor vst = new run_visitor(path);
    }
}

So can anyone tell me if it is the internal feature of AST that cannot capture the string content of these two types of nodes, or, maybe I made a mistake in using any functions or procedures?那么谁能告诉我是AST的内部特性无法捕获这两种类型节点的字符串内容,还是我在使用任何函数或过程时犯了错误?

Some referred links are provided as follows:提供了一些参考链接如下:

1.http://help.eclipse.org/2021-06/index.jsp 2.https://git.eclipse.org/c/jdt/eclipse.jdt.core.git/tree/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom 1.http://help.eclipse.org/2021-06/index.jsp 2.https://git.eclipse.org/c/jdt/eclipse.jdt.core.git/tree/org.eclipse.jdt .core/dom/org/eclipse/jdt/core/dom

The Comment class just remembers the start and length of the comment, so if you want the actual comment text you get it from the original source using the getStartPosition and getLength methods of Comment . Comment类只记住Comment的开始和长度,所以如果你想要实际的评论文本,你可以使用CommentgetStartPositiongetLength方法从原始来源获取它。

int startPos = comment.getStartPosition();
int endPos = startPos + comment.getLength();
String text = content.substring(startPos, endPos);

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

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