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

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:

<?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检测的类型集合(详解)的全部内容。

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

在这里插入图片描述

加油!

共同努力!

Keafmd

评论 29
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

牛哄哄的柯南

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值