分类: Java
2009-08-22 03:23:19
PMD scans Java source code and looks for potential problems like:
翻译:
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目录下:
|
4.以上脚本要设置的只有要检查的文件路径(java文件,文件夹,jar/zip包),和检查结果要输出的目录即可。
5.执行该批量处理文件,结果如下:
(1).生成的报告界面十分友好,每个类存在什么级别的多少个问题,一目了然。
(2).存在的每个问题,如何修改的建议也一一指出,比如
|
6.接下来便是按照提示一个个更改了,相信修改之后代码可以让自己慢慢欣赏了,因为里面检查的大多数规则,都是有着丰富经验的JAVA程序员提供的AST(抽象语义树,不用明白它具体什么意思,可以当做一条检查规则),它从程序容易出现的问题,和不规范的设计等等方面进行了检查。
7.重要的是长期坚持,这样好的编程习惯应该会不自觉的形成。
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 |
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.