使用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的压缩包就可以了,如下图:

to_downlod_pmd_2_1

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

使用

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

@echo off

rem 要检查的文件和路径,也可以是代码目录
set filepath=E:\SRC\ComposeMessageActivity.java
rem 报告输出的路径
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

FilesTotalQRank Level 1QRank Level 2QRank Level 3QRank Level 4QRank Level 5QRank Level 6QRank Level 7QRank Level 8
118
0
0
18
0
0
0
0
0

E:\SRC\ConfirmRateLimitActivity

PrioBegin LineMethodDescription
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
47onCreateAvoid excessively long variable names like savedInstanceState
3
47onCreateParameter 'savedInstanceState' is not assigned and could be declared final
3
55onClickAvoid variables with short names like v
3
55onClickParameter 'v' is not assigned and could be declared final
3
62onClickAvoid variables with short names like v
3
62onClickParameter 'v' is not assigned and could be declared final
3
68onCreateTo be compliant to J2EE, a webapp should not use any thread.
3
71runLog.v calls should be protected by checking Config.LOGV first
3
84onResumeLocal variable 'delay' could be declared final
3
99onPausesuper should be called at the end of the method
3
105onKeyDownParameter 'event' is not assigned and could be declared final
3
105onKeyDownParameter 'keyCode' is not assigned and could be declared final
3
113doAnswerParameter 'answer' is not assigned and could be declared final
3
114doAnswerLocal 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这类工具不少公司在真正使用时都会对规则进行过滤,要不然搞出来的错误实在是太多了~~~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值