官方網站:http://pmd.sourceforge.net/
PMD的意義:
作者坦率的說,其實他們也不知道PMD所代表的意義,當初覺得只是PMD念起來好聽而已,
他們也曾經試著找出其含意。
用途:
靜態程式碼分析,找尋潛在問題,如
疑似臭蟲的程式碼(Possible bugs),如空的try/catch/finally/switch語句。
無法執行的程式碼(Dead code),如未使用的區域變數、參數、私有函數。
不佳的程式碼(Suboptimal code),如String/StringBuffer的不良使用方式。
過度複雜的語法(Overcomplicated expressions),如不必要的「if」語句或可用「while」迴圈取代「for」迴圈。
重複程式碼(Duplicate code),如透過複製/貼上有可能連臭蟲程式碼也複製了。
安裝需求:
1.Java JRE 1.6 或更新版本
2.WinZip 或 7-zip等其它解壓縮軟體
下載:
http://sourceforge.net/projects/pmd/files/pmd/5.2.1/
下載後解壓縮於
C:\JavaTools\pmd\
檔案清單
C:\JavaTools\pmd\bin\
1.bgastviewer.bat
2.cpd.bat
3.cpdgui.bat (PMD Duplicate Code Detector)
4.designer.bat(PMD Rule Designer)
5.pmd.bat
6.run.sh
C:\JavaTools\pmd\lib\
7.asm-5.0.3.jar
8.commons-io-2.4.jar
9commons-lang3-3.3.2.jar
10.javacc-5.0.jar
11.jaxen-1.1.4.jar
12.jcommander-1.35.jar
13.pmd-core-5.2.1.jar
14.pmd-cpp-5.2.1.jar
15.pmd-cs-5.2.1.jar
16.pmd-fortran-5.2.1.jar
17.pmd-go-5.2.1.jar
18.pmd-java-5.2.1.jar
19.pmd-javascript-5.2.1.jar
20.pmd-jsp-5.2.1.jar
21.pmd-php-5.2.1.jar
22.pmd-plsql-5.2.1.jar
23.pmd-ruby-5.2.1.jar
24.pmd-vm-5.2.1.jar
25.pmd-xml-5.2.1.jar
26.rhino-1.7R4.jar
27.saxon-9.1.0.8.jar
28.saxon-9.1.0.8-dom.jar
29.xercesImpl-2.11.0.jar
30.xml-apis-1.4.01.jar
PMD支援的自訂義檢核規則:
C:\JavaTools\pmd\pmd.bat執行訊息
C:\JAVA\pmd-bin-5.2.1\bin>pmd.bat
For example: Languages and version suported: Available report formats and their configuration properties are: For example on windows: For example on *nix: |
執行範例:
使用參數說明:
-R:使用「,」分隔檢核規則
-d:指定要檢核原始碼路徑
-r:定義輸出檔案名稱,否則只顯示在「System.out」
-f:定義檢核結果輸出格式,預設為「text」,支援html、xml、csv、emacs、ideaj、summaryhtml、textcolor、textpad、vbhtml、xslt、yahtml
-e:要檢核原始碼的檔案編碼,預設為「UTF-8」
執行條件:
1.參考PMD支援的 JAVA檢核規則 http://pmd.sourceforge.net/pmd-5.2.1/pmd-java/rules/index.html
使用檢核條件
Unused Code :The Unused Code ruleset contains rules that find unused or ineffective code.
Unnecessary:The Unnecessary Ruleset contains a collection of rules for unnecessary code.
Basic:The Basic ruleset contains a collection of good practices which should be followed.
Braces:The Braces ruleset contains rules regarding the use and placement of braces.
J2EE:Rules specific to the use of J2EE implementations.
JavaBeans:The JavaBeans Ruleset catches instances of bean rules not being followed.
Controversial:The Controversial ruleset contains rules that, for whatever reason, are considered controversial.They are held here to allow people to include them as they see fit within their custom rulesets.
2.假設JAVA原始碼路徑為 C:\Demo\pmdProj\src\
3.使用用PMD檢查C:\Demo\pmdProj\src\的原始碼
4.需產製html格式報表
Windows下於命令視窗執行語法
C:\JavaTools\pmd\bin\pmd.bat -d C:\Demo\pmdProj\src\ -f html -R java-basic,java-unnecessary,java-unusedcode,java-braces,java-j2ee,java-javabeans,java-controversial -r pmdProj_report.html
備註:
若檢核條件名稱寫錯會有類似下列的錯誤訊息
net.sourceforge.pmd.RuleSetNotFoundException: Can't find resource 'null' for rule 'java-Unnecessary'. Make sure the resource is a valid file or URL a
nd is on the CLASSPATH. Here's the current classpath: .
請自行選擇合適於自己的檢核規則,並將執行語法寫成批次檔以利後續執行。
與PMD類似的專案:
擷取資料來源:http://pmd.sourceforge.net/pmd-5.2.1/overview/similar-projects.html
Open Source
- Checkstyle - Very detailed, supports both Maven and Ant. Uses ANTLR.
- DoctorJ - Uses JavaCC. Checks Javadoc, syntax and calculates metrics.
- ESC/Java - Finds null dereference errors, array bounds errors, type cast errors, and race conditions. Uses Java Modeling Language annotations.
- FindBugs - works on bytecode, uses BCEL. Source code uses templates, nifty stuff!
- Hammurapi - Uses ANTLR, excellent documentation, lots of rules
- Jamit - bytecode analyzer, nice graphs
- JCSC - Does a variety of coding standard checks, uses JavaCC and the GNU Regexp package.
- Jikes - More than a compiler; now it reports code warnings too
- JLint - Written in C++. Uses data flow analysis and a lock graph to do lots of synchronization checks. Operates on class files, not source code.
- JPathFinder - A verification VM written by NASA; supports a subset of the Java packages
- JWiz - Research project, checks some neat stuff, like if you create a Button without adding an ActionListener to it. Neat.
Commercial
- AppPerfect - 750 rules, produces PDF/Excel reports, supports auto-fixing problems
- Assent - The usual stuff, seems pretty complete.
- Aubjex - Rules aren't listed online. Appears to have some code modification stuff, which would be cool to have in PMD. $299.
- AzoJavaChecker - Rules aren't listed online so it's hard to tell what they have. Not sure how much it costs since I don't know German.
- CodePro AnalytiX - Eclipse plug-in, extensive audit rules, JUnit test generation/editing, code coverage and analysis
- Enerjy Java Code Analyser - 200 rules, lots of IDE plugins
- Flaw Detector - In beta, does control/data flow analysis to detect NullPointerExceptions
- JStyle - $995, nice folks, lots of metrics and rules
- JTest - Very nice with tons of features, but also very expensive and requires a running X server (or Xvfb) to run on Linux. They charge $500 to move a license from one machine to another.
- Lint4J - Lock graph, DFA, and type analysis, many EJB checks
- SolidSDD - Code duplication detection, nice graphical reporting. Free licensing available for Educational or OSS use.
High level reporting
- XRadar - Agregates data from a lot of code quality tool to generate a full quality dashboard.
- Sonar - Pretty much like XRadar, but younger project, fully integrated to maven 2 (but requires a database)
- Maven Dashboard - Same kind of agregator but only for maven project.
- QALab - Yet another maven plugin...