pmd使用说明以及生成的报告

使用PMD检查代码      以前不知道PMD,最近听说了PC Lint,才开始接触PMD,这么好的工具,如果善用了,真是可以帮助提高很多,对自己的代码习惯、规范等都有很大的帮助~

来看看他到底是怎么样的》。。。

    很多公司都在用的PC Lint是针对C/c++的一个静态代码审查工具,据说Microsoft把PC Lint审查无错误无警告作为代码要过的第一关;而PMD是个跟他功能类似的针对java的工具,PMD是开源的,可以下载到整个开源项目,其官网上有对其这样的简介(PMD):

简介

PMD scans Java source code and looks for potential problems like:

•Possible bugs - empty try/catch/finally/switch statements
•Dead code - unused local variables, parameters and private methods
•Suboptimal code - wasteful String/StringBuffer usage
•Overcomplicated expressions - unnecessary if statements, for loops that could be while loops
•Duplicate code - copied/pasted code means copied/pasted bugs
译:

PMD扫描java代码并寻找潜在的如下问题:

     1.可能的bug - 空try/catch/finally/switch语句

     2.无效代码 -未使用的变量,参数和私有方法

     3.非最佳的代码 - 较耗费资源的String/StringBuffer用法

     4.过于复杂的表达式 - 不必要的if语句,或应该为while的for循环

     5.重复代码 - 复制/粘贴代码意味着复制/粘贴bug

下载

这里可以下载最新版本的PMD(pmd-4.2.5),里面有源代码和bin zip,下载bin的压缩包就可以了,如下图:


我下载的时候,速度超慢,10KB/s的跳~

使用

下载下来之后,随便解压到任意目录,然后根据自己的目录修改下面的这个脚本(.bat),放置到解压出来的目录的bin文件夹下即可;

@echo off

rem 要检查的文件和路径,也可以是代码目录set filepath=E:\SRC\ComposeMessageActivity.javarem 报告输出的路径
set reportpath=E:\

rem 获取当前年月日和时分秒
set nowTime=%DATE:~0,10%" "%TIME:~1,7%

rem 替换:为-
set nowTime=%nowtime::=-%

rem 合成报告文件路径名
set outfile=%reportpath%%nowtime%.html

rem 指定输出的格式
set xslt="../etc/xslt/corley-pmd-report.xslt"

rem 指定检查规则,这里包括了官方推出的java的所有规则,也可以自己指定其他规则
set ruleset="rulesets/internal/all-java.xml"

rem 执行检查并输出报告
pmd %filepath% nicehtml %ruleset% -xslt %xslt% -reportfile %outfile%

运行这个脚本文件就可以得到报告文件如下:

Summary
Files Total QRank Level 1 QRank Level 2 QRank Level 3 QRank Level 4 QRank Level 5 QRank Level 6 QRank Level 7 QRank Level 8
1 18 0 0 18 0 0 0 0 0

--------------------------------------------------------------------------------

E:\SRC\ConfirmRateLimitActivity
Prio Begin Line Method Description
Total number of violations for this class: 18 (Click anywhere on this row to see/hide details) 
3 42  Found non-transient, non-static member. Please mark as transient or provide accessors.
3 43  Found non-transient, non-static member. Please mark as transient or provide accessors.
3 44  Found non-transient, non-static member. Please mark as transient or provide accessors.
3 44  To be compliant to J2EE, a webapp should not use any thread.
3 47 onCreate Avoid excessively long variable names like savedInstanceState
3 47 onCreate Parameter 'savedInstanceState' is not assigned and could be declared final
3 55 onClick Avoid variables with short names like v
3 55 onClick Parameter 'v' is not assigned and could be declared final
3 62 onClick Avoid variables with short names like v
3 62 onClick Parameter 'v' is not assigned and could be declared final
3 68 onCreate To be compliant to J2EE, a webapp should not use any thread.
3 71 run Log.v calls should be protected by checking Config.LOGV first
3 84 onResume Local variable 'delay' could be declared final
3 99 onPause super should be called at the end of the method
3 105 onKeyDown Parameter 'event' is not assigned and could be declared final
3 105 onKeyDown Parameter 'keyCode' is not assigned and could be declared final
3 113 doAnswer Parameter 'answer' is not assigned and could be declared final
3 114 doAnswer Local variable 'intent' could be declared final




Generated by PMD 4.2.5 on 2011-09-09 - 14:49:43.


(1).生成的报告界面十分友好,每个类存在什么级别的多少个问题,一目了然。

(2).存在的每个问题,如何修改的建议也一一指出,比如

line: 84
方法: onResume
建议:Local variable 'delay' could be declared final
意思是说,这个函数里的变量delay最好加上final关键字~     接下来便是按照提示一个个更改了,相信修改之后代码可以让自己慢慢欣赏了,因为里面检查的大多数规则,都是有着丰富经验的JAVA程序员提供的AST(抽象语义树,不用明白它具体什么意思,可以当做一条检查规则),它从程序容易出现的问题,和不规范的设计等等方面进行了检查。

    重要的是长期坚持,这样好的编程习惯应该会不自觉的形成,说实话,PMD真的很严格,苛刻,所以PMD和PC LInt这类工具不少公司在真正使用时都会对规则进行过滤,要不然搞出来的错误实在是太多了~~~

猜你喜欢

转载自sisi111111.iteye.com/blog/1667223