Check javadoc description with custom rule in java

Hi, i need to implement a rule for check javadoc description on a java method.
The rule also needs to check out if the javadoc for the method has the following structure:

/*
** Purpose:
** Prerequisites:
** Design steps:
*/
pubic void someMethod() {
}

I started as follows - but I do not see any possibility to ask for the description of the method. Can anyone here help me - thank you!

    public List<Tree.Kind> nodesToVisit() {
        return Collections.singletonList(Tree.Kind.METHOD);
    }

    @Override
    public void visitNode(Tree tree) {
        MethodTree methodTree = (MethodTree) tree;

Hello @itWorks,

Comments are only attached to tokens in our AST. So, in order to get them, you need to access comments on top of a method, you need to consider “trivias” attach to its first token:

List<SyntaxTrivia> trivias = methodTree.firstToken().trivias();

From there, I let you figure it out on how to exploit the content of the comments. You might want to have a look at some classes we use for our own check:

I hope this helps,
Michael

Thanks for your reply!
I have done the following implementation - unfortunately the array ‘trivias’ is always empty…

@Rule(key ="JavadocTestDescription")
public class JavadocTestDescriptionCheck extends IssuableSubscriptionVisitor {

    @Override
    public List<Tree.Kind> nodesToVisit() {
        return Collections.singletonList(Tree.Kind.METHOD);
    }

    @Override
    public void visitNode(Tree tree) {
        MethodTree methodTree = (MethodTree) tree;
        List<SyntaxTrivia> trivias = methodTree.firstToken().trivias();
        System.out.println("=" + trivias);
    }

Can you give me a hint about this?
Thanks!

Hello,

I think that you are having issues with your test file. Please check the following:

  • Are you sure the test file compiles?
  • Can you share the whole file you are using as input?
  • Can you make sure you are using the LATEST version of the sonar-java-plugin dependency
    • 6.3.2, if you target SQ LTS
    • 6.7, if you target SQ 8.X

On my side I tested your code with the following test file:

package org.foo;

public class MyClass {
  /*
   ** Purpose:
   ** Prerequisites:
   ** Design steps:
   */
  public void foo() {
  }

  /**
   * some documentation
   */
  public void bar() {
  }
}

and I get this:

=[org.sonar.java.model.InternalSyntaxTrivia@106cc338]
=[org.sonar.java.model.InternalSyntaxTrivia@7a67e3c6]

And the following if I change your way of displaying the result to:

    String comments = tree.firstToken()
      .trivias()
      .stream()
      .map(SyntaxTrivia::comment)
      .collect(Collectors.joining());
    System.out.println(tree.simpleName() + "=" + comments);
foo=/*
   ** Purpose:
   ** Prerequisites:
   ** Design steps:
   */
bar=/**
   * some documentation
   */

Please use ``` to format the code in your messages, it makes it easier to read.

Thanks for your help - i will try out the next week and give you feedback.
sorry for bad formatting - beginner mistake - very good support!

hello @itWorks,

Did you manage to make it works? If it’s the case, could you please mark my answer as the solution?

Thanks,
Michael

Hi Michael,
sorry I’m late - I started yesterday and was able to solve my problems with your help.
Thanks for the great support!

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