Community
Participate
Working Groups
It would be quite helpful to have an ASTNode that represents non-Javadoc comments (i.e. "/* ... */" and "// ...." In that way one would be able to parse a .java file into a AST tree, modify the tree and reconstruct a .java file from that modified AST tree. If one tries to do that now, one looses the original non-Javadoc comments.
We have the same need. Can this be fixed in the 2.1 timeframe?
Unfortunately, there is no consensus yet on what comments should be surfaced as (especially what node would they get associated with). Currently, you have to resort to using our scanning API to rescan portions of the source. Will reconsider post 2.1
Will consider providing a list of detected comments instead. Then provide a heuristic to perform associations with nodes. Javadoc comments would be more detailed so as to surface tags.
Here is how I suggest to implement Comment ASTNodes. There are two distinct scenarios under which comments occur, 1. Comment does not unambiguously belong to a particular ASTNode. Any ASTNode that implements method like List statements(); is a good candidate. E.g., in case of comments inside of the Block statement. In such case, Block.statements(); method shall return CommentStatement object in the proper place of List. E.g., // java file scope comment void foo () { // method block scope comment if (true) { // if statement block scope comment } else { /* else statement block scope comment */ } } 2. Comment is clearly associated with a particular node. E.g., Expression objects of, say, List MethodInvocation.arguments(), or List IfStatement.getExpression () In such case, Expression's methods Comment getPreceedingComment(); Comment getTrailingComment(); shall return Comment object, or null if no comment is present. Generally speaking, if there is an expression element that JLS allows to put a comment next to, it should implement two of the above methods. E.g., if (/* Preceding comment */ true /* Trailing comment */) {} A.foo(/* pre-comment */ 5 /* post comment */, 7, 11 /*post comment*/ ); int a = 5 /* post comment*/; will fall into this category Comment object shall be wrapped in an object appropriate for an ASTNode it is located within whenever it is necessary. E.g., as stated above, in case of block statement it shall be wrapped in CommentStatement; if a list of expressions is expected by the method returning a comment, it should be wrapped in CommentExpression, etc. toString () methods of the nodes shall reflect this change appropriately.
Created attachment 7016 [details] First rough implementation for ASTNode comments
If validated, this implementation could be put in M6...
Created attachment 7020 [details] First implementation for ASTNode comments This version pass all JDTCore tests, but unfortunately does not implements last design agreed with Martin (removal of JavadocEmbeddedTag)... This will be done hopefully tomorrow.
Created attachment 7076 [details] New version after Jim feedback Pass all JDT-Core and JDT-UI refactoring tests. Last design implemented (remove of JavadocEmbeddedTag).
Implementation will start as soon as Jim will provide final feedbacks on DOM AST comments spec which should synthesize our (ie. Jim, Martin, Philippe and I) last exchanges...
Fixed. Specific class jdt.core.tests.dom.ASTConverterJavadocTest has been created to test this new functionality. Currently it only has 2 tests and will be completed by the end of the week.. Jim, I've only added a reference to this bug in buildnotes_jdt-core.html file. May you add more information either in buildnotes or in this bug you think user should have about this new functionality? Thanks
The original problem has been partially addressed by recent changes detailed in bug 50683 and bug 50697. However, API for serializing an AST, including comments, is still an open issue. See bug 23152.
Verified for 3.0M7