How exclude simple getter and setter from sonar?

2019-07-17 06:10发布

问题:

There is the way to exclude getter and setters from sonar report. Suppose I have 2 "getters":

public int getId(){
    return this.id;
}

public int getComplexId(){
    int result = 0;
    // some complex calculation there
    return result;
}

It is possible to exclude getId() and include getComplexId() simultaneously? Can Sonar analyze simple return this.id from complex code?

回答1:

You can use NOPMD comment to avoid Sonar analysis.

public int getId(){ // NOPMD
    return this.id;
}

public int getComplexId(){ 
    int result = 0;
    // some complex calculation there
    return result;
}

Also you can use //NOSONAR or //CHECKSTYLE:OFF comment. More info in http://www.sonarqube.org/sonar-1-12-in-screenshots/



回答2:

@Cherry, out of the box SonarQube already behaves as expected : the first method is considered as a getter and not the second one as this method contains some logic.