Chinaunix首页 | 论坛 | 博客
  • 博客访问: 661128
  • 博文数量: 220
  • 博客积分: 10487
  • 博客等级: 上将
  • 技术积分: 2072
  • 用 户 组: 普通用户
  • 注册时间: 2009-08-09 00:25
文章分类

全部博文(220)

文章存档

2012年(5)

2011年(38)

2010年(135)

2009年(42)

我的朋友

分类: Java

2009-08-22 03:23:19

最近一个月日夜加班开发一个银企互联项目,狂敲了好多代码。这个星期做单元测试,才发现代码的质量较低,bug改个不停。
 
今天趁有空,仔细研究了一下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

以前在项目组,把它和CruiseControl集成起来用过,针对项目持续集成构建,它能自动检测各成员提交代码规范性和潜在问题,并自动发送不合格问题到提交代码人邮箱。但由于发现的问题太多,最后还是取消了。

今天再次想起它来了,把它的用法详细的说下

1.下载PMD压缩包

2.解压

3.创建如下脚本文件(checkAll.bat),放置在解压后的bin目录下:

@echo off

rem author:xiadehu 2009-8-22

rem 要检查的文件和路径
set filepath=F:\SRC\EASCBM\CBMS_SRC\CBMS_CJ001_Common\ProductCode\com\zte\cbms\common\framework\upload\MyUpload.java

rem 报告输出的路径
set reportpath=E:\code-check-report\

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

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

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

rem 指定输出rendener
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%

4.以上脚本要设置的只有要检查的文件路径(java文件,文件夹,jar/zip包),和检查结果要输出的目录即可。

5.执行该批量处理文件,结果如下:

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

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

line:80行,

方法findItemAndAdjustAllFiles,

建议:Substitute calls to size() == 0 (or size() != 0) with calls to isEmpty()

意思是说对List类的if(list.size()==0),建议使用isEmpty()来操作

6.接下来便是按照提示一个个更改了,相信修改之后代码可以让自己慢慢欣赏了,因为里面检查的大多数规则,都是有着丰富经验的JAVA程序员提供的AST(抽象语义树,不用明白它具体什么意思,可以当做一条检查规则),它从程序容易出现的问题,和不规范的设计等等方面进行了检查。

7.重要的是长期坚持,这样好的编程习惯应该会不自觉的形成。

PMD 4.2.5 Report
2009-08-22 - 02:24:02


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 27
0
0
27
0
0
0
0
0

F:\SRC\EASCBM\CBMS_SRC\CBMS_CJ001_Common\ProductCode\com\zte\cbms\common\framework\upload\MyUpload

Prio Begin Line Method Description
Total number of violations for this class: 27 (Click anywhere on this row to see/hide details)
3
29
3
31 setFbpvDao
3
44 findAccessories
3
45 findAccessories
3
48 findAccessories
3
48 findAccessories
3
59 findItemAndAdjustAllFiles
3
60 findItemAndAdjustAllFiles
3
62 findItemAndAdjustAllFiles
3
65 findItemAndAdjustAllFiles
3
67 findItemAndAdjustAllFiles Avoid using if statements without curly braces
3
67 findItemAndAdjustAllFiles
3
71 findItemAndAdjustAllFiles
3
71 findItemAndAdjustAllFiles
3
72 findItemAndAdjustAllFiles StringBuffer.append is called 2 consecutive times with literal Strings. Use a single append with a single String.
3
77 findItemAndAdjustAllFiles
3
80 findItemAndAdjustAllFiles
3
82 findItemAndAdjustAllFiles
3
82 findItemAndAdjustAllFiles
3
86 findItemAndAdjustAllFiles
3
93 findItemAndAdjustAllFiles
3
93 findItemAndAdjustAllFiles
3
99 findFileByHql
3
99 findFileByHql
3
100 findFileByHql
3
100 findFileByHql
3
101 findFileByHql StringBuffer.append is called 2 consecutive times with literal Strings. Use a single append with a single String.

Generated by on 2009-08-22 - 02:24:02.

阅读(1333) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~