日期:2014-05-16  浏览次数:20461 次

CheckStyle 和findbugs 验证代码质量工具
CheckStyle是什么?

  CheckStyle是SourceForge下的一个项目,提供了一个帮助JAVA开发人员遵守某些编码规范的工具。它能够自动化代码规范检查过程,从而使得开发人员从这项重要,但是枯燥的任务中解脱出来[1]。

  2.2. CheckStyle检验的主要内容

  CheckStyle默认提供一下主要检查内容:

  ·Javadoc注释

  ·命名约定

  ·标题

  ·Import语句

  ·体积大小

  ·空白

  ·修饰符

  ·块

  ·代码问题

  ·类设计

  ·混合检查(包活一些有用的比如非必须的System.out和printstackTrace)

  从上面可以看出,CheckStyle提供了大部分功能都是对于代码规范的检查,而没有提供象PMD和Jalopy那么多的增强代码质量和修改代码的功能。但是,对于团队开发,尤其是强调代码规范的公司来说,它的功能已经足够强大。

  2.3. CheckStyle的主要运行方式

  目前,CheckStyle的版本是3.0,与以前的版本不同,它的配置文件是基于XML而非Properties文件。

  它的3.0版本提供了两种运行的方式:

  ·命令行工具

  ·ANT任务

  同时,CheckStyle目前有很多针对流行IDE的插件,例如Eclipse、IntelliJ IDEA、JBuilder等。但是,大部分都是基于2.4的版本,新版本的特性不支持,同时配置也较为复杂。

  因为一般情况下,如果与开发过程与环境集成起来,编码规范的检查会更加有效,因此,作为ANT任务的运行方式使用的更加普遍。

  在ANT的build.xml文件中添加CheckStyle任务的步骤如下:

  1. 将checkstyle-all-3.1.jar拷贝到项目的LIB目录;

  2. 建立配置文件;

  3. 声明CheckStyle任务:

<taskdef resource="checkstyletask.properties" classpath="${lib}/checkstyle-all-3.1.jar"/>

  4. 建立CheckStyle任务:

<target name="checkstyle">
<checkstyle c>
<fileset dir="${src}" includes=" **/*.java" />
</checkstyle>
</target>

  2.4. 定制CheckStyle

  CheckStyle的执行基于XML配置文件,它的主要组成部分是:

  ·Module:整个配置文件就是一棵Module树。根节点是Checker Module。

  ·Properties:它来决定一个Module如何进行检查。每个Module都有一个默认值,如果不满足开发需求,可以设定其它的值。

  下面是一个示例:

<module name="MethodLength">
<property name="max" value="60"/>
</module>

  它表示,如果方法或者构造函数的长度超过60行,CheckStyle就会报错。而默认值是150行。

  以下是一段CheckStyle对于Maven项目源文件的检查报告:

Method 'createExpression' is not designed for extension - needs to be abstract, final or empty. 91
Unable to get class information for JellyException. 91
Line has trailing spaces. 93
Line has trailing spaces. 104
Method 'evaluate' is not designed for extension - needs to be abstract, final or empty. 113
Parameter context should be final. 113
Line has trailing spaces. 130
Method 'getExpressionText' is not designed for extension - needs to be abstract, final or empty. 131
Line has trailing spaces. 134
Line has trailing spaces. 135
Method 'toString' is not designed for extension - needs to be abstract, final or empty. 137
Method 'isSupportAntVariables' is not designed for extension - needs to be abstract, final or empty. 156
Method 'setSupportAntVariables' is not designed for extension - needs to be abstract, final or empty. 168
Parameter supportAntVariables should be final. 168
'supportAntVariables' hides a field. 168
Method 'isValidAntVariableName' is not designed for extension - needs to be abstract, final or empty. 183
Parameter text should be final. 183

  一般情况下,与IDE集成在一起使用的时候,点击出错的条目,可以跳转到相应的代码。
本贴来自ZDNetChina中文社区http://bbs.zdnet.com.cn,本贴地址:http://bbs.zdnet.com.cn/viewthread.php?tid=178617




三、CheckStyle的最佳实践

  3.1. Sun’s Code Conventions的修改

  在CheckStyle的最新发布版本中,有一个对于Sun的Java编码规范的配置文件信息。但是,其中有很多条目并不一定符合项目开发的需要。就算是对于很多优秀的开源项目,按照这个规范来进行检查,也会出现成千上万的错误。

  下面提出的一些修改意见,是从实际项目执行过程中总结出来的,可以作为大家的参考。我们以CheckStyle3.0配置文件的顺序来介绍:

  1. 去除对于每个包都有一个package.html文件的限制;

<!--<module name="PackageHtml"/>-->
  
  2. 修改对于JavaDoc Comments的限定:对于很多使用Code Generator的项目来说,需要将手写代码与生成代码、单元测试代码的检查分开进行;

  3. 修改对于体积大小的限制:目前,很多显示器都是17寸,而且打印方面的限制也比以前有所改善,同时,由于代码中Factory等模式的运用,以及有意义的方法名称等约定,默认每行代码的长度(80)是远远不能满足要求;对于方法长度等等,也应该根据项目情况自行决定:

<module name="FileLength"/>
<module name="LineLength">
<property name="max" value="120"/>
</module>
<module name="MethodLength">
<property name="max" value="300