itWorks
(Heiko )
September 15, 2020, 2:06pm
1
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;
Michael
(Michael Gumowski)
September 17, 2020, 6:23am
2
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
itWorks
(Heiko )
September 17, 2020, 1:41pm
3
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!
Michael
(Michael Gumowski)
September 17, 2020, 2:39pm
4
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.
itWorks
(Heiko )
September 17, 2020, 3:22pm
5
Thanks for your help - i will try out the next week and give you feedback.
sorry for bad formatting - beginner mistake - very good support!
Michael
(Michael Gumowski)
September 30, 2020, 4:15pm
6
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
itWorks
(Heiko )
October 1, 2020, 7:47am
7
Hi Michael,
sorry I’m late - I started yesterday and was able to solve my problems with your help.
Thanks for the great support!
system
(system)
Closed
October 8, 2020, 7:47am
8
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.