Hi
I am creating a custom rule in java to have an error message whenever the lamba expressions are not explained. I wrote the code like below.
import java.util.List;
import org.sonar.check.Priority;
import org.sonar.check.Rule;
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
import org.sonar.plugins.java.api.tree.*;
import org.sonar.plugins.java.api.tree.Tree.Kind;
import com.google.common.collect.ImmutableList;
@Rule(key = "LambdaExpressionsMustBeCommented", description = "The lambda expressions must be explained in a comment.",
priority = Priority.MAJOR)
public class LambdaExpressionsMustBeCommentedRule extends IssuableSubscriptionVisitor {
@Override
public List<Kind> nodesToVisit(){
return ImmutableList.of(Tree.Kind.LAMBDA_EXPRESSION);
}
@Override
public void visitNode(Tree tree) {
if (tree.is(Tree.Kind.LAMBDA_EXPRESSION)) {
LambdaExpressionTree lambdaExpression = (LambdaExpressionTree) tree;
String lambdaAsString = lambdaExpression.toString();
if (!lambdaAsString.matches(".*//.*[^\\n\\r]")) {
reportIssue(tree, "You must comment the lamda expression in order to explain it. It has to be on the same line too.");
}
}
}
}
The issue is that the comments are not read with that rule. I fell like the list is getting only the lambda expressions and not the full line as I want.
I also tired.
if (!lambdaAsString.contains("//") || !lambdaAsString.contains("/*")) {
Does anyone an give me an advice to help me ?
I am using sonar 10.1 and java 17.
Thank you all. Have a nice day
Gab.