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

自定义PMD检测的类型集合(详解)自定义 PMD 检测的类型集合 PMD 所能检测的类型 八大种 使用方法使用 xml 配置文件配置多条规则 1 在 resources 目录下写个配置文件 settings xml 命名无要求 2 configuratio setRuleSets settings xml 引用规则集使用 setRuleSets 配置多条规则 PMD 所能检测的类型 八大种 官网链接 点这里大概共有 319 条小规则 不算舍弃的规则 源码包中检测 Java 的规则所在的位置 pmd src 6 32 0 pmd java src main

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

官网链接:点这里

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

源码包中检测Java的规则所在的位置:pmd-src-6.32.0\pmd-java\src\main\resources\category\java

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.
    • 标记潜在安全漏洞的规则。

还有一个快速检测的规则集的源码所在的位置:pmd-src-6.32.0\pmd-java\src\main\resources\rulesets\java

rulesets/java/quickstart.xml

  • Additional rulesets:附加规则集

    quickstart (rulesets/java/quickstart.xml)

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

使用方法

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

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

在这里插入图片描述
settings.xml:

 
    <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>  //VariableDeclaratorId[@Image = "keafmd" and ../../Type[@TypeImage = "String"]]   
          value>  
           property>  
            properties>  
             rule>  
              
              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检测的类型集合(详解)的全部内容。

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


在这里插入图片描述

加油!

共同努力!

Keafmd

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/208308.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月19日 上午11:53
下一篇 2026年3月19日 上午11:53


相关推荐

发表回复

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

关注全栈程序员社区公众号