在pmd中抑制违规行为

blu*_*sky 11 java spring pmd

当我运行pmd分析时,我发现了违规行为:

Each class should declare at least one constructor
Run Code Online (Sandbox Code Playgroud)

此违规是在Spring控制器上.这个控制器是由Spring实例化的,所以不应该调用这个类.

建议忽略此违规的方法是什么?

根据http://pmd.sourceforge.net/pmd-4.3/suppressing.html可以使用// NOPMD,但我只是想忽略特定的违规行为.

Hai*_*otr 15

PMD还支持@SuppressWarnings注释:

// This will suppress all the PMD warnings in this class
@SuppressWarnings("PMD")
public class Bar {
 void bar() {
  int foo;
 }
}
Run Code Online (Sandbox Code Playgroud)

或者只是一种警告:

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

您可能还想要研究的是创建规则集和排除项.也许您想禁用某个规则,或从PMD检查中排除某些文件.

  • PMD 还有关于抑制的[官方文档](https://pmd.github.io/latest/pmd_userdocs_suppressing_warnings.html):如果你想忽略多个警告: `@SuppressWarnings({"PMD.UnusedLocalVariable", "PMD .UnusedPrivateMethod"})` (2认同)