代碼格式化的好處我想我就不需要說了,大家肯定也都了解其優點,否則你也不會看這個文檔.
這裡我以checkstyle3.4為例,講解checkstyle的使用,确切的說是其配置的具體解釋,假如你是checkstyle3.4以下,那麼有一部分module将是不可以答應的,請删除之;在這份文檔中我試圖把所有的module都分析講解一次,同時下面的這份文檔也是我的設定,對于我個人不喜歡的設定我都注釋了,假如你需要請取消注釋或如何!!
checkstyle簡介,其是目前最廣泛使用的代碼檢查工具,功能強大,操作簡單可以和ant結合使用,最重要的是其是open source的,你不用擔心收到律師函,哈哈!
下載下傳本文示例: olics_checkstyle_checks.zip
開始我們的講解:
首先,checkstyle可以和ant結合使用,下面是ant腳步片斷
<!--checkstyle配置,這裡你替換成你實際的環境-->
<property name="checkstyle.config" value="${project.docs.dir}/checkstyle_checks.xml"/>
<property name="checkstyle.report.style" value="${project.docs.dir}/checkstyle-frames.xsl"/>
<property name="checkstyle.result" value="${build.checkstylereport.dir}/checkstyle_result.xml"/>
<property name="checkstyle.report" value="${build.checkstylereport.dir}/checkstyle_report.html"/>
<!―checkstyle腳步-->
<taskdef resource="checkstyletask.properties" classpathref=" checkstyle-all-3.4.jar"/>
<target name="checkstyle_check" depends="init">
<checkstyle config="${checkstyle.config}" failonviolation="false" failureproperty="checkstyle.failure">
<formatter type="xml" tofile="${checkstyle.result}"/>
<fileset dir="${project.src.dir}" includes="**/*.java"/>
</checkstyle>
<!―生成報告,其格式取決于${checkstyle.report.style}-->
<style in="${checkstyle.result}" out="${checkstyle.report}" style="${checkstyle.report.style}"/>
</target>
下面是我了解的 checkstyle 的使用 :
<?xml version="1.0" encoding="utf-8"?>
<!doctype module public "-//puppy crawl//dtd check configuration 1.2//en"
<!-- 對于所有的子產品來書,假如有這個子產品則說明檢測這一項,沒有則不檢測這一項 -->
<!-- 所有的子產品中,其root必須為checker -->
<module name="checker">
<!-- 檢驗每個包是否存在package.html檔案-->
<!--
<module name="packagehtml"/>
-->
<!-- 檢驗每個檔案末尾是否有一個空行,在unit機器上是有的,在cvs上假如沒有會有警告的-->
<module name="newlineatendoffile"/>
<!-- checks that property files contain the same keys. -->
<module name="translation"/>
<module name="treewalker">
<!-- checks for javadoc comments. -->
<!-- checks javadoc comments for method definitions.-->
<module name="javadocmethod">
<property name="scope" value="public"/>
<!-- 是否答應錯誤的參數聲明,true為答應,預設為不答應 -->
<property name="allowmissingparamtags" value="true"/>
<!-- 是否答應錯誤的錯誤聲明,true為答應,預設為不答應 -->
<property name="allowmissingthrowstags" value="true"/>
<!-- 是否答應錯誤的傳回類型聲明,true為答應,預設為不答應 -->
<property name="allowmissingreturntag" value="true"/>
</module>
<!--checks javadoc comments for class and interface definitions.-->
<module name="javadoctype"/>
<!-- checks that variables have javadoc comments.-->
<module name="javadocvariable">
<property name="scope" value="protected"/>
<!-- 檢查javadoc的格式 -->
<module name="javadocstyle">
<!-- comment的第一句的末尾是否要有一個句号,true必須有,default為true -->
<property name="checkfirstsentence" value="false"/>
<!-- 檢查錯誤的html腳本,比如不比對,true檢查,default為true -->
<property name="checkhtml" value="true"/>
<!-- checks for naming conventions. -->
<!-- 确省必須以abstract開始或者以factory結束 -->
<module name="abstractclassname"/>
<module name="constantname"/>
<module name="localfinalvariablename"/>
<module name="localvariablename"/>
<module name="membername"/>
<module name="methodname"/>
<module name="packagename"/>
<module name="parametername"/>
<module name="staticvariablename"/>
<module name="typename"/>
<!-- checks for headers -->
<!-- 檢查檔案是否以指定檔案開始,這裡最好是放一些版權資訊和工程描述 -->
<!-- headerfile:指定的檔案 -->
<!-- ignorelines:忽略哪些行,以","分隔 -->
<module name="header">
<property name="headerfile" value="java.header"/>
<property name="ignorelines" value="2, 3, 4, 5"/>
<!-- following interprets the header file as regular expressions. -->
<module name="regexpheader"/>
<!-- checks for imports -->
<!-- 檢查使用*号的導入,預設為全部類 -->
<module name="avoidstarimport"/>
<!-- 檢查是否有非法的包,确省檢查sun.*;對于某些包是不建議直接調用的 -->
<module name="illegalimport">
<property name="illegalpkgs" value="sun.*"/>
<!-- 檢查多于的導入,如一個類導入了多次 -->
<module name="redundantimport"/>
<!-- 檢查沒有使用的導入 -->
<module name="unusedimports"/>
<!-- 導入排序 -->
<!-- groups:分組,哪些是一組的 -->
<!-- ordered:同一個組内是否排序,true排序,确省為true -->
<!-- separated:各個組之間是否需要用空行分隔,确省為false -->
<!-- casesensitive:是否是大小寫敏感的,确省是 -->
<module name="importorder">
<property name="groups" value="java,javax"/>
<property name="ordered" value="true"/>
<property name="separated" value="true"/>
<property name="casesensitive" value="true"/>
<!-- checks for size violations. -->
<!-- 檢查方法内可執行語句的個數,确省為30行 -->
<module name="executablestatementcount">
<property name="max" value="30"/>
<!-- 檔案的最大行數,預設為1500 -->
<module name="filelength">
<property name="max" value="2000"/>
<!-- 每行的最大字元數,預設為80 -->
<module name="linelength">
<!-- 忽略指定格式的行,如*号開始的,等 -->
<property name="ignorepattern" value="^ *\* *[^ ]+$"/>
<property name="max" value="120"/>
<!-- 方法的最大行數,預設為150 -->
<module name="methodlength">
<property name="max" value="200"/>
<!-- 統計時是否包括空行和以//開始的注釋,預設為統計(true)-->
<property name="countempty" value="false"/>
<!-- 匿名類的最大行數,預設為20 -->
<module name="anoninnerlength">
<property name="max" value="60"/>
<!-- 檢查方法和構造子參數的最大個數,預設為7 -->
<module name="parametern
關注此文的讀者還看過:
·2013-8-13 8:55:48 使用java将word轉為html或txt!
·2013-8-13 8:55:48 精通hibernate之映射繼承關系七(圖)
·2013-8-13 8:55:47 根據name調用getname方法
·2013-8-13 8:55:46 java gui誤解 因為外觀而拒絕swing?
·2013-8-13 8:55:38 java的字元串操作
·2013-8-13 8:55:36 java的檔案與磁盤操作
資料引用:http://www.knowsky.com/367856.html