欢迎来到天天文库
浏览记录
ID:14462555
大小:29.76 KB
页数:8页
时间:2018-07-28
《checkstyle代码检测》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、CheckStyle是什么? CheckStyle是SourceForge下的一个项目,提供了一个帮助JAVA开发人员遵守某些编码规范的工具。它能够自动化代码规范检查过程,从而使得开发人员从这项重要,但是枯燥的任务中解脱出来[1]。 2.2.CheckStyle检验的主要内容 CheckStyle默认提供一下主要检查内容: ·Javadoc注释 ·命名约定 ·标题 ·Import语句 ·体积大小 ·空白 ·修饰符 ·块 ·代码问题 ·类设计 ·混合检查(包活一些有用的比如非必须的System.out和printstackTrace) 从上面可以看出,Chec
2、kStyle提供了大部分功能都是对于代码规范的检查,而没有提供象PMD和Jalopy那么多的增强代码质量和修改代码的功能。但是,对于团队开发,尤其是强调代码规范的公司来说,它的功能已经足够强大。 2.3.CheckStyle的主要运行方式 目前,CheckStyle的版本是3.0,与以前的版本不同,它的配置文件是基于XML而非Properties文件。 它的3.0版本提供了两种运行的方式: ·命令行工具 ·ANT任务 同时,CheckStyle目前有很多针对流行IDE的插件,例如Eclipse、IntelliJIDEA、JBuilder等。但是,大部分都是基于2.4的版本,新
3、版本的特性不支持,同时配置也较为复杂。 因为一般情况下,如果与开发过程与环境集成起来,编码规范的检查会更加有效,因此,作为ANT任务的运行方式使用的更加普遍。 在ANT的build.xml文件中添加CheckStyle任务的步骤如下: 1.将checkstyle-all-3.1.jar拷贝到项目的LIB目录; 2.建立配置文件; 3.声明CheckStyle任务:<taskdefresource="checkstyletask.properties"classpath="${lib}/checkstyle-all-3.1.jar"/> 4.建立CheckStyle任务:<ta
4、rgetname="checkstyle"><checkstylec><filesetdir="${src}"includes="**/*.java"/></checkstyle></target> 2.4.定制CheckStyle CheckStyle的执行基于XML配置文件,它的主要组成部分是: ·Module:整个配置文件就是一棵Module树。根节点是CheckerModule。 ·Properties:它来决定一个Module如何进行检查。每个Module都有一个默认值,如果不满足开发需求,可以设定其它的值。 下面是一个示例:<modulename="MethodLe
5、ngth"><propertyname="max"value="60"/></module> 它表示,如果方法或者构造函数的长度超过60行,CheckStyle就会报错。而默认值是150行。 以下是一段CheckStyle对于Maven项目源文件的检查报告:Method'createExpression'isnotdesignedforextension-needstobeabstract,finalorempty.91UnabletogetclassinformationforJellyException.91Linehastrailingspaces.93Linehastrail
6、ingspaces.104Method'evaluate'isnotdesignedforextension-needstobeabstract,finalorempty.113Parametercontextshouldbefinal.113Linehastrailingspaces.130Method'getExpressionText'isnotdesignedforextension-needstobeabstract,finalorempty.131Linehastrailingspaces.134Linehastrailingspaces.135Method'toString
7、'isnotdesignedforextension-needstobeabstract,finalorempty.137Method'isSupportAntVariables'isnotdesignedforextension-needstobeabstract,finalorempty.156Method'setSupportAntVariables'isnotdesignedforextension-needstobeabs
此文档下载收益归作者所有