Turning Sonar off for certain code

Ant Kutschera picture Ant Kutschera · Jun 10, 2012 · Viewed 186.9k times · Source

Is it possible to turn off sonar (www.sonarsource.org) measurements for specific blocks of code, which one doesn't want to be measured?

An example is the "Preserve Stack Trace" warning which Findbugs outputs. When leaving the server, I might well want to only pass the message back to the client, not including the actual exception which I just caught, if that exception is unknown to the client (because the client doesn't have the JAR in which that exception was contained for example).

Answer

maestr0 picture maestr0 · Apr 1, 2015

You can annotate a class or a method with SuppressWarnings

@java.lang.SuppressWarnings("squid:S00112")

squid:S00112 in this case is a Sonar issue ID. You can find this ID in the Sonar UI. Go to Issues Drilldown. Find an issue you want to suppress warnings on. In the red issue box in your code is there a Rule link with a definition of a given issue. Once you click that you will see the ID at the top of the page.