Custom rule for multiple java classes

297 Views Asked by At

Can you please let me know if there is way to write a check when it spans across multiple java classes.

For Example: we want to check if there is DB call made in loop, in the below example the call from first class is made to a second class. Can we want check if helper method is a call to DB. Is it possible to write a rule for this scenario?

None of the existing rules span across multiple java classes https://rules.sonarsource.com/java

First.java

public class First {
 public void method1(){
      Second secRef = new Second();
      List<String> res = new ArrayList<>();

      for(int i=0;i<n;i++){
           red.add(secRef.helper(i));
      }
 }
}

Second.java

public class Second{
  public String helper(int i){
        //      call database
        
        String result;
        result = DAO.callSQL(i);
        return result;
  }
}
1

There are 1 best solutions below

0
On

As you asked in the sonarqube wiki, I believe it is not possible with classic custom rules of SonarQube.

You can try to create your own tool that deals with such a rule and integrate it with the generic issue format.

Personally, we are working with Moose to implement such a rule with our brand-new MooseCritic tool. However, it is still far from ready for production.