Check Variable Type in Application


Sonarqube v7.7

I am trying to validate a variableType and VariableName whether it is declared and used inside methods.

For eg: Usage of Loggers and log.types(info,debug…)

If the class and methods declared inside the class not consists of any usage of loggers, I have to raise issue. For that with the help of VariableTree I am validating the data. But when i run the sonar-scanner it is reporting the issues for each variable usage (getting same issue message) in class.

I want to raise the issue only once in a class, if there is no usage of loggers. Any possible way to scan all variable at a time and return issue once if the variable is not found.


Hello @AnuragSanagapalli,

Sorry for the late reply, we were quite busy working on SonarJava front-end migration!

To solve your problem, you can set a flag when you visit a VariableTree, and report the issue only once you visit the class, something like this (using a BaseTreeVisitor):

  public void visitClass(ClassTree tree) {
      //Report issue

  public void visitVariable(VariableTree tree) {
    flag = true;

Hope this helps,

Thanks @Quentin It was done.