We have been trying to use the Eclipse JDT core parser in our project to parse Java source files, but one of the problems we have is that we cannot get the list of comments from a file. We have tried calling the getComments
method on the CompilationUnit
but it shows us a list of empty comments.
This is from a test project whose Test1.java
source is here:
public class Test1 {
public static void main(String[] args) throws IOException {
File file = new File("src/main/java/DummyFile.java");
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
StringBuilder source = new StringBuilder();
String line = "";
while ((line = bufferedReader.readLine()) != null) {
source.append(line + System.getProperty("line.separator"));
}
bufferedReader.close();
ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setSource(source.toString().toCharArray());
parser.setResolveBindings(true);
Hashtable<String, String> options = JavaCore.getOptions();
options.put(JavaCore.COMPILER_DOC_COMMENT_SUPPORT, JavaCore.ENABLED);
parser.setCompilerOptions(options);
CompilationUnit cu = (CompilationUnit) parser.createAST(null);
List comments = cu.getCommentList();
System.out.println(cu); // breakpoint is here.
for (Object comment : comments) {
System.out.println(comment.toString());
}
}
The DummyFile.java is as follows:
public class DummyFile {
// 1
private String str;
public static int number;
public DummyFile() { // 2
// 3
str = "something";
number = 2; // 4
// 5
}
// 6
}
This is the output:
public class DummyFile {
private String str;
public static int number;
public DummyFile(){
str="something";
number=2;
}
}
//
//
//
//
//
//
This is what see in the debugger when we look inside the compilationUnit and look at the list of comments:
[//
, //
, //
, //
, //
, //
]
So we were wondering if this is normal behaviour and if so, what do to get the content of the comments or what are we doing wrong?
If this is not normal behaviour then maybe someone has any suggestions as to what is going wrong and what to look for.
The comments obtained through the getCommentList()
method of CompilationUnit will not have the comment body. Also the comments will not be visited during an AST Visit. In order to visit the comments, we have to call 'accept
' for each comment in the Comment List.
See the answer to the below question on how to extract comments from a CompilationUnit:
How to access comments from the java compiler tree api generated ast?