(Eclipse) AST 可以捕获 LineCommet 和 BlockComment 内容吗?
Can (Eclipse) AST capture the LineCommet and BlockComment content?
我最近尝试使用 Eclipse AST(即 org.eclipse.jdt.core.dom.AST 等)来分析 java 源代码。我在尝试捕获 BlockComment 和 LineComment 的内容时遇到了问题。具体的,我使用getCommentList函数获取了这两类评论节点的节点信息。虽然它们的节点类型被识别,但是 comment.toString 未能 return 除“/* */”和“//”之外的任何有意义的内容。
我使用的代码如下:
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
class 只记住评论的开始和长度,所以如果你想要实际的评论文本,你可以使用 getStartPosition
和 getLength
Comment
.
方法
int startPos = comment.getStartPosition();
int endPos = startPos + comment.getLength();
String text = content.substring(startPos, endPos);
我最近尝试使用 Eclipse AST(即 org.eclipse.jdt.core.dom.AST 等)来分析 java 源代码。我在尝试捕获 BlockComment 和 LineComment 的内容时遇到了问题。具体的,我使用getCommentList函数获取了这两类评论节点的节点信息。虽然它们的节点类型被识别,但是 comment.toString 未能 return 除“/* */”和“//”之外的任何有意义的内容。
我使用的代码如下:
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
class 只记住评论的开始和长度,所以如果你想要实际的评论文本,你可以使用 getStartPosition
和 getLength
Comment
.
int startPos = comment.getStartPosition();
int endPos = startPos + comment.getLength();
String text = content.substring(startPos, endPos);