自定义PMD检测的类型集合(详解)

PMD所能检测的类型(八大种)

官网链接:点这里

大概共有319条小规则(不算舍弃的规则)

源码包中检测Java的规则所在的位置:pmd-src-6.32.0pmd-javasrcmainresourcescategoryjava

category/java/XXX.xml/xxxxx

  • Best Practices:Rules which enforce generally accepted best practices.

    • 执行普遍接受的最佳做法的规则。
  • Code Style:Rules which enforce a specific coding style.

    • 执行特定编码样式的规则。
  • Design:Rules that help you discover design issues.

    • 帮助您发现设计问题的规则。
  • Documentation:Rules that are related to code documentation.

    • 与代码文档相关的规则。
  • Error Prone:Rules to detect constructs that are either broken, extremely confusing or prone to runtime errors.

    • 用于检测结构的规则,这些构造要么被破坏,要么非常混乱,或者容易出现运行时错误。
  • Multithreading:Rules that flag issues when dealing with multiple threads of execution.

    • 在处理多个执行线程时标记问题的规则。
  • Performance:Rules that flag suboptimal code.

    • 标记次优代码的规则。
  • Security:Rules that flag potential security flaws.

    • 标记潜在Additional rulesets:附加规则集

      quickstart (rulesets/java/quickstart.xml)

      PMD的快速启动配置。包括最有可能适用于任何地方的规则。

    使用方法

    使用xml配置文件配置多条规则

    1、在resources目录下写个配置文件 settings.xml(命名无要求)

    自定义PMD检测的类型集合(详解)
    settings.xml:

    <?xml version="1.0"?> <ruleset name="myruleset"          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 https://pmd.sourceforge.io/ruleset_2_0_0.xsd">     <description>My ruleset</description>      <rule ref="category/java/codestyle.xml">         <exclude name="WhileLoopsMustUseBraces"/>         <exclude name="IfElseStmtsMustUseBraces"/>     </rule>     <rule ref="category/java/performance.xml"></rule>     <rule ref="category/java/bestpractices.xml"/>     <!--引用自定义规则-->     <rule name="myrule"           language="java"           message="不能有变量为keafmd的String类型的变量"           class="net.sourceforge.pmd.lang.rule.XPathRule">         <description>             自定义规则         </description>         <priority>3</priority>         <properties>             <property name="version" value="2.0"/>             <property name="xpath">                 <value>                     <![CDATA[ //VariableDeclaratorId[@Image = "keafmd" and ../../Type[@TypeImage = "String"]] ]]>                 </value>             </property>         </properties>     </rule>       <!-- Rules here ... -->  </ruleset> 

    2、configuration.setRuleSets(“settings.xml”); 引用规则集

    package com.keafmd;  import net.sourceforge.pmd.PMD; import net.sourceforge.pmd.PMDConfiguration;  import java.util.UUID;   /**  * Keafmd  *  * @ClassName: PmdExample  * @Description:  * @author: 牛哄哄的柯南  * @Date: 2021-03-22 15:01  * @Blog: https://keafmd.blog.csdn.net/  */ public class PmdExample {     public static void main(String[] args) {         /*PMDConfiguration configuration = new PMDConfiguration();         configuration.setInputPaths("D:/javaworkspace/pdm-test04/src/main/java/com/keafmd/test01");         configuration.setRuleSets("rulesets/java/quickstart.xml");         configuration.setReportFormat("html");         configuration.setReportFile("D:/pmdreport/pmd-report.html");          PMD.doPMD(configuration);*/          String fileName ;         // 重新生成文件名(根据具体情况生成对应文件名)         fileName = UUID.randomUUID()+"_"+"pmd-report.html";          PMDConfiguration configuration = new PMDConfiguration();         configuration.setInputPaths("D:\javaworkspace\pmd-test04\src\main\java\com\keafmd\test01\Test01.java");         //configuration.setRuleSets("rulesets/java/quickstart.xml");         //configuration.setRuleSets("src/main/java/com/keafmd/pmd/myrule.xml");         configuration.setRuleSets("settings.xml");         configuration.setReportFormat("html");         configuration.setReportFile("D:/pmdreport/"+fileName);          PMD.doPMD(configuration);     }  } 

    使用setRuleSets配置多条规则

    例如:

    configuration.setRuleSets("category/java/bestpractices.xml"); configuration.setRuleSets("category/java/codestyle.xml"); 

    完整代码:

    package com.keafmd;  import net.sourceforge.pmd.PMD; import net.sourceforge.pmd.PMDConfiguration;  import java.util.UUID;   /**  * Keafmd  *  * @ClassName: PmdExample  * @Description:  * @author: 牛哄哄的柯南  * @Date: 2021-03-22 15:01  * @Blog: https://keafmd.blog.csdn.net/  */ public class PmdExample {     public static void main(String[] args) {         /*PMDConfiguration configuration = new PMDConfiguration();         configuration.setInputPaths("D:/javaworkspace/pdm-test04/src/main/java/com/keafmd/test01");         configuration.setRuleSets("rulesets/java/quickstart.xml");         configuration.setReportFormat("html");         configuration.setReportFile("D:/pmdreport/pmd-report.html");          PMD.doPMD(configuration);*/          String fileName ;         // 重新生成文件名(根据具体情况生成对应文件名)         fileName = UUID.randomUUID()+"_"+"pmd-report.html";          PMDConfiguration configuration = new PMDConfiguration();         configuration.setInputPaths("D:\javaworkspace\pmd-test04\src\main\java\com\keafmd\test01\Test01.java");         //configuration.setRuleSets("rulesets/java/quickstart.xml");         //configuration.setRuleSets("src/main/java/com/keafmd/pmd/myrule.xml");         //configuration.setRuleSets("settings.xml");         configuration.setRuleSets("category/java/bestpractices.xml");         configuration.setRuleSets("category/java/codestyle.xml");         configuration.setReportFormat("html");         configuration.setReportFile("D:/pmdreport/"+fileName);          PMD.doPMD(configuration);     }  } 

    以上就是自定义PMD检测的类型集合(详解)的全部内容。

    看完如果对你有帮助,感谢点赞支持!
    如果你是电脑端的话,看到右下角的 “一键三连” 了吗,没错点它[哈哈]

    自定义PMD检测的类型集合(详解)

    加油!

    共同努力!

    Keafmd

版权声明:玥玥 发表于 2021-04-03 10:41:21。
转载请注明:自定义PMD检测的类型集合(详解) | 女黑客导航