![](/img/trans.png)
[英]How can I retrieve inheritance information about a Class in AST eclipse?
[英]Can (Eclipse) AST capture the LineCommet and BlockComment content?
我最近尝试了 Eclipse AST(即 org.eclipse.jdt.core.dom.AST 等)来分析 java 源代码。 我在尝试捕获 BlockComment 和 LineComment 的内容时遇到了问题。 具体来说,我使用getCommentList函数来获取这两种评论节点的节点信息。 尽管它们的节点类型被识别,comment.toString 未能返回任何有意义的内容,除了“/* */”和“//”。
我使用的代码提供如下:
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);
}
}
那么谁能告诉我是AST的内部特性无法捕获这两种类型节点的字符串内容,还是我在使用任何函数或过程时犯了错误?
提供了一些参考链接如下:
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
Comment
类只记住Comment
的开始和长度,所以如果你想要实际的评论文本,你可以使用Comment
的getStartPosition
和getLength
方法从原始来源获取它。
int startPos = comment.getStartPosition();
int endPos = startPos + comment.getLength();
String text = content.substring(startPos, endPos);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.