Is there a way to skip specific method in sonar-scanner without using ignore block?

Most of our project written in java uses EMF (Eclipse Modeling Framework) to generate java code. Generated classes and method are tagged with ‘@generated’ and must be ignored. Here are the sample snapshot of generated method:

/*
* This is a sample java function
*
* @generated
*/
public void myFunction(){
    ...
}

We only want to skip classes or method with ‘@generated’ tag and not the whole file since the file has modified methods that are tagged as ‘@generated NOT’ and must be included in sonar analysis. We are aware of using ignore blocks to ignore parts of a file, but it seems that it will not be a suitable solution since it would require to update thousand of java file.

We are using the following in our environment:

  • SonarQube Scanner 3.0.3.778
  • Java 1.8.0_241 Oracle Corporation (64-bit)
  • SonarQube server 7.9.1

Please advice if there are other way to ignore blocks of code or if it this scenario is possible in sonarqube. Thanks!

Hi,

There’s not a good, language-agnostic way to do this. Nor is there a Java-specific way.

However, if you can configure your generation so that all the genned methods are added at the bottom of the file, then the Ignore Issues in Blocks method should work for you. Configure @generated as the start of the block and leave the end marker empty, and according to the docs

If the first regular expression is found but not the second one, the end of the file is considered to be the end of the block.

 
HTH,
Ann

3 Likes

Your suggestion sounds good, we will consider this approach. Thanks!