How to get the comment content inside a method?

One rule I’m writing now is:

Method, start another line above the annotated statement, and use // to comment.

In the actual development, trying use visitMethod and visitClass can’t get the internal annotation content of the method.

Do you have any feasible methods?Thank you for following this post.

Because the content of my previous rules is not very accurate and easy to confuse, I specially corrected it.

The method uses single line comments internally, such as / / comments. Multi line comments or Javadoc comments are prohibited

Hey @KevinJin ,

What do you mean by internal annotation ?
Can you share an example of code that would contain something that your rule should catch?



I want to standardize the internal annotation writing method of the method. Only one rule of / / single line annotation can be used.

Fortunately, I have finished this rule with methodTree.modifiers().firstToken().trivias().

Thank you for your kindly reply.


Hello @KevinJin ,

Okay, so what you call “internal annotations” are absolutely not annotations according to the Java languages. These are comments or javadoc.

In our API, you can access them through the modifiers() of some elements from the AST (such as MethodThree). However, the simplest way is to retrieve directly from any syntax token, using the trivias() method. You can also decide to use a SubscriptionVisitor, and register to the TRIVIA kind.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.