Check Variable Type in Application

Hi,

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.

Regards,

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):

  @Override
  public void visitClass(ClassTree tree) {
    super.visitClass(tree);
    if(flag)
      //Report issue
  }

  @Override
  public void visitVariable(VariableTree tree) {
    flag = true;
    super.visitVariable(tree);
  }

Hope this helps,
Quentin

Thanks @Quentin_Jaquier It was done.