官方網站: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等其它解壓縮軟體

下載:

pmd-bin-5.2.1.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支援的自訂義檢核規則:

項次 名稱 規則說明網址
1 JAVA http://pmd.sourceforge.net/pmd-5.2.1/pmd-java/rules/index.html
2 JAVASCRIPT http://pmd.sourceforge.net/pmd-5.2.1/pmd-javascript/rules/index.html
3 JSP http://pmd.sourceforge.net/pmd-5.2.1/pmd-jsp/rules/index.html
4 PL/SQL http://pmd.sourceforge.net/pmd-5.2.1/pmd-plsql/rules/index.html
5 Velocity http://pmd.sourceforge.net/pmd-5.2.1/pmd-vm/rules/index.html
6 XML http://pmd.sourceforge.net/pmd-5.2.1/pmd-xml/rules/index.html

 

 

 

 

 

 

 

 

C:\JavaTools\pmd\pmd.bat執行訊息

C:\JAVA\pmd-bin-5.2.1\bin>pmd.bat
Usage: pmd [options]
Options:
-auxclasspath
specifies the classpath for libraries used by the source code. This is
used by the type resolution. Alternatively, a 'file://' URL to a text file
containing path elements on consecutive lines can be specified.
-benchmark, -b
Benchmark mode - output a benchmark report upon completion; default to
System.err
Default: false
-dir, -d
root directory for sources
-encoding, -e
specifies the character set encoding of the source code files PMD is
reading (i.e., UTF-8)
Default: UTF-8
-format, -f
report format type
Default: text
-help, -h, -H
Display help on usage
Default: false
-language, -l
specify a language PMD should use
-minimumpriority, -min
rule priority threshold; rules with lower priority than they will not be
used
Default: Low
-property, -P
{name}={value}: define a property for the report
Default: {}
-reportfile, -r
send report output to a file; default to System.out
* -rulesets, -R
comma separated list of rulesets name to use
-shortnames
prints shortened filenames in the report
Default: false
-showsuppressed
report should show suppressed rule violations
Default: false
-stress, -S
performs a stress test
Default: false
-suppressmarker
specifies the String that marks the a line which PMD should ignore;
default is NOPMD
Default: NOPMD
-threads, -t
set the number of threads used by PMD
Default: 1
-uri, -u
Database URI for sources
-debug, -verbose, -D, -V
Debug mode
Default: false
-version, -v
specify version of a language PMD should use


Mandatory arguments:
1) A java source code filename or directory
2) A report format
3) A ruleset filename or a comma-delimited string of ruleset filenames

For example:
C:\>pmd-bin-5.2.1\bin\pmd.bat -d c:\my\source\code -f html -R java-unusedcode

Languages and version suported:
xsl, java, xml, vm, ecmascript, jsp, plsql

Available report formats and their configuration properties are:
csv: Comma-separated values tabular format.
problem - Include Problem column default: true
package - Include Package column default: true
file - Include File column default: true
priority - Include Priority column default: true
line - Include Line column default: true
desc - Include Description column default: true
ruleSet - Include Rule set column default: true
rule - Include Rule column default: true
emacs: GNU Emacs integration.
html: HTML format
linePrefix - Prefix for line number anchor in the source file.
linkPrefix - Path to HTML source.
ideaj: IntelliJ IDEA integration.
classAndMethodName - Class and Method name, pass '.method' when processing a directory. default:
sourcePath - Source path. default:
fileName - File name. default:
summaryhtml: Summary HTML format.
linePrefix - Prefix for line number anchor in the source file.
linkPrefix - Path to HTML source.
text: Text format.
textcolor: Text format, with color support (requires ANSI console support, e.g. xterm, rxvt, etc.).
color - Enables colors with anything other than 'false' or '0'. default: yes
textpad: TextPad integration.
vbhtml: Vladimir Bossicard HTML format.
xml: XML format.
encoding - XML encoding format, defaults to UTF-8. default: UTF-8
xslt: XML with a XSL Transformation applied.
encoding - XML encoding format, defaults to UTF-8. default: UTF-8
xsltFilename - The XSLT file name.
yahtml: Yet Another HTML format.
outputDir - Output directory.

For example on windows:
C:\>pmd-bin-5.2.1\bin\pmd.bat -dir c:\my\source\code -format text -R java-unusedcode,java-imports -version 1.5 -language java -debug
C:\>pmd-bin-5.2.1\bin\pmd.bat -dir c:\my\source\code -f xml -rulesets java-basic,java-design -encoding UTF-8
C:\>pmd-bin-5.2.1\bin\pmd.bat -d c:\my\source\code -rulesets java-typeresolution -auxclasspath commons-collections.jar;derby.jar
C:\>pmd-bin-5.2.1\bin\pmd.bat -d c:\my\source\code -f html -R java-typeresolution -auxclasspath file:///C:/my/classpathfile

For example on *nix:
$ pmd-bin-5.2.1/bin/run.sh pmd -dir /home/workspace/src/main/java/code -f html -rulesets java-basic,java-design
$ pmd-bin-5.2.1/bin/run.sh pmd -d ./src/main/java/code -f xslt -R java-basic,java-design -property xsltFilename=my-own.xsl
$ pmd-bin-5.2.1/bin/run.sh pmd -d ./src/main/java/code -f html -R java-typeresolution -auxclasspath commons-collections.jar:derby.jar

 

執行範例:

      使用參數說明:

             -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.

Similar to CPD

Commercial

  • Simian - fast, works with Java, C#, C, CPP, COBOL, JSP, HTML
  • Simscan - free for open source projects

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...

 

arrow
arrow
    文章標籤
    PMD 原始碼分析
    全站熱搜

    K 發表在 痞客邦 留言(0) 人氣()