views:

112

answers:

2

Hi,

I've used the java compiler tree api to generate the ast for java source files. However, i'm unable to access th comments in the source files.

So far, i've been unable to find a way to extract comments from source file .. is there a way using the compiler api or some other tool ?

A: 

You might to use a different tool, like ANTLR's Java grammar. javac has no use for comments, and is likely to discard them completely. The parsers upon which tools like IDEs are built are more likely to retain comments in their AST.

erickson
As far as i know Netbeans uses java tree api for syntax highlighting, code completion, checking etc.
Code freak
Check out its formatter. That's going to use a tree parser to correctly handle the code, but it also formats comments. Also, ANTLR has special support for comment nodes, so I'm pretty sure its Java grammar will handle this case.
erickson
+1  A: 

The SD Java Front End is a Java parser that builds ASTs (and optionally symbol tables). It captures comments on tree nodes.

The Java Front End is a member of a family of compiler langauge front ends (C, C++, C#, COBOL, JavaScript, ...) all of which are supported by DMS Software Reengineering Toolkit. DMS is designed to process languages for the purposes of transformation, and thus can capture comments, layout and formats to enable regeneration of code preserving the original layout as much as possible.

Ira Baxter
+1 for DMS, didn't know abt it.
Code freak
@Code freak - If this is helpful, you should actually give Ira Baxter an upvote, not just comment "+1".
erickson