checkstyle 是一個幫助開發者按照某種習慣編寫 java 代碼的工具,他實現了代碼檢查的自動化,幫助人們從這種繁瑣的工作中解放出來。
默認提供了對 sun 編程規范的支持,但是 checkstyle 是一個具有高可配置性的,你完全可以根據自己的要求來配置需要檢查的內容。
有以下這些東西
\lib\checkstyle-3.1\contrib\checkstyle-noframes.xsl
\lib\checkstyle-3.1\checkstyle-all-3.1.jar
\lib\checkstyle-3.1\sun_checks.xml????????????????????
在build.xml文件中添加
??? <patternset id="java.files.pattern" includes="**/*.java"/>
??? <target name="checkstyle" depends="prepare"?????????? 依賴prepare target
??????? description="Check code style for compliance with coding standards">
??????? <property name="checkstyle.data.dir"
??????????? location="${build.dir}/docs/checkstyle"/>???????????????? 存放路徑
??????? <property name="checkstyle.data.file"
??????????? location="${checkstyle.data.dir}/checkstyle.xml"/>???? xml文件
??????? <property name="checkstyle.report.file"
??????????? location="${checkstyle.data.dir}/checkstyle.html"/>?? html文件
??????? <property name="checkstyle.xsl.file"
??????????? location="${checkstyle.dir}/contrib/checkstyle-noframes.xsl"/>?? 選用的樣式表,checkstyle.dir為jar包的位置
??????? <mkdir dir="${checkstyle.data.dir}"/>
??????? <taskdef resource="checkstyletask.properties" classpath="${checkstyle.jar}"/>? 引入jar文件
??????? <checkstyle config="${checkstyle.dir}/sun_checks.xml"????????????????????? 選用sun的規范,可以修改為自己的最佳實踐
??????????? failOnViolation="false" failureProperty="checkstyle.failure">
??????????? <fileset dir="src">?????????????????????????????????????????????????????????????????? 對src目錄進行檢查
??????????????? <patternset refid="java.files.pattern"/>
??????????? </fileset>
??????????? <fileset dir="test">????????????????????????????????????????????????????????????????? 對test目錄進行檢查
??????????????? <patternset refid="java.files.pattern"/>
??????????? </fileset>
??????????? <!-- uncomment to print to console as well -->
??????????? <!--formatter type="plain"/-->
??????????? <formatter type="xml" toFile="${checkstyle.data.file}"/>???????? 生成xml文件
??????? </checkstyle>
??????? <xslt in="${checkstyle.data.file}" out="${checkstyle.report.file}"
??????????? style="${checkstyle.xsl.file}"/>??????????????????????????????????????? 生成報告,其格式取決于checkstyle.xsl.file
??? </target>
如圖所示:圖1列出了所有文件,圖2列出了所以錯誤
下面解釋了一些常見的輸出結果,以供參考。 ?
序號 ? ? ? ? ? ?輸出內容意義 ? ?
1 ?Type ?is ?missing ?a ?javadoc ?commentClass ? ?缺少類型說明 ? ?
2“{” ?should ?be ?on ?the ?previous ?line ?“{” ?應該位于前一行 ? ?
3Methos ?is ?missing ?a ?javadoc ?comment方法前面缺少javadoc注釋 ? ?
4Expected ?@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“}” ?應該與下條語句位于同一行 ? ?
10Unused ?@param ?tag ?for ?“unused”沒有參數“unused”,不需注釋 ? ?
11Variable ?“CA” ?missing ?javadoc變量“CA”缺少javadoc注釋 ? ?
12Line ?longer ?than ?80characters行長度超過80 ? ?
13Line ?contains ?a ?tab ?character行含有”tab” ?字符 ? ?
14Redundant ?“Public” ?modifier冗余的“public” ?modifier ? ?
15Final ?modifier ?out ?of ?order ?with ?the ?JSL ?suggestionFinal ?modifier的順序錯誤 ? ?
16Avoid ?using ?the ?“.*” ?form ?of ?importImport格式避免使用“.*” ? ?
17Redundant ?import ?from ?the ?same ?package從同一個包中Import內容 ? ?
18Unused ?import-java.util.listImport進來的java.util.list沒有被使用 ? ?
19Duplicate ?import ?to ?line ?13重復Import同一個內容 ? ?
20Import ?from ?illegal ?package從非法包中 ?Import內容 ? ?
21“while” ?construct ?must ?use ?“{}”“while” ?語句缺少“{}” ? ?
22Variable ?“sTest1” ?must ?be ?private ?and ?have ?accessor ?method變量“sTest1”應該是private的,并且有調用它的方法 ? ?
23Variable ?“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“)” ?前面不能有空格
25 Line has trailing spaces? 行的最后不能有空格
根據sun_checks.xml文件的內容,可以到http://checkstyle.sourceforge.net/checks.html這里查看具體的配置,實現你們的最佳實踐