Checkstyle是一个开发工具,可以帮助编程人员编写符合Sun编码规范的java 代码 。程序员都非常讨厌程序的
check工作,checkstyle能够自动地完成这项烦琐但又非常重要的工作 ,从而节省了的程序员的时间,也提高了检
查的质量。Checkstyle已经成了加强编码规范的首选工具。
1. 安装
(1) 安装方式1:通过Help -> Instal New Sofware...方式安装;
(2) 安装方式2:http://sourceforge.net/projects/eclipse-cs/下载
net.sf.eclipsecs-updatesite_5.5.0.201111092104-bin.zip,解压后放入Eclipse的dropin目录下,重启Eclipse
生效。
2. 配置
Preference->CheckStyle,点右边的new按钮,增加一条规则,Type选择External Configuration File。名字叫"
checkStyle-3.7.2"。location选择此文件 checkstyle-3.7.2.xml ,点ok。check规则就已导入成功,在列表中将此
规则设为默认规则。
3. CheckStyle常见结果输出
1. Type is missing a javadoc commentClass:缺少类型说明
2. “{” should be on the previous line “{”:应该位于前一行
3. Methos is missing a javadoc comment:方法前面缺少javadoc注释
4. Expected @throws tag for “Exception”:在注释中希望有@throws的说明
5. “.” Is preceeded with whitespace “.”:前面不能有空格
6. “.” Is followed by whitespace“.”:后面不能有空格
7. “=” is not preceeded with whitespace“=”:前面缺少空格
8. “=” is not followed with whitespace“=”:后面缺少空格
9. “}” should be on the same line“}”:应该与下条语句位于同一行
10.Unused @param tag for “unused”:没有参数“unused”,不需注释
11.Variable “CA” missing javadoc:变量“CA”缺少javadoc注释
12.Line longer than 80characters:行长度超过80
13.Line contains a tab character:行含有”tab” 字符
14.Redundant “Public” modifier:冗余的“public” modifier
15.Final modifier out of order with the JSL suggestion:Final modifier的顺序错误
16.Avoid using the “.*” form of import:Import格式避免使用“.*”
17.Redundant import from the same package:从同一个包中Import内容
18.Unused import-java.util.list:Import进来的java.util.list没有被使用
19.Duplicate import to line 13:重复Import同一个内容
20.Import from illegal package:从非法包中Import内容
21.“while” construct must use “{}”“:while”语句缺少“{}”
22.Variable “sTest1” must be private and have accessor method:变量“sTest1”应该是private的,并且有调用它的
方法
23.Variable “ABC” must match pattern “^[a-z][a-zA-Z0-9]*$”:变量“ABC”不符合命名规则“^[a-z][a-zA-Z0-9]*$”
24.“(” is followed by whitespace“(”后面不能有空格 25“)” is proceeded by whitespace“)” 前面不能有空格
(1) Generate Checkstyle Report As Part of the Project Reports
<project>
...
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.9.1</version>
</plugin>
</plugins>
</reporting>
...
</project>
执行命令:mvn site
(2) Generate Checkstyle Report As Standalone
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.9.1</version>
<configuration>
<enableRulesSummary>false</enableRulesSummary>
...
</configuration>
</plugin>
</plugins>
</build>
...
</project>
执行命令:mvn checkstyle:checkstyle