静态代码检查工具PMD安装、使用

介绍

PMD官网:https://pmd.github.io/
文档:https://docs.pmd-code.org/latest/
https://docs.pmd-code.org/latest/pmd_languages_java.html
Java检查规则:https://docs.pmd-code.org/pmd-doc-7.0.0-rc3/pmd_rules_java.html

PMD是一款可扩展的跨语言的静态代码检查工具,它可以发现普通的编程错误,例如未使用的变量、空的catch块、不必要的对象创建等。它支持Java、Apex等10几种编程语言。
它还包含CPD检测器(copy-paste-detector),用于检测代码中的重复代码。
在这里插入图片描述

用maven PMD插件扫描生成报告方便,而用eclipse PMD插件可以实时看到结果,修改起来方便。两种是独立的,可以结合起来使用

PMD对Java版本的支持

https://docs.pmd-code.org/latest/pmd_languages_java.html
在这里插入图片描述

PMD源码

https://pmd.github.io/
在这里插入图片描述

用maven安装、运行

安装PMD

第一次执行maven的pmd:pmd这个goal,就会将maven的PMD插件、连同PMD的版本都下载到maven本地仓库。例如执行后,下载的是PMD 6.55.0版本:
在这里插入图片描述
在这里插入图片描述
到maven本地仓库中看:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

安装Maven PMD 插件

https://maven.apache.org/plugins/maven-pmd-plugin/

Maven PMD 插件允许在maven工程的源代码上自动运行PMD代码分析,生成一个检查结果的site报告,它也支持单独的Copy/Paste Detector tool (即CPD)。

Maven PMD 插件3.21.0版本需要Java 8。支持在运行期间升级PMD。

第一次执行maven的pmd:pmd,就会将maven的PMD插件、连同PMD的版本都下载下来:
在这里插入图片描述
在这里插入图片描述

可以看到,下载的是maven PMD插件3.21.0版本。

打开maven-pmd-plugin-3.21.0.pom这个文件,看看内容片段:
在这里插入图片描述
在这里插入图片描述
可以看到,其中指定了对PMD 6.55.0的依赖,所以下载插件的时候,顺便就把PMD的版本下载下来了。

Maven PMD 插件版本和PMD版本之间的关系

https://maven.apache.org/plugins/maven-pmd-plugin/examples/upgrading-PMD-at-runtime.html
在这里插入图片描述

Maven PMD 插件使用的Java检查默认规则集

在maven本地仓库中找到Maven PMD 插件的目录:
在这里插入图片描述
打开maven-pmd-plugin-3.21.0.jar这个文件,在其中可以找到/rulesets/java/maven-pmd-plugin-default.xml,这个就是Maven PMD 插件使用的Java检查默认规则集:
在这里插入图片描述

打开这个文件的内容可以看到,里边只包含了很少一部分规则:

<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
-->
<ruleset name="Default Maven PMD Plugin Ruleset"
    xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd">

    <description>
        The default ruleset used by the Maven PMD Plugin, when no other ruleset is specified.
        It contains the rules of the old (pre PMD 6.0.0) rulesets java-basic, java-empty, java-imports,
        java-unnecessary, java-unusedcode.

        This ruleset might be used as a starting point for an own customized ruleset [0].

        [0] https://pmd.github.io/latest/pmd_userdocs_making_rulesets.html
    </description>

    <rule ref="category/java/bestpractices.xml/AvoidUsingHardCodedIP" />
    <rule ref="category/java/bestpractices.xml/CheckResultSet" />
    <rule ref="category/java/bestpractices.xml/PrimitiveWrapperInstantiation" />
    <rule ref="category/java/bestpractices.xml/UnusedFormalParameter" />
    <rule ref="category/java/bestpractices.xml/UnusedLocalVariable" />
    <rule ref="category/java/bestpractices.xml/UnusedPrivateField" />
    <rule ref="category/java/bestpractices.xml/UnusedPrivateMethod" />

    <rule ref="category/java/codestyle.xml/EmptyControlStatement" />
    <rule ref="category/java/codestyle.xml/ExtendsObject" />
    <rule ref="category/java/codestyle.xml/ForLoopShouldBeWhileLoop" />
    <rule ref="category/java/codestyle.xml/TooManyStaticImports" />
    <rule ref="category/java/codestyle.xml/UnnecessaryFullyQualifiedName" />
    <rule ref="category/java/codestyle.xml/UnnecessaryImport" />
    <rule ref="category/java/codestyle.xml/UnnecessaryModifier" />
    <rule ref="category/java/codestyle.xml/UnnecessaryReturn" />
    <rule ref="category/java/codestyle.xml/UnnecessarySemicolon" />
    <rule ref="category/java/codestyle.xml/UselessParentheses" />
    <rule ref="category/java/codestyle.xml/UselessQualifiedThis" />

    <rule ref="category/java/design.xml/CollapsibleIfStatements" />
    <rule ref="category/java/design.xml/SimplifiedTernary" />
    <rule ref="category/java/design.xml/UselessOverridingMethod" />

    <rule ref="category/java/errorprone.xml/AvoidBranchingStatementAsLastInLoop" />
    <rule ref="category/java/errorprone.xml/AvoidDecimalLiteralsInBigDecimalConstructor" />
    <rule ref="category/java/errorprone.xml/AvoidMultipleUnaryOperators" />
    <rule ref="category/java/errorprone.xml/AvoidUsingOctalValues" />
    <rule ref="category/java/errorprone.xml/BrokenNullCheck" />
    <rule ref="category/java/errorprone.xml/CheckSkipResult" />
    <rule ref="category/java/errorprone.xml/ClassCastExceptionWithToArray" />
    <rule ref="category/java/errorprone.xml/DontUseFloatTypeForLoopIndices" />
    <rule ref="category/java/errorprone.xml/EmptyCatchBlock" />
    <rule ref="category/java/errorprone.xml/JumbledIncrementer" />
    <rule ref="category/java/errorprone.xml/MisplacedNullCheck" />
    <rule ref="category/java/errorprone.xml/OverrideBothEqualsAndHashcode" />
    <rule ref="category/java/errorprone.xml/ReturnFromFinallyBlock" />
    <rule ref="category/java/errorprone.xml/UnconditionalIfStatement" />
    <rule ref="category/java/errorprone.xml/UnnecessaryConversionTemporary" />
    <rule ref="category/java/errorprone.xml/UnusedNullCheckInEquals" />
    <rule ref="category/java/errorprone.xml/UselessOperationOnImmutable" />

    <rule ref="category/java/multithreading.xml/AvoidThreadGroup" />
    <rule ref="category/java/multithreading.xml/DontCallThreadRun" />
    <rule ref="category/java/multithreading.xml/DoubleCheckedLocking" />

    <rule ref="category/java/performance.xml/BigIntegerInstantiation" />

</ruleset>

那么,规则集中参考的ref=“category/java/bestpractices.xml/AvoidUsingHardCodedIP” 引用的规则具体来自哪里呢?这个来自PMD版本的压缩包中:
在这里插入图片描述

打开pmd-java-6.55.0.jar这个包,可以看到携带的Java检查规则集文件,分了8个大类:
在这里插入图片描述

8个大类的规则集文件跟PMD官网中Java规则集正好对应:
https://docs.pmd-code.org/pmd-doc-7.0.0-rc3/pmd_rules_java.html

在这里插入图片描述

打开categories.properties文件,内容 如下:
在这里插入图片描述

打开bestpractices.xml,看看内容片段:
在这里插入图片描述

Maven PMD 插件的goals

https://maven.apache.org/plugins/maven-pmd-plugin/plugin-info.html
在这里插入图片描述

pmd:pmd

https://maven.apache.org/plugins/maven-pmd-plugin/pmd-mojo.html

这个goal生成PMD site报告,默认也生成xml格式的报告。

例如,在cmd窗口下执行mvn pmd:pmd:
在这里插入图片描述

到target目录下查看内容:
在这里插入图片描述
打开pmd.xml文件,里边存放着xml形式呈现的PMD违反项:
在这里插入图片描述

下面是自动拷贝过来的规则集:
在这里插入图片描述

下面是生成的html形式的报告:
在这里插入图片描述

打开报告看看内容:
在这里插入图片描述

pmd:aggregate-pmd

https://maven.apache.org/plugins/maven-pmd-plugin/aggregate-pmd-mojo.html

当maven过程中包含几个模块的时候,可以用这个goal生成一个PMD集成报告。这个goal生成PMD site报告,默认也生成xml格式的报告。

例如,在cmd窗口下执行mvn pmd:aggregate-pmd(输出太长,只部分截屏):
在这里插入图片描述
在这里插入图片描述

到target目录下查看:
在这里插入图片描述

自动拷贝过来的规则集文件:
在这里插入图片描述
pmd.xml是xml形式的集成报告:
在这里插入图片描述

html形式的集成报告:
在这里插入图片描述
在这里插入图片描述

pmd:cpd

https://maven.apache.org/plugins/maven-pmd-plugin/cpd-mojo.html

用PMD的 Copy/Paste Detector (CPD) 工具生成一个拷贝、粘贴的报告。这个goal生成PMD site报告,默认也生成xml格式的报告。

例如,在cmd下执行mvn pmd:cpd:
在这里插入图片描述

到target目录下看看内容:
在这里插入图片描述
cpd.xml是xml形式的报告:
在这里插入图片描述

html形式的报告:
在这里插入图片描述
在这里插入图片描述

pmd:aggregate-cpd

https://maven.apache.org/plugins/maven-pmd-plugin/aggregate-cpd-mojo

当maven过程中包含几个模块的时候,可以用这个goal调用PMD的 Copy/Paste Detector (CPD) 工具生成一个拷贝、粘贴的集成报告。这个goal生成PMD site报告,默认也生成xml格式的报告。

例如,在cmd下执行mvn pmd:aggregate-cpd:
在这里插入图片描述
在这里插入图片描述

到target目录下查看:
在这里插入图片描述
xml形式的报告:
在这里插入图片描述
html形式的报告:
在这里插入图片描述
在这里插入图片描述

pmd:check

https://maven.apache.org/plugins/maven-pmd-plugin/check-mojo.html

如果在源代码中发现PMD的违反项就停止构建。在调用自身之前,会先自动调用pmd:pmd。这个goal生成PMD site报告,默认也生成xml格式的报告。

例如,在cmd下执行mvn pmd:check:
在这里插入图片描述
在这里插入图片描述

到target目录下查看下内容:
在这里插入图片描述

xml形式的报告:
在这里插入图片描述

html形式的报告:
在这里插入图片描述
在这里插入图片描述

pmd:aggregate-pmd-check

https://maven.apache.org/plugins/maven-pmd-plugin/aggregate-pmd-check-mojo.html

如果一个工程包含几个模块,执行这个goal的时候,在源代码中发现PMD的违反项就停止构建。在调用自身之前,会先自动调用pmd:aggregate-pmd。这个goal生成PMD site报告,默认也生成xml格式的报告。

例如,在cmd下执行mvn pmd:aggregate-pmd-check(结果很长,只有部分截屏):
在这里插入图片描述
在这里插入图片描述

到target目录下看看内容:
在这里插入图片描述

xml形式的报告:
在这里插入图片描述

html形式的报告:
在这里插入图片描述

在这里插入图片描述

pmd:cpd-check

https://maven.apache.org/plugins/maven-pmd-plugin/cpd-check-mojo.html

执行拷贝、粘贴检查,如果有违反项,就停止构建。在执行这个goal之前,会自动先调用pmd:cpd这个goal。生成xml和html形式的报告。

在cmd下执行mvn pmd:cpd-check:
在这里插入图片描述
在这里插入图片描述
到target目录下查看内容:
在这里插入图片描述

xml形式的报告:
在这里插入图片描述

html形式的报告:
在这里插入图片描述
在这里插入图片描述

pmd:aggregate-cpd-check

https://maven.apache.org/plugins/maven-pmd-plugin/aggregate-cpd-check-mojo.html

如果工程有几个模块,执行拷贝、粘贴集成检查,如果有违反项,就停止构建。在执行这个goal之前,会自动先调用pmd:aggregate-cpd这个goal。生成xml和html形式的报告。

在cmd下执行mvn pmd:aggregate-cpd-check(输出很长,只部分截屏):
在这里插入图片描述
在这里插入图片描述

到target目录下查看:
在这里插入图片描述

xml形式的报告:
在这里插入图片描述

html形式的报告:
在这里插入图片描述
在这里插入图片描述

maven PMD插件引用定制的规则集文件

例如,定制的规则集文件的路径:D:\temp\eclipse-workspace\java_work\config\pmd\java\pmd-java-rules.xml

maven工程的pom文件增加如下配置:

  <build>
	<pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-pmd-plugin</artifactId>
          <version>3.21.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
    <plugins>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-pmd-plugin</artifactId>
          <configuration>
            <rulesets>
              <ruleset>D:/temp/eclipse-workspace/java_work/config/pmd/java/pmd-java-rules.xml</ruleset>
            </rulesets>
          </configuration>
        </plugin>
     </plugins>
  </build>
  <reporting>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-pmd-plugin</artifactId>
        <configuration>
          <rulesets>
            <ruleset>D:/temp/eclipse-workspace/java_work/config/pmd/java/pmd-java-rules.xml</ruleset>
          </rulesets>
        </configuration>
      </plugin>
    </plugins>
  </reporting>

eclipse PMD插件安装、运行

安装eclipse PMD插件

进入Eclipse Marketplace:
在这里插入图片描述
在搜索框中输入PMD,回车:
在这里插入图片描述

点击后面的install按钮:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

点击Finish按钮:
在这里插入图片描述
在这里插入图片描述

安装一会后,弹出如下窗口:
在这里插入图片描述

点击Select All按钮:
在这里插入图片描述

点击Trust Selected按钮:
在这里插入图片描述

安装完成后,弹出如下窗口:
在这里插入图片描述

点击Restart Now按钮,重启eclipse。eclipse重启后,点击Window->Preferences菜单:
在这里插入图片描述
在这里插入图片描述

点击左侧导航栏的PMD,出现如下界面,说明已经安装成功:
在这里插入图片描述

右侧窗口中,可以看到PMD违反项每个基本的名字、符号:
在这里插入图片描述

右边窗口下边可以看到eclipse PMD插件的版本、PMD的版本:
在这里插入图片描述

配置eclipse PMD插件的信息

在这里插入图片描述
在这里插入图片描述

CPD(拷贝、粘贴检测器)配置

点击Launch CPD按钮:
在这里插入图片描述
在这里插入图片描述

有多个选项,例如可以选择源文件的根目录、语言是Java或者其它的语言:
在这里插入图片描述
备注:在PMD CPD这个配置界面,如果点击右上角的关闭按钮,本来就想关闭这个配置界面的,但会把eclipse一起关闭,这点不友好。

Rule Configuraton(规则配置)

在这里插入图片描述

  • 在右侧最上面勾选Use global rule management以后,在这个地方可以全局配置去掉某个规则,下面的规则集就可以选上、或者不选。这个设置会覆盖工程特有的选择:
    在这里插入图片描述

  • 在Rules grouped by选择Rule set,可以按照类来分组,共8个大类(这里边的规则包含了所有语言的):
    在这里插入图片描述

  • 按语言分类查看:
    在这里插入图片描述

  • 按PMD版本查看:
    在这里插入图片描述

  • 按优先级查看:
    在这里插入图片描述

  • 点击右面的Import按钮,可以导入外部规则集,跟现在的合并:

在这里插入图片描述
在这里插入图片描述

  • 点击右边的Export按钮,可以将现在的规则集导出到一个文件中。例如,要将Java的规则集导出到一个外部文件中,可以先按照语言分类查看,然后选中Java这一行:
    在这里插入图片描述
    点击右边的Export按钮:
    在这里插入图片描述
    输入要导出的文件名:
    在这里插入图片描述
    输入规则集描述(这个描述会放到导出的文件的描述中):
    在这里插入图片描述
    在这里插入图片描述

打开导出的文件看看:
在这里插入图片描述

  • 如果规则有属性,在Properties栏会有显示,在下面的Properties这个Tab页,可以设置属性:

在这里插入图片描述

用eclipse PMD插件在工程上执行PMD检查

鼠标右键点击工程,选择PMD->Check Code:
在这里插入图片描述

Violations Outline这个Tab页显示的是上面打开的文件的违反项,点击下面某个违反项,上面对应的代码高亮显示:
在这里插入图片描述

在这里插入图片描述

Violations Overview这个Tab页显示了按照文件汇总的信息,下面可以展开:
在这里插入图片描述

在这里插入图片描述

在eclipse工程上使能PMD

在工程上使能PMD的好处是,如果编码的过程保存以后有违反项,可以立即给出提示。

在工程上鼠标右键单击,点击上下文菜单中的Properties:

在这里插入图片描述
左侧导航栏选中PMD,右侧窗口中选中Enable PMD:
在这里插入图片描述

单击Select a working set按钮,在弹出窗口中选择,例如:
在这里插入图片描述

点击Apply and Close按钮。

例如实时显示的违反项:
在这里插入图片描述

在eclipse工程上检查疑似拷贝、粘贴代码

在工程上单击鼠标右键,在弹出菜单上选择PMD->Find Suspect Cut And Paste:
在这里插入图片描述

选择源代码的语言,例如Java:
在这里插入图片描述

选择输出检查报告文件的类型:
在这里插入图片描述

点击OK按钮:
在这里插入图片描述

在工程下面生成了reports/cpd-report.xml文件:
在这里插入图片描述

打开这个文件看看内容片段:
在这里插入图片描述
在这里插入图片描述

在eclipse复合工程(工程中包含了几个子工程)上检查疑似拷贝、粘贴代码

这个父工程下面包括了三个子工程(是maven的三个模块工程):
在这里插入图片描述

在父工程上单击鼠标右键,在弹出菜单上选择PMD->Find Suspect Cut And Paste:
在这里插入图片描述

选择语言类型、输出报告的文件类型:
在这里插入图片描述

点击OK按钮,在父工程的目录下生成了reports/cpd-report.xml文件:
在这里插入图片描述

定制PMD规则集文件

https://docs.pmd-code.org/pmd-doc-6.55.0/pmd_userdocs_making_rulesets.html
https://docs.pmd-code.org/pmd-doc-6.55.0/pmd_userdocs_configuring_rules.html
https://docs.pmd-code.org/pmd-doc-6.55.0/pmd_userdocs_best_practices.html

在哪个基础上定制?

  • 可以在maven PMD插件默认规则集文件的基础上定制。但这可能不是有一个好方法。因为它包含的规则太少了,如果要增加的话,要手工录入很多,比较繁琐。
  • 另外一个比较省力的方法是打开eclipse,用eclipse PMD插件(如果没有安装这个插件,要先安装,安装的方法见上面的章节介绍)导出Java这一语言的检查规则,然后在此基础上定制。如何用eclipse PMD插件导出规则到外部文件中,见上面的章节描述。

建议每条采用引用单个规则的方式,例如:
在这里插入图片描述
前面章节说过,category/java/errorprone.xml/EmptyCatchBlock这样的源在PMD发布包中。
引用单个规则的好处是自己可以完全控制,因为每个规则有很多属性可以利用。

当然,每条也可以采用大块引用的方式(引用了xml文件中的所有规则,但不推荐这种方式),例如:
在这里插入图片描述
这种添加方式是简单、快捷,但自己不能完全控制,PMD以后的版本可能增加、或者减少规则,默认都会被引用进来,而自己可能还不知情。

可以把定制的规则集文件放到某个目录下面,被引用。例如:
D:/temp/eclipse-workspace/java_work/config/pmd/java/pmd-java-rules.xml

附:PMD 6.55.0版本规则全集(去掉了已经废弃的和7.0版本将要去掉的规则):

<?xml version="1.0" encoding="UTF-8"?>

<ruleset name="Custom Rules"
    xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd">

    <description>
        Custom rules for java.
    </description>

    <rule ref="category/java/bestpractices.xml/CheckResultSet" />
    <rule ref="category/java/bestpractices.xml/PrimitiveWrapperInstantiation" />
    <rule ref="category/java/bestpractices.xml/UnusedFormalParameter" />
    <rule ref="category/java/bestpractices.xml/UnusedLocalVariable" />
    <rule ref="category/java/bestpractices.xml/UnusedPrivateField" />
    <rule ref="category/java/bestpractices.xml/UnusedPrivateMethod" />
    <rule ref="category/java/bestpractices.xml/AbstractClassWithoutAbstractMethod" />
    <rule ref="category/java/bestpractices.xml/ArrayIsStoredDirectly">
        <properties>
            <property name="allowPrivate" value="true" />
        </properties>
    </rule>
    <rule ref="category/java/bestpractices.xml/AvoidMessageDigestField" />
    <rule ref="category/java/bestpractices.xml/AvoidPrintStackTrace" />
    <rule ref="category/java/bestpractices.xml/AvoidReassigningCatchVariables" />
    <rule ref="category/java/bestpractices.xml/AvoidReassigningLoopVariables" />
    <rule ref="category/java/bestpractices.xml/AvoidReassigningParameters" />
    <rule ref="category/java/bestpractices.xml/AvoidStringBufferField" />
    <rule ref="category/java/bestpractices.xml/AvoidUsingHardCodedIP" />
    <rule ref="category/java/bestpractices.xml/ConstantsInInterface" />
    <rule ref="category/java/bestpractices.xml/DefaultLabelNotLastInSwitchStmt" />
    <rule ref="category/java/bestpractices.xml/DoubleBraceInitialization" />
    <rule ref="category/java/bestpractices.xml/ForLoopCanBeForeach" />
    <rule ref="category/java/bestpractices.xml/ForLoopVariableCount" />
    <rule ref="category/java/bestpractices.xml/GuardLogStatement" />
    <rule ref="category/java/bestpractices.xml/JUnit4SuitesShouldUseSuiteAnnotation" />
    <rule ref="category/java/bestpractices.xml/JUnit4TestShouldUseAfterAnnotation" />
    <rule ref="category/java/bestpractices.xml/JUnit4TestShouldUseBeforeAnnotation" />
    <rule ref="category/java/bestpractices.xml/JUnit4TestShouldUseTestAnnotation" />
    <rule ref="category/java/bestpractices.xml/JUnit5TestShouldBePackagePrivate" />
    <rule ref="category/java/bestpractices.xml/JUnitAssertionsShouldIncludeMessage" />
    <rule ref="category/java/bestpractices.xml/JUnitTestContainsTooManyAsserts" />
    <rule ref="category/java/bestpractices.xml/JUnitTestsShouldIncludeAssert" />
    <rule ref="category/java/bestpractices.xml/JUnitUseExpected" />
    <rule ref="category/java/bestpractices.xml/LiteralsFirstInComparisons" />
    <rule ref="category/java/bestpractices.xml/LooseCoupling" />
    <rule ref="category/java/bestpractices.xml/MethodReturnsInternalArray" />
    <rule ref="category/java/bestpractices.xml/MissingOverride" />
    <rule ref="category/java/bestpractices.xml/OneDeclarationPerLine" />
    <rule ref="category/java/bestpractices.xml/PreserveStackTrace" />
    <rule ref="category/java/bestpractices.xml/ReplaceEnumerationWithIterator" />
    <rule ref="category/java/bestpractices.xml/ReplaceHashtableWithMap" />
    <rule ref="category/java/bestpractices.xml/ReplaceVectorWithList" />
    <rule ref="category/java/bestpractices.xml/SimplifiableTestAssertion" />
    <rule ref="category/java/bestpractices.xml/SwitchStmtsShouldHaveDefault" />
    <rule ref="category/java/bestpractices.xml/SystemPrintln" />
    <rule ref="category/java/bestpractices.xml/UnusedAssignment" />
    <rule ref="category/java/bestpractices.xml/UseCollectionIsEmpty" />
    <rule ref="category/java/bestpractices.xml/UseStandardCharsets" />
    <rule ref="category/java/bestpractices.xml/UseTryWithResources" />
    <rule ref="category/java/bestpractices.xml/UseVarargs" />
    <rule ref="category/java/bestpractices.xml/WhileLoopWithLiteralBoolean" />

    <rule ref="category/java/codestyle.xml/EmptyControlStatement" />
    <rule ref="category/java/codestyle.xml/ExtendsObject" />
    <rule ref="category/java/codestyle.xml/ForLoopShouldBeWhileLoop" />
    <rule ref="category/java/codestyle.xml/TooManyStaticImports" />
    <rule ref="category/java/codestyle.xml/UnnecessaryFullyQualifiedName" />
    <rule ref="category/java/codestyle.xml/UnnecessaryImport" />
    <rule ref="category/java/codestyle.xml/UnnecessaryModifier" />
    <rule ref="category/java/codestyle.xml/UnnecessaryReturn" />
    <rule ref="category/java/codestyle.xml/UnnecessarySemicolon" />
    <rule ref="category/java/codestyle.xml/UselessParentheses" />
    <rule ref="category/java/codestyle.xml/UselessQualifiedThis" />
    <rule ref="category/java/codestyle.xml/AtLeastOneConstructor" />
    <rule ref="category/java/codestyle.xml/AvoidDollarSigns" />
    <rule ref="category/java/codestyle.xml/AvoidProtectedFieldInFinalClass" />
    <rule ref="category/java/codestyle.xml/AvoidProtectedMethodInFinalClassNotExtending" />
    <rule ref="category/java/codestyle.xml/AvoidUsingNativeCode" />
    <rule ref="category/java/codestyle.xml/BooleanGetMethodName" />
    <rule ref="category/java/codestyle.xml/CallSuperInConstructor" />
    <rule ref="category/java/codestyle.xml/ClassNamingConventions" />
    <rule ref="category/java/codestyle.xml/CommentDefaultAccessModifier" />
    <rule ref="category/java/codestyle.xml/ConfusingTernary" />
    <rule ref="category/java/codestyle.xml/ControlStatementBraces" />
    <rule ref="category/java/codestyle.xml/EmptyMethodInAbstractClassShouldBeAbstract" />
    <rule ref="category/java/codestyle.xml/FieldDeclarationsShouldBeAtStartOfClass" />
    <rule ref="category/java/codestyle.xml/FieldNamingConventions" />
    <rule ref="category/java/codestyle.xml/FinalParameterInAbstractMethod" />
    <rule ref="category/java/codestyle.xml/FormalParameterNamingConventions" />
    <rule ref="category/java/codestyle.xml/GenericsNaming" />
    <rule ref="category/java/codestyle.xml/IdenticalCatchBranches" />
    <rule ref="category/java/codestyle.xml/LinguisticNaming" />
    <rule ref="category/java/codestyle.xml/LocalHomeNamingConvention" />
    <rule ref="category/java/codestyle.xml/LocalInterfaceSessionNamingConvention" />
    <rule ref="category/java/codestyle.xml/LocalVariableCouldBeFinal" />
    <rule ref="category/java/codestyle.xml/LocalVariableNamingConventions" />
    <rule ref="category/java/codestyle.xml/LongVariable" />
    <rule ref="category/java/codestyle.xml/MDBAndSessionBeanNamingConvention" />
    <rule ref="category/java/codestyle.xml/MethodArgumentCouldBeFinal" />
    <rule ref="category/java/codestyle.xml/MethodNamingConventions" />
    <rule ref="category/java/codestyle.xml/NoPackage" />
    <rule ref="category/java/codestyle.xml/OnlyOneReturn" />
    <rule ref="category/java/codestyle.xml/PackageCase" />
    <rule ref="category/java/codestyle.xml/PrematureDeclaration" />
    <rule ref="category/java/codestyle.xml/RemoteInterfaceNamingConvention" />
    <rule ref="category/java/codestyle.xml/RemoteSessionInterfaceNamingConvention" />
    <rule ref="category/java/codestyle.xml/ShortClassName" />
    <rule ref="category/java/codestyle.xml/ShortMethodName" />
    <rule ref="category/java/codestyle.xml/ShortVariable" />
    <rule ref="category/java/codestyle.xml/UnnecessaryAnnotationValueElement" />
    <rule ref="category/java/codestyle.xml/UnnecessaryCast" />
    <rule ref="category/java/codestyle.xml/UnnecessaryConstructor" />
    <rule ref="category/java/codestyle.xml/UnnecessaryLocalBeforeReturn" />
    <rule ref="category/java/codestyle.xml/UseDiamondOperator" />
    <rule ref="category/java/codestyle.xml/UseShortArrayInitializer" />
    <rule ref="category/java/codestyle.xml/UseUnderscoresInNumericLiterals" />

    <rule ref="category/java/design.xml/CollapsibleIfStatements" />
    <rule ref="category/java/design.xml/SimplifiedTernary" />
    <rule ref="category/java/design.xml/UselessOverridingMethod" />
    <rule ref="category/java/design.xml/AbstractClassWithoutAnyMethod" />
    <rule ref="category/java/design.xml/AvoidCatchingGenericException" />
    <rule ref="category/java/design.xml/AvoidDeeplyNestedIfStmts" />
    <rule ref="category/java/design.xml/AvoidRethrowingException" />
    <rule ref="category/java/design.xml/AvoidThrowingNewInstanceOfSameException" />
    <rule ref="category/java/design.xml/AvoidThrowingNullPointerException" />
    <rule ref="category/java/design.xml/AvoidThrowingRawExceptionTypes" />
    <rule ref="category/java/design.xml/AvoidUncheckedExceptionsInSignatures" />
    <rule ref="category/java/design.xml/ClassWithOnlyPrivateConstructorsShouldBeFinal" />
    <rule ref="category/java/design.xml/CognitiveComplexity" />
    <rule ref="category/java/design.xml/CouplingBetweenObjects" />
    <rule ref="category/java/design.xml/CyclomaticComplexity" />
    <rule ref="category/java/design.xml/DataClass" />
    <rule ref="category/java/design.xml/DoNotExtendJavaLangError" />
    <rule ref="category/java/design.xml/ExceptionAsFlowControl" />
    <rule ref="category/java/design.xml/ExcessiveImports" />
    <rule ref="category/java/design.xml/ExcessiveParameterList" />
    <rule ref="category/java/design.xml/ExcessivePublicCount" />
    <rule ref="category/java/design.xml/FinalFieldCouldBeStatic" />
    <rule ref="category/java/design.xml/GodClass" />
    <rule ref="category/java/design.xml/ImmutableField" />
    <rule ref="category/java/design.xml/InvalidJavaBean" />
    <rule ref="category/java/design.xml/LawOfDemeter" />
    <rule ref="category/java/design.xml/LogicInversion" />
    <rule ref="category/java/design.xml/MutableStaticState" />
    <rule ref="category/java/design.xml/NcssCount" />
    <rule ref="category/java/design.xml/NPathComplexity" />
    <rule ref="category/java/design.xml/SignatureDeclareThrowsException" />
    <rule ref="category/java/design.xml/SimplifyBooleanExpressions" />
    <rule ref="category/java/design.xml/SimplifyBooleanReturns" />
    <rule ref="category/java/design.xml/SimplifyConditional" />
    <rule ref="category/java/design.xml/SingularField" />
    <rule ref="category/java/design.xml/SwitchDensity" />
    <rule ref="category/java/design.xml/TooManyFields" />
    <rule ref="category/java/design.xml/TooManyMethods" />
    <rule ref="category/java/design.xml/UseObjectForClearerAPI" />
    <rule ref="category/java/design.xml/UseUtilityClass" />

    <rule ref="category/java/documentation.xml/CommentContent" />
    <rule ref="category/java/documentation.xml/CommentRequired" />
    <rule ref="category/java/documentation.xml/CommentSize" />
    <rule ref="category/java/documentation.xml/UncommentedEmptyConstructor" />
    <rule ref="category/java/documentation.xml/UncommentedEmptyMethodBody" />

    <rule ref="category/java/errorprone.xml/AvoidBranchingStatementAsLastInLoop" />
    <rule ref="category/java/errorprone.xml/AvoidDecimalLiteralsInBigDecimalConstructor" />
    <rule ref="category/java/errorprone.xml/AvoidMultipleUnaryOperators" />
    <rule ref="category/java/errorprone.xml/AvoidUsingOctalValues" />
    <rule ref="category/java/errorprone.xml/BrokenNullCheck" />
    <rule ref="category/java/errorprone.xml/CheckSkipResult" />
    <rule ref="category/java/errorprone.xml/ClassCastExceptionWithToArray" />
    <rule ref="category/java/errorprone.xml/DontUseFloatTypeForLoopIndices" />
    <rule ref="category/java/errorprone.xml/EmptyCatchBlock" />
    <rule ref="category/java/errorprone.xml/JumbledIncrementer" />
    <rule ref="category/java/errorprone.xml/MisplacedNullCheck" />
    <rule ref="category/java/errorprone.xml/OverrideBothEqualsAndHashcode" />
    <rule ref="category/java/errorprone.xml/ReturnFromFinallyBlock" />
    <rule ref="category/java/errorprone.xml/UnconditionalIfStatement" />
    <rule ref="category/java/errorprone.xml/UnnecessaryConversionTemporary" />
    <rule ref="category/java/errorprone.xml/UnusedNullCheckInEquals" />
    <rule ref="category/java/errorprone.xml/UselessOperationOnImmutable" />
    <rule ref="category/java/errorprone.xml/AssignmentInOperand" />
    <rule ref="category/java/errorprone.xml/AssignmentToNonFinalStatic" />
    <rule ref="category/java/errorprone.xml/AvoidAccessibilityAlteration" />
    <rule ref="category/java/errorprone.xml/AvoidAssertAsIdentifier" />
    <rule ref="category/java/errorprone.xml/AvoidCallingFinalize" />
    <rule ref="category/java/errorprone.xml/AvoidCatchingNPE" />
    <rule ref="category/java/errorprone.xml/AvoidCatchingThrowable" />
    <rule ref="category/java/errorprone.xml/AvoidDuplicateLiterals" />
    <rule ref="category/java/errorprone.xml/AvoidEnumAsIdentifier" />
    <rule ref="category/java/errorprone.xml/AvoidFieldNameMatchingMethodName" />
    <rule ref="category/java/errorprone.xml/AvoidFieldNameMatchingTypeName" />
    <rule ref="category/java/errorprone.xml/AvoidInstanceofChecksInCatchClause" />
    <rule ref="category/java/errorprone.xml/AvoidLiteralsInIfCondition" />
    <rule ref="category/java/errorprone.xml/AvoidLosingExceptionInformation" />
    <rule ref="category/java/errorprone.xml/CallSuperFirst" />
    <rule ref="category/java/errorprone.xml/CallSuperLast" />
    <rule ref="category/java/errorprone.xml/CloneMethodMustBePublic" />
    <rule ref="category/java/errorprone.xml/CloneMethodMustImplementCloneable" />
    <rule ref="category/java/errorprone.xml/CloneMethodReturnTypeMustMatchClassName" />
    <rule ref="category/java/errorprone.xml/CloseResource" />
    <rule ref="category/java/errorprone.xml/CompareObjectsWithEquals" />
    <rule ref="category/java/errorprone.xml/ComparisonWithNaN" />
    <rule ref="category/java/errorprone.xml/ConstructorCallsOverridableMethod" />
    <rule ref="category/java/errorprone.xml/DetachedTestCase" />
    <rule ref="category/java/errorprone.xml/DoNotCallGarbageCollectionExplicitly" />
    <rule ref="category/java/errorprone.xml/DoNotExtendJavaLangThrowable" />
    <rule ref="category/java/errorprone.xml/DoNotHardCodeSDCard" />
    <rule ref="category/java/errorprone.xml/DoNotTerminateVM" />
    <rule ref="category/java/errorprone.xml/DoNotThrowExceptionInFinally" />
    <rule ref="category/java/errorprone.xml/DontImportSun" />
    <rule ref="category/java/errorprone.xml/EmptyFinalizer" />
    <rule ref="category/java/errorprone.xml/EqualsNull" />
    <rule ref="category/java/errorprone.xml/FinalizeDoesNotCallSuperFinalize" />
    <rule ref="category/java/errorprone.xml/FinalizeOnlyCallsSuperFinalize" />
    <rule ref="category/java/errorprone.xml/FinalizeOverloaded" />
    <rule ref="category/java/errorprone.xml/FinalizeShouldBeProtected" />
    <rule ref="category/java/errorprone.xml/IdempotentOperations" />
    <rule ref="category/java/errorprone.xml/ImplicitSwitchFallThrough" />
    <rule ref="category/java/errorprone.xml/InstantiationToGetClass" />
    <rule ref="category/java/errorprone.xml/InvalidLogMessageFormat" />
    <rule ref="category/java/errorprone.xml/JUnitSpelling" />
    <rule ref="category/java/errorprone.xml/JUnitStaticSuite" />
    <rule ref="category/java/errorprone.xml/MethodWithSameNameAsEnclosingClass" />
    <rule ref="category/java/errorprone.xml/MissingSerialVersionUID" />
    <rule ref="category/java/errorprone.xml/MissingStaticMethodInNonInstantiatableClass" />
    <rule ref="category/java/errorprone.xml/MoreThanOneLogger" />
    <rule ref="category/java/errorprone.xml/NonCaseLabelInSwitchStatement" />
    <rule ref="category/java/errorprone.xml/NonSerializableClass" />
    <rule ref="category/java/errorprone.xml/NonStaticInitializer" />
    <rule ref="category/java/errorprone.xml/NullAssignment" />
    <rule ref="category/java/errorprone.xml/ProperCloneImplementation" />
    <rule ref="category/java/errorprone.xml/ProperLogger" />
    <rule ref="category/java/errorprone.xml/ReturnEmptyCollectionRatherThanNull" />
    <rule ref="category/java/errorprone.xml/SimpleDateFormatNeedsLocale" />
    <rule ref="category/java/errorprone.xml/SingleMethodSingleton" />
    <rule ref="category/java/errorprone.xml/SingletonClassReturningNewInstance" />
    <rule ref="category/java/errorprone.xml/StaticEJBFieldShouldBeFinal" />
    <rule ref="category/java/errorprone.xml/StringBufferInstantiationWithChar" />
    <rule ref="category/java/errorprone.xml/SuspiciousEqualsMethodName" />
    <rule ref="category/java/errorprone.xml/SuspiciousHashcodeMethodName" />
    <rule ref="category/java/errorprone.xml/SuspiciousOctalEscape" />
    <rule ref="category/java/errorprone.xml/TestClassWithoutTestCases" />
    <rule ref="category/java/errorprone.xml/UnnecessaryBooleanAssertion" />
    <rule ref="category/java/errorprone.xml/UnnecessaryCaseChange" />
    <rule ref="category/java/errorprone.xml/UseCorrectExceptionLogging" />
    <rule ref="category/java/errorprone.xml/UseEqualsToCompareStrings" />
    <rule ref="category/java/errorprone.xml/UseLocaleWithCaseConversions" />
    <rule ref="category/java/errorprone.xml/UseProperClassLoader" />

    <rule ref="category/java/multithreading.xml/AvoidThreadGroup" />
    <rule ref="category/java/multithreading.xml/DontCallThreadRun" />
    <rule ref="category/java/multithreading.xml/DoubleCheckedLocking" />
    <rule ref="category/java/multithreading.xml/AvoidSynchronizedAtMethodLevel" />
    <rule ref="category/java/multithreading.xml/AvoidUsingVolatile" />
    <rule ref="category/java/multithreading.xml/DoNotUseThreads" />
    <rule ref="category/java/multithreading.xml/NonThreadSafeSingleton" />
    <rule ref="category/java/multithreading.xml/UnsynchronizedStaticFormatter" />
    <rule ref="category/java/multithreading.xml/UseConcurrentHashMap" />
    <rule ref="category/java/multithreading.xml/UseNotifyAllInsteadOfNotify" />

    <rule ref="category/java/performance.xml/BigIntegerInstantiation" />
    <rule ref="category/java/performance.xml/AddEmptyString" />
    <rule ref="category/java/performance.xml/AppendCharacterWithChar" />
    <rule ref="category/java/performance.xml/AvoidArrayLoops" />
    <rule ref="category/java/performance.xml/AvoidCalendarDateCreation" />
    <rule ref="category/java/performance.xml/AvoidFileStream" />
    <rule ref="category/java/performance.xml/AvoidInstantiatingObjectsInLoops" />
    <rule ref="category/java/performance.xml/ConsecutiveAppendsShouldReuse" />
    <rule ref="category/java/performance.xml/ConsecutiveLiteralAppends" />
    <rule ref="category/java/performance.xml/InefficientEmptyStringCheck" />
    <rule ref="category/java/performance.xml/InefficientStringBuffering" />
    <rule ref="category/java/performance.xml/InsufficientStringBufferDeclaration" />
    <rule ref="category/java/performance.xml/OptimizableToArrayCall" />
    <rule ref="category/java/performance.xml/RedundantFieldInitializer" />
    <rule ref="category/java/performance.xml/StringInstantiation" />
    <rule ref="category/java/performance.xml/StringToString" />
    <rule ref="category/java/performance.xml/TooFewBranchesForASwitchStatement" />
    <rule ref="category/java/performance.xml/UseArrayListInsteadOfVector" />
    <rule ref="category/java/performance.xml/UseArraysAsList" />
    <rule ref="category/java/performance.xml/UseIndexOfChar" />
    <rule ref="category/java/performance.xml/UseIOStreamsWithApacheCommonsFileItem" />
    <rule ref="category/java/performance.xml/UselessStringValueOf" />
    <rule ref="category/java/performance.xml/UseStringBufferForStringAppends" />
    <rule ref="category/java/performance.xml/UseStringBufferLength" />

    <rule ref="category/java/security.xml/HardCodedCryptoKey" />
    <rule ref="category/java/security.xml/InsecureCryptoIv" />

</ruleset>
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值