Java 代碼檢查工具之PMD入門使用詳細教程
介紹
PMD是一個靜態(tài)源代碼分析器。它發(fā)現(xiàn)了常見的編程缺陷,如未使用的變量、空捕獲塊、不必要的對象創(chuàng)建等等。
使用方式
1、使用插件的方式
下載:File -> Settings -> Plugins -> Marketplace 搜索 “PMDPlugin” ,下載插件。
使用方法:在代碼編輯框或Project 窗口的文件夾、包、文件右鍵,選擇“Run PMD”->“Pre Defined”->“All”,對指定的文件夾、包、文件進行分析,分析結(jié)果在控制臺輸出。
2、maven項目引入依賴的方式
pom.xml:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.keafmd</groupId> <artifactId>pdm-test01</artifactId> <version>1.0-SNAPSHOT</version> <!--<dependencies> <dependency> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-pmd-plugin</artifactId> <version>3.14.0</version> <type>maven-plugin</type> </dependency> </dependencies>--> <!-- 用于生成錯誤到代碼內(nèi)容的鏈接 --> <reporting> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-pmd-plugin</artifactId> <version>3.14.0</version> </plugin> </plugins> </reporting> </project>
mvn 命令執(zhí)行
在項目目錄打開cmd窗口,輸入以下命令:
mvn pmd:pmd

分析結(jié)果為pmd.html文件,在項目的target下的site目錄下:


分析結(jié)果顯示內(nèi)容:

3、pmd 命令行的方式
pmd -d 源代碼路徑 -f xml(結(jié)果輸出格式) -r 結(jié)果保存所在目錄及名稱 -R rulesets/java/unusedcode.xml
例子:

結(jié)果存放在制定文件目錄下,格式也為命令語句指定的:

檢測結(jié)果內(nèi)容:

4、Java API的方式 *
需要先引入maven依賴
項目結(jié)構(gòu)

測試代碼
Test01:
package com.keafmd.test01;
/**
* Keafmd
*
* @ClassName: Test01
* @Description: 測試1
* @author: 牛哄哄的柯南
* @Date: 2021-03-15 15:29
* @Blog: https://keafmd.blog.csdn.net/
*/
public class Test01 {
public static void main(String[] args) {
int a =100;
int b=29;
String s ="abc";
System.out.println("hello!");
}
}
Test02:
package com.keafmd.test02;
/**
* Keafmd
*
* @ClassName: Test02
* @Description:
* @author: 牛哄哄的柯南
* @Date: 2021-03-15 15:30
* @Blog: https://keafmd.blog.csdn.net/
*/
public class Test02 {
public static void main(String[] args) {
boolean flag=true;
while(flag){
flag=false;
}
System.out.println("123");
int a =100;
int b=29;
String s ="abc";
System.out.println("hello!");
}
}
pmdArgs方式
命令行接口的方式
最簡單的方法是使用與命令行相同的接口調(diào)用PMD
Example :
package com.keafmd;
import net.sourceforge.pmd.PMD;
/**
* Keafmd
*
* @ClassName: Example
* @Description:
* @author: 牛哄哄的柯南
* @Date: 2021-03-15 15:51
* @Blog: https://keafmd.blog.csdn.net/
*/
public class Example {
public static void main(String[] args) {
String[] pmdArgs = {
"-d", "D:/javaworkspace/pdm-test02/src",
"-R", "rulesets/java/quickstart.xml",
"-f", "xml",
"-r", "D:/pmdreport/pmd-report.xml"
};
PMD.main(pmdArgs);
}
}
PMDConfiguration方式
PmdExample:
package com.keafmd;
import net.sourceforge.pmd.PMD;
import net.sourceforge.pmd.PMDConfiguration;
/**
* Keafmd
*
* @ClassName: PmdExample
* @Description:
* @author: 牛哄哄的柯南
* @Date: 2021-03-15 15:57
* @Blog: https://keafmd.blog.csdn.net/
*/
public class PmdExample {
public static void main(String[] args) {
PMDConfiguration configuration = new PMDConfiguration();
configuration.setInputPaths("D:/javaworkspace/pdm-test/src");
configuration.setRuleSets("rulesets/java/quickstart.xml");
configuration.setReportFormat("html");
configuration.setReportFile("D:/pmdreport/pmd-report.html");
PMD.doPMD(configuration);
}
}
Programmatically(拓展)
這使您能夠更好地控制處理哪些文件,但也會更加復雜。您還可以提供自己的偵聽器和呈現(xiàn)器。
1. 首先,我們創(chuàng)建一個PMDConfiguration。目前,這是指定規(guī)則集的唯一方法:
PMDConfiguration configuration = new PMDConfiguration();
configuration.setMinimumPriority(RulePriority.MEDIUM);
configuration.setRuleSets("rulesets/java/quickstart.xml");
2. 為了支持類型解析,PMD還需要訪問已編譯的類和依賴項。這被稱為“生長素路徑”,并且在這里也進行了配置。注意:您可以指定由:關(guān)于Unix系統(tǒng)或;在Windows下。
configuration.prependClasspath("/home/workspace/target/classes:/home/.m2/repository/my/dependency.jar");
3. 那我們需要一個規(guī)則工廠。這是使用配置創(chuàng)建的,同時考慮到最低優(yōu)先級:
RuleSetFactory ruleSetFactory = RulesetsFactoryUtils.createFactory(configuration);
4. PMD操作于DataSource。您可以收集自己的列表FileDataSource.
List<DataSource> files = Arrays.asList(new FileDataSource(new File("/path/to/src/MyClass.java")));
5. 對于報告,您可以使用內(nèi)置渲染器。XMLRenderer。注意,必須通過設(shè)置適當?shù)?code>Writer打電話start()。在pmd運行之后,您需要調(diào)用end()和flush()。那么你的作者應(yīng)該收到所有的輸出。
StringWriter rendererOutput = new StringWriter();
Renderer xmlRenderer = new XMLRenderer("UTF-8");
xmlRenderer.setWriter(rendererOutput);
xmlRenderer.start();
6. 創(chuàng)建一個RuleContext。這是上下文實例,在規(guī)則實現(xiàn)中是可用的。注意:當在多線程模式下運行時(這是默認的),規(guī)則上下文實例將被克隆到每個線程。
RuleContext ctx = new RuleContext();
7. 可以選擇注冊報表偵聽器。這樣你就可以對發(fā)現(xiàn)的違規(guī)行為立即做出反應(yīng)。您也可以使用這樣的偵聽器來實現(xiàn)您自己的呈現(xiàn)器。偵聽器必須實現(xiàn)接口。ThreadSafeReportListener并且可以通過ctx.getReport().addListener(...).
ctx.getReport().addListener(new ThreadSafeReportListener() {
public void ruleViolationAdded(RuleViolation ruleViolation) {
}
public void metricAdded(Metric metric) {
}
8. 現(xiàn)在,所有的準備工作都完成了,PMD可以執(zhí)行了。這是通過調(diào)用PMD.processFiles(...)。此方法調(diào)用接受配置、規(guī)則集工廠、要處理的文件、規(guī)則上下文和呈現(xiàn)器列表。如果不想使用任何渲染器,請?zhí)峁┮粋€空列表。注意:需要顯式關(guān)閉輔助路徑。否則,類或JAR文件可能會保持打開狀態(tài),并且文件資源會泄漏。
try {
PMD.processFiles(configuration, ruleSetFactory, files, ctx,
Collections.singletonList(renderer));
} finally {
ClassLoader auxiliaryClassLoader = configuration.getClassLoader();
if (auxiliaryClassLoader instanceof ClasspathClassLoader) {
((ClasspathClassLoader) auxiliaryClassLoader).close();
}
}
9. 呼叫后,您需要完成渲染器end()和flush()。然后,您可以檢查呈現(xiàn)的輸出。
renderer.end();
renderer.flush();
System.out.println("Rendered Report:");
System.out.println(rendererOutput.toString());
下面是一個完整的例子:
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.nio.file.FileSystems;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import net.sourceforge.pmd.PMD;
import net.sourceforge.pmd.PMDConfiguration;
import net.sourceforge.pmd.RuleContext;
import net.sourceforge.pmd.RulePriority;
import net.sourceforge.pmd.RuleSetFactory;
import net.sourceforge.pmd.RuleViolation;
import net.sourceforge.pmd.RulesetsFactoryUtils;
import net.sourceforge.pmd.ThreadSafeReportListener;
import net.sourceforge.pmd.renderers.Renderer;
import net.sourceforge.pmd.renderers.XMLRenderer;
import net.sourceforge.pmd.stat.Metric;
import net.sourceforge.pmd.util.ClasspathClassLoader;
import net.sourceforge.pmd.util.datasource.DataSource;
import net.sourceforge.pmd.util.datasource.FileDataSource;
public class PmdExample2 {
public static void main(String[] args) throws IOException {
PMDConfiguration configuration = new PMDConfiguration();
configuration.setMinimumPriority(RulePriority.MEDIUM);
configuration.setRuleSets("rulesets/java/quickstart.xml");
configuration.prependClasspath("/home/workspace/target/classes");
RuleSetFactory ruleSetFactory = RulesetsFactoryUtils.createFactory(configuration);
List<DataSource> files = determineFiles("/home/workspace/src/main/java/code");
Writer rendererOutput = new StringWriter();
Renderer renderer = createRenderer(rendererOutput);
renderer.start();
RuleContext ctx = new RuleContext();
ctx.getReport().addListener(createReportListener()); // alternative way to collect violations
try {
PMD.processFiles(configuration, ruleSetFactory, files, ctx,
Collections.singletonList(renderer));
} finally {
ClassLoader auxiliaryClassLoader = configuration.getClassLoader();
if (auxiliaryClassLoader instanceof ClasspathClassLoader) {
((ClasspathClassLoader) auxiliaryClassLoader).close();
}
}
renderer.end();
renderer.flush();
System.out.println("Rendered Report:");
System.out.println(rendererOutput.toString());
}
private static ThreadSafeReportListener createReportListener() {
return new ThreadSafeReportListener() {
@Override
public void ruleViolationAdded(RuleViolation ruleViolation) {
System.out.printf("%-20s:%d %s%n", ruleViolation.getFilename(),
ruleViolation.getBeginLine(), ruleViolation.getDescription());
}
@Override
public void metricAdded(Metric metric) {
// ignored
}
};
}
private static Renderer createRenderer(Writer writer) {
XMLRenderer xml = new XMLRenderer("UTF-8");
xml.setWriter(writer);
return xml;
}
private static List<DataSource> determineFiles(String basePath) throws IOException {
Path dirPath = FileSystems.getDefault().getPath(basePath);
PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:*.java");
List<DataSource> files = new ArrayList<>();
Files.walkFileTree(dirPath, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException {
if (matcher.matches(path.getFileName())) {
System.out.printf("Using %s%n", path);
files.add(new FileDataSource(path.toFile()));
} else {
System.out.printf("Ignoring %s%n", path);
}
return super.visitFile(path, attrs);
}
});
System.out.printf("Analyzing %d files in %s%n", files.size(), basePath);
return files;
}
}
分析結(jié)果
分析結(jié)果會根據(jù)指定格式輸出在指定文件目錄下。
圖形界面
檢測
D:\MyFile\Tool\pmd-bin-6.32.0\bin 目錄下打開cmd窗口輸入:
cpdgui.bat

自定義規(guī)則

D:\MyFile\Tool\pmd-bin-6.32.0\bin 目錄下打開cmd窗口輸入:
designer.bat

自定義規(guī)則:不能有變量為keafmd的String類型的變量
String keafmd; //這樣就是不合法的。
Source:
public class KeepingItSerious {
Delegator keafmd; // FieldDeclaration
public void method() {
String keafmd; // LocalVariableDeclaration
}
}
導出的自定義規(guī)則:
<rule name="myrule" language="java" message="不能有變量為keafmd的String類型的變量" class="net.sourceforge.pmd.lang.rule.XPathRule"> <description> 自定義規(guī)則 </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>
到此這篇關(guān)于Java 代碼檢查工具之PMD入門使用詳細教程的文章就介紹到這了,更多相關(guān)Java 代碼檢查工具PMD內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決一個JSON反序列化問題的辦法(空字符串變?yōu)榭占?
在平時的業(yè)務(wù)開發(fā)中,經(jīng)常會有拿到一串序列化后的字符串要來反序列化,下面這篇文章主要給大家介紹了如何解決一個JSON反序列化問題的相關(guān)資料,空字符串變?yōu)榭占?需要的朋友可以參考下2024-03-03
基于Spring Cloud Zookeeper實現(xiàn)服務(wù)注冊與發(fā)現(xiàn)
這篇文章主要介紹了基于Spring Cloud Zookeeper實現(xiàn)服務(wù)注冊與發(fā)現(xiàn),幫助大家更好的理解和學習spring框架,感興趣的朋友可以了解下2020-11-11
springboot 實現(xiàn)記錄業(yè)務(wù)日志和異常業(yè)務(wù)日志的操作
這篇文章主要介紹了springboot 實現(xiàn)記錄業(yè)務(wù)日志和異常業(yè)務(wù)日志的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07
詳解Java去除json數(shù)據(jù)中的null空值問題
這篇文章主要介紹了詳解Java去除json數(shù)據(jù)中的null空值問題,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-08-08

