如何屏蔽/忽略sonar的规则(如何去除误判的违规)

sonar使用的代码静态检查工具(插件), 主要是3个插件findbugs, PMD, checkstyle.

针对这3个插件,都可以通过方法来忽略掉相应的检查规则.这样可以将误判的代码忽略掉检查,可以在sonar检查后的报告中,呈现更好看的数据. 唉,领导嘛,总是希望数据证明自己的功绩. 就和gdp一样,一般是不会考虑细节问题. 所以开发人员可以适当的利用规则,作数据.

findbugs:

1. 通过filter files , 这个方式不如直接在代码中加注释的方式简单,且非常的不方便.由于findbugs 是基于bytecode字节码的检查进行工作的,所以无法通过注释的方式.

http://findbugs.sourceforge.net/manual/filter.html

2. 通过annotation 方式. http://findbugs.sourceforge.net/api/edu/umd/cs/findbugs/annotations/SuppressWarnings.html

PMD:

http://pmd.sourceforge.net/pmd-5.0.1/suppressing.html

1. annotation

You can use a JDK 1.5 annotation to suppress PMD warnings, like this:

// This will suppress all the PMD warnings in this class
@SuppressWarnings("PMD")
public class Bar {
 void bar() {
  int foo;
 }
}
Or you can suppress one rule with an annotation like this:

// This will suppress UnusedLocalVariable warnings in this class
@SuppressWarnings("PMD.UnusedLocalVariable")
public class Bar {
 void bar() {
  int foo;
 }
}

2. 通过代码注释的方式

写在某行代码后面  code; ///NO_PMD

checkstyle

1. 通过代码注释方式 此方法有待于实际考证.

//CHECKSTYLE:OFF
public void someMethod(String arg1, String arg2, String arg3, String arg4) {
//CHECKSTYLE:ON

下面可以对中间的代码对具体的规则进行忽略检查

//CHECKSTYLE.OFF: IllegalCatch - Much more readable than catching 7 exceptions
catch (Exception e)
//CHECKSTYLE.ON: IllegalCatch

2. 通过filter配置

http://checkstyle.sourceforge.net/config.html

此篇文章已被阅读6390 次

Tags:

Add a Comment

邮箱地址不会被公开。 必填项已用*标注