Skip to content

Comments (Features)

Danny van Bruggen edited this page Jul 9, 2016 · 3 revisions

Support available for comments in the JavaParser

JavaParser can be used in many different ways, therefore different users can need different levels of support for parsing comments.

We identified four typical scenarios:

  1. Some users are interested exclusively in the proper Abstract Syntax Tree and not concerned with comments, that is why comments parsing is optional. For example invoking the method parse(final InputStream in, final String encoding, boolean considerComments) the last parameter specifies whether or not comments should be parsed.

  2. Other users could be interested to know which comments are present in a file, without the need to consider their position or the elements they refer to. We do provide a method to collect all the comments which are contained in a node and its children (Node.getAllContainedComments()). Calling this method on CompilationUnit gives all the comments in the source file.

  3. Others may want some comments attribution, normally at the level of class and methods. Those users can use the method Node.getComment() on the element for which they want to retrieve the information. This method returns the comment that is more closely associated to the element (typically a JavadocComment), however that doesn't have to be the case. For the details about the algorithm that assigns comments to elements please refer to this page.

  4. Finally, some could be interested in comments attribution at the finer levels. Those users can collect all the comments contained in a method by calling Node.getAllContainedComments() on that element, they can retrieve the comment associated to each element (each statement or part of a statement could have comments associated) using Node.getComment(). Users can also retrieve the list of comments that were not associated to any statements Node.getOrphanComments() on a container like a method or a class.

More demanding users can also tweak the strategy for attributing comments to elements by using JavaParser.setDoNotAssignCommentsPreceedingEmptyLines(boolean) and setDoNotConsiderAnnotationsAsNodeStartForCodeAttribution(boolean)

Clone this wiki locally