Can custom java rule report many (2+) issues during one execution of visitNode(Tree tree)?


(N.N.) #1

Hi all.
I am writing custom java rule that extends IssuableSubscriptionVisitor.

Main question: can rule call reportIssue(...) more, than once in visitNode(Tree tree)?

I checked sonar rules, they almost always have one invocation of reportIssue(...).

My goal is to create rule, that will perform some checks (5+) if class has specified annotation (e.g. “@XYZ”).
So should I write rule for every check or I can perform all checks in one invocation of visitNode(...)?

Current realization has about 8 checks in one rule. It works well locally during testing (JavaCheckVerifier.verify("src/test/files/", rule);). But in web SonarQube (local installation of community edition) another situation: almost always only one issue per file, even it has 5+.

Code from rule

public List<Tree.Kind> nodesToVisit() {
    // Register to the kind of nodes we want to be called upon visit.
    return ImmutableList.of(Tree.Kind.CLASS, Tree.Kind.INTERFACE);

And main part of rule:

public void visitNode(Tree tree) {
    ClassTree clazz = (ClassTree) (tree);
    List<AnnotationTree> classAnnotations = getAnnotations(clazz);
    AnnotationTree annotationDto = findAnnotationByName(classAnnotations, ANNOTATION_DTO);
    AnnotationTree swaggerApiModel = findAnnotationByName(classAnnotations, ANNOTATION_API_MODEL);

    if (annotationDto == null) {
        reportIssue(clazz.declarationKeyword(), String.format("Missed annotation @%s.", ANNOTATION_DTO));
	if (swaggerApiModel == null) {
        reportIssue(clazz.declarationKeyword(), String.format("Missed annotation @%s.", ANNOTATION_API_MODEL));

...and so on, about 10 reportIssue()

(Nicolas Peru) #2

reporting multiple issue in one visit is absolutely not a problem.
However those are going to have the same rule id and so this is more a question about how you want to categorize your issues in sonarqube UI and also how you want to share code between your checks.

(N.N.) #4

Thanks for answer @Nicolas_Peru

What did you mean? I run my checks locally, our company testing Sonar, we need custom rules and if all will be ok, we will buy and use branch analyse.

So can you help me, where is my mistake, what can I forgot to do? Locally via JavaCheckVerifier.verify rule works perfectly on test case with 8 issues (2 of them in one line). But in Sonar Qube it doesn’t work properly, some issues omitted… SonarQube 7.3, sonar-java-plugin-

(Nicolas Peru) #5

I mean you can write a parent class that is not annotated withsome rule id and the children are annotated with different rule ids. (just a way to factorize things). This was not about sharing your code with the world :wink:

(Nicolas Peru) #6

wild guess here : but are those issues a bit different (in message and/or in location) ?

(N.N.) #7

Yes, messages are different. Location not always. And in case equal location some issues omitted.
In debug mode from IDE I see map with all issues, all ok with it, but in web ui some missed…

Do you work tomorrow to continue our investigation?

(Nicolas Peru) #8

This is a community support, not professional support, please post a request with a clear problem and the community might help you to answer it.

Moreover, this is not a chat, so there absolutely not any requirement of synchronous interaction.

(N.N.) #9

Ok, I understand your idea.
@Nicolas_Peru But what about performance?

My custom rule should analyze class if and only if it annotated with e.g. @XYZ.

In case different rules per check the same class will be scanned more, than once, maybe 10 times… Classes for analysis about 1000.

Or I misunderstood logic of visiting java code by sonar?..
I checked source code, found some visitors, but nothing that can help me scan class only once and report many issues under different rules.


(N.N.) #10

My bad, I found mistakes in my code, which caused NPE, so some issues lost. All fine, one rule – multiple issues.

Maybe it will be better to use nested classes for checks?
Parent rule and nested rules. Parent will visit nodes and proxies checks to it nested classes (rules)…