I have recently tried the Eclipse AST (i.e., org.eclipse.jdt.core.dom.AST and etc) to analyze java source codes. I encountered a problem when trying to capture the content of BlockComment and LineComment. Concretely, I used getCommentList function to obtain the node information of these two types of comment nodes. Although their node types are recognized, the comment.toString failed to return any meaningful content except "/* */" and "//".
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?
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
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
.
int startPos = comment.getStartPosition();
int endPos = startPos + comment.getLength();
String text = content.substring(startPos, endPos);