How to read inline comments from java file using javac tools parser?

I am using javac from tools.jar (i.e. JavaCompiler ) to parse java files. I am parsing sources using the TreePathScanner implementation. So far, everything seems perfect, as I can parse the import, package name, class name, method name, instructions ...

But I have a problem with inline comments - I can't just get them to appear in the created AST tree or visit them. However, I can read javadoc comments for classes, methods, etc., but there are no built-in comments.

How to read inline comments in the best way? I am looking at the source code of netbeans (as it also uses javac for parsing), but I cannot easily find anything about it.

My desperate decision would be to use the position of the source file, and then manually analyze everything between the two statements for comment. Or similar, but between two nodes of the tree.

Does anyone know a better solution? Thanks!

+6
source share
2 answers

You can not. The compiler discards them. Compilers always do this. The Java compiler does not discard Javadoc comments just because Javadoc uses the compiler to find them, and the Javadoc guys get together with the compiler guys.

+4
source

The key difference between a "compiler-parser" and a "reengineered parser" is related to what information is captured regarding layout, comments, and literal formats. Like other observers, most compilers discard all this information, since it is not related to compiling to low-level code.

Similarly, classic parser generators (such as JavaCC, ANTLR, etc.) offer very little support in writing / regenerating this information.

Reengineering parsers, by contrast, are used to analyze code and comments, sometimes even to review code without loss (or to properly review comments). To analyze code with comments, you cannot reject comments: -} To modify the code, if you regenerate the modified code based on the original, it is nice if the modified code saves the code layout, comments and format literals (for example, the hexadecimal literal case as a decimal value is legal and equivalent, but makes the original authors rather unhappy). To do this, reengineering analyzers need special lexers to capture all this data and analyze machines that don't throw it away.

Our DMS software reengineering toolkit includes, well, reengineering parser as a universal equipment; DMS parsers exist for a wide range of languages ​​(including OP's interest in Java). DMS captures all information about comments / layout / formatting. Analysis tools have access to all of this.

TXL and Stratego also support this feature.

+1
source

All Articles