自定義PMD檢測的型別集合
- PMD所能檢測的型別(八大種)
- 使用方法
- 使用xml組態檔配置多條規則
- 1、在resources目錄下寫個組態檔 settings.xml(命名無要求)
- 2、configuration.setRuleSets("settings.xml"); 參考規則集
- 使用setRuleSets配置多條規則
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
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/271939.html
標籤:其他
上一篇:原來ReadWriteLock也能開發高性能快取,看完我也能和面試官好好聊聊了!
下一篇:面試篇
