免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3556 | 回复: 0
打印 上一主题 下一主题

JAVA程序员的利器,PMD [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-08-22 03:23 |只看该作者 |倒序浏览
最近一个月日夜加班开发一个银企互联项目,狂敲了好多代码。这个星期做单元测试,才发现代码的质量较低,bug改个不停。

今天趁有空,仔细研究了一下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
以前在项目组,把它和CruiseControl集成起来用过,针对项目持续集成构建,它能自动检测各成员提交代码规范性和潜在问题,并自动发送不合格问题到提交代码人邮箱。但由于发现的问题太多,最后还是取消了。
今天再次想起它来了,把它的用法详细的说下
1.下载PMD压缩包
最新pmd4.2.5.zip
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.重要的是长期坚持,这样好的编程习惯应该会不自觉的形成。
  @import "fcoltable.css";
        body { margin-left: 2%; margin-right: 2%; font:normal verdana,arial,helvetica; color:#000000; }
        table.sortable tr th { font-weight: bold; text-align:left; background:#a6caf0; }
        table.sortable tr td { background:#eeeee0; }
        table.classcount tr th { font-weight: bold; text-align:left; background:#a6caf0; }
        table.classcount tr td { background:#eeeee0; }
        table.summary tr th { font-weight: bold; text-align:left; background:#a6caf0; }
        table.summary tr td { background:#eeeee0; text-align:center;}
        .p1 { background:#FF3300; }
        .p2 { background:#FF9966; }
        .p3 { background:#FFCC33; }
        .p4 { background:#FFFF00; }
        .p5 { background:#FFFFCC; }
        .p6 { background:#CCFFCC; }
        .p7 { background:#66FF66; }
        .p8 { background:#00FF00; }
  div.top{text-align:right;margin:1em 0;padding:0}
  div.top div{display:inline;white-space:nowrap}
  div.top div.left{float:left}
  #content>div.top{display:table;width:100%}
  #content>div.top div{display:table-cell}
  #content>div.top div.left{float:none;text-align:left}
  #content>div.top div.right{text-align:right}
  #topbar{
   position:absolute;
   border: 1px solid black;
   padding: 2px;
   background-color: lightyellow;
   width: 620px;
   visibility: hidden;
   z-index: 100;
  }
   
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
Found non-transient, non-static member. Please mark as transient or provide accessors.
3
31
setFbpvDao
Parameter 'fbpvDao' is not assigned and could be declared final
3
44
findAccessories
Parameter 'appFrameCon' is not assigned and could be declared final
3
45
findAccessories
Local variable 'list' could be declared final
3
48
findAccessories
Avoid instantiating new objects inside loops
3
48
findAccessories
Do not add empty strings
3
59
findItemAndAdjustAllFiles
Parameter 'appFrameCon' is not assigned and could be declared final
3
60
findItemAndAdjustAllFiles
Local variable 'itemHeaderId' could be declared final
3
62
findItemAndAdjustAllFiles
Local variable 'resultList' could be declared final
3
65
findItemAndAdjustAllFiles
Local variable 'itemList' could be declared final
3
67
findItemAndAdjustAllFiles
Avoid using if statements without curly braces
3
67
findItemAndAdjustAllFiles
Substitute calls to size() == 0 (or size() != 0) with calls to isEmpty()
3
71
findItemAndAdjustAllFiles
Local variable 'hql' could be declared final
3
71
findItemAndAdjustAllFiles
StringBuffer constructor is initialized with size 16, but has at least 128 characters appended.
3
72
findItemAndAdjustAllFiles
StringBuffer.append is called 2 consecutive times with literal Strings. Use a single append with a single String.
3
77
findItemAndAdjustAllFiles
Local variable 'adjustIds' could be declared final
3
80
findItemAndAdjustAllFiles
Substitute calls to size() == 0 (or size() != 0) with calls to isEmpty()
3
82
findItemAndAdjustAllFiles
Avoid instantiating new objects inside loops
3
82
findItemAndAdjustAllFiles
Local variable 'adjustList' could be declared final
3
86
findItemAndAdjustAllFiles
Substitute calls to size() == 0 (or size() != 0) with calls to isEmpty()
3
93
findItemAndAdjustAllFiles
Avoid instantiating new objects inside loops
3
93
findItemAndAdjustAllFiles
Do not add empty strings
3
99
findFileByHql
Parameter 'relationId' is not assigned and could be declared final
3
99
findFileByHql
Parameter 'type' is not assigned and could be declared final
3
100
findFileByHql
Local variable 'hql' could be declared final
3
100
findFileByHql
StringBuffer constructor is initialized with size 16, but has at least 85 characters appended.
3
101
findFileByHql
StringBuffer.append is called 2 consecutive times with literal Strings. Use a single append with a single String.
Generated by
PMD 4.2.5
on 2009-08-22 - 02:24:02.


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/103146/showart_2034142.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP