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

简介: 自定义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(命名无要求)


4.png

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


相关文章
|
Linux iOS开发 MacOS
CrossOver23中文永久免费版MacOS平台快速运行Windows软件
CrossOver是一款可以让Mac和Linux系统中正常运行Windows软件的应用程序。它不像虚拟机一样需要安装Windows系统之后才可以安装Windows的应用程序,这一方式给大多数用户带来了方便。通过CrossOver实现跨平台的文件复制粘贴,使Mac/Linux系统与Windows应用良好结合。CrossOver下载:http://t.csdn.cn/Ixdq6
5515 0
CrossOver23中文永久免费版MacOS平台快速运行Windows软件
|
前端开发
Vue3/React 动态设置 ant-design/icons 图标
Vue3/React 动态设置 ant-design/icons 图标
1098 1
|
Java Spring
Spring中事务失效的场景
因为Spring事务是基于代理来实现的,所以某个加了@Transactional的⽅法只有是被代理对象调⽤时, 那么这个注解才会⽣效 , 如果使用的是被代理对象调用, 那么@Transactional会失效 同时如果某个⽅法是private的,那么@Transactional也会失效,因为底层cglib是基于⽗⼦类来实现 的,⼦类是不能重载⽗类的private⽅法的,所以⽆法很好的利⽤代理,也会导致@Transactianal失效 如果在业务中对异常进行了捕获处理 , 出现异常后Spring框架无法感知到异常, @Transactional也会失效
|
8月前
|
人工智能 JavaScript 测试技术
当Playwright遇见MCP,AI智能体实现自主化UI回归测试
本文探讨如何通过Model Context Protocol(MCP)让AI智能体驱动Playwright实现端到端自动化测试。重点解析快照技术的实现原理与实战流程,同时深入剖析其在信息丢失、元素定位、成本效率及逻辑复杂性等方面的现实挑战。
|
存储 Unix Serverless
【C语言】常用函数汇总表
本文总结了C语言中常用的函数,涵盖输入/输出、字符串操作、内存管理、数学运算、时间处理、文件操作及布尔类型等多个方面。每类函数均以表格形式列出其功能和使用示例,便于快速查阅和学习。通过综合示例代码,展示了这些函数的实际应用,帮助读者更好地理解和掌握C语言的基本功能和标准库函数的使用方法。感谢阅读,希望对你有所帮助!
1698 8
|
缓存 IDE 开发工具
Arduino快速上手esp8266方案开发
Arduino快速上手esp8266方案开发
909 0
|
存储 移动开发 前端开发
HTML5 的新特性
【8月更文挑战第24天】
425 0
|
存储 消息中间件 数据采集
Flume 配置文件编写技巧(包会的,抄就完了)
本文介绍了Apache Flume的基础配置,包括数据源(Source)、数据通道(Channel)和数据处理器(Sink)三大部分。配置文件编写流程包括查阅官方文档、参考样例配置、实际操作配置。文章提供了一个经典例子,展示如何从本地端口收集数据并通过内存通道缓冲,最终记录到日志。配置流程包括声明组件、配置Source、Sink和Channel,然后将它们绑定。通过示例展示了如何配置HTTP Source和HDFS Sink,并给出了完整的配置文件示例及测试步骤,帮助读者理解Flume配置文件的编写。
1187 0
|
Java BI Maven
springboot maven项目集成阿里p3c-pmd插件使用
springboot maven项目集成阿里p3c-pmd插件使用

热门文章

最新文章