Java Custom Rule test with more than 1 File

Greetings. I am a novice learner in SonarQube world. While trying to extend with a new rules, I got stuck in 2 issues:

  1. API public static void verify(String filename, JavaFileScanner check) is used for testing any new rules where filename– The file to be analyzed
    check – The check to be used for the analysis
    My question is if my rule (check) needs more than 1 file to validate, how can I do that. I’ve not found any API that allows more than 1 file! More clearly, we can only check the rule against a class tree and have no access to other class’s context. Please correct me if I am wrong somehow.

  2. Based on previous point, do SonarQube engine follow the same strategy-- check the rules file by file or apply the rules on the whole project? In the later case, how can we get to the similar environment in the test environment.

Many thanks in advance.

Hi @Mike,

You are indeed right in your analysis, the vast majority of the time the analysis (in java) is done file by file and so you have (to some extent) not access to other trees of other files.

There are peculiar cases where this is not true in SonarJava but this is not supported as an API and so not available for custom plugin writing.

Note that you have access to the semantic API that gives you a lot of information on symbols and types though.

The question would then be : what are you trying to achieve ? could you describe a bit your usecase so we can eventually tell you if there’s a way with the current java API for java rules to achieve it.

Thank you, Nicolas. We would like to detect race conditions which requires looking into external methods to see if there is any?

hey @Mike,

sorry for late reaction. Are you still having the issue? Can you post an example what kind of race conditions are you trying to detect?