Skip to content Skip to sidebar Skip to footer

How To Use Android Classes On A Sonarqube Custom Rule

I'm trying to develop a custom rule for SonarQube Java Plugin, where it will analyze Android code. I've already created some rules that just identify basic functions of Java as the

Solution 1:

Got it!

The problem was on the import of dependencies of SonarQube. I've added a lot of Android dependencies on my pom.xml and test after the alterations. After this, still not working. In some searches I've discovered that the responsable for do the interpretation of the classes was the android-maven-plugin. When I've imported by the pom interface of Eclipse, always get some error saying that some arctifacts was missing. This problem occurred because the remote dependency was corrupted or doesn't exist, so I downloaded the jar file from dependency and create a file target/test-jars. With this, the maven identify the external dependencies and proceed with the test by JUnit.

To validate the informations, I've printed the attributes of the classes on my rule Check file:

package org.sonar.template.java.checks;

import com.google.common.collect.ImmutableList;

import org.sonar.check.Priority;
import org.sonar.check.Rule;
import org.sonar.plugins.java.api.semantic.Symbol;
import org.sonar.plugins.java.api.semantic.Symbol.TypeSymbol;
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
import org.sonar.plugins.java.api.tree.*;
import org.sonar.plugins.java.api.tree.Tree.Kind;

import java.util.List;

@Rule(
    key = "SharedPreferencesRule",
    name = "Utilização de SharedPreferences no código",
    description = "Para cada utilização de SharedPreferences, é feito um alerta para revisar o que está sendo armazenado.",
    priority = Priority.CRITICAL,
    tags = {"attention point", "security"})
publicclassSharedPreferencesCheckextendsIssuableSubscriptionVisitor {

    @OverridepublicList<Kind> nodesToVisit() {
    returnImmutableList.of(Kind.METHOD_INVOCATION);
  }

  @OverridepublicvoidvisitNode(Tree tree){
      System.out.println("====================== Kind Finded ======================");

      MethodInvocationTree kindTree = (MethodInvocationTree) tree;

      Symbol symbol = (Symbol) kindTree.symbol();

      TypeSymbol classe = symbol.owner().enclosingClass();

      System.out.println("Name >>>>>>>>>>>>> " + symbol.name());
      System.out.println("Enclosing >>>>>>>>>>>>> " + classe);

      if (classe != null && classe.equals("SharedPreferences")){
          reportIssue(kindTree.firstToken(), "SharedPreferences sendo utilizado no código. Analisar!!");
      }

  }
}

I hope that it could help someone who is going throught the same or an similar problem.

If anyone can give a better explanation, would be really helpfull.

Thanks a lot!

Post a Comment for "How To Use Android Classes On A Sonarqube Custom Rule"