简体   繁体   中英

Custom rule for multiple java classes

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;
  }
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM