您好,登錄后才能下訂單哦!
小編給大家分享一下Java代碼檢查工具之PMD的使用方法,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
PMD是一個靜態(tài)源代碼分析器。它發(fā)現(xiàn)了常見的編程缺陷,如未使用的變量、空捕獲塊、不必要的對象創(chuàng)建等等。
下載:File -> Settings -> Plugins -> Marketplace 搜索 “PMDPlugin” ,下載插件。
使用方法:在代碼編輯框或Project 窗口的文件夾、包、文件右鍵,選擇“Run PMD”->“Pre Defined”->“All”,對指定的文件夾、包、文件進行分析,分析結(jié)果在控制臺輸出。
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)容:
pmd -d 源代碼路徑 -f xml(結(jié)果輸出格式) -r 結(jié)果保存所在目錄及名稱 -R rulesets/java/unusedcode.xml
例子:
結(jié)果存放在制定文件目錄下,格式也為命令語句指定的:
檢測結(jié)果內(nèi)容:
官方文檔
需要先引入maven依賴
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!"); } }
命令行接口的方式
最簡單的方法是使用與命令行相同的接口調(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); } }
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); } }
這使您能夠更好地控制處理哪些文件,但也會更加復(fù)雜。您還可以提供自己的偵聽器和呈現(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è)置適當(dāng)?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)中是可用的。注意:當(dāng)在多線程模式下運行時(這是默認的),規(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)在,所有的準(zhǔ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é)果會根據(jù)指定格式輸出在指定文件目錄下。
D:\MyFile\Tool\pmd-bin-6.32.0\bin 目錄下打開cmd窗口輸入:
cpdgui.bat
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 } }
導(dǎo)出的自定義規(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>
以上是“Java代碼檢查工具之PMD的使用方法”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。