前段時(shí)間研究了一下SUN的編碼規(guī)范,公司在經(jīng)過(guò)再三考慮之后,決定在我們項(xiàng)目組試用。
      以下是我以過(guò)對(duì)SUN的CheckStyle進(jìn)行學(xué)習(xí)后修正的XML文檔,在eclipse中導(dǎo)入后即可使用。下面中以<module>開關(guān)的,被我注釋掉的部分為我們修正的部分。
      相關(guān)的詳細(xì)信息,可以參看下面每個(gè)module對(duì)應(yīng)的url。
      <?xml version="1.0"?>
<!DOCTYPE module PUBLIC
    "-//Puppy Crawl//DTD Check Configuration 1.2//EN"
    "

<module name="Checker">
 <property name="severity" value="warning"/>

    <!-- Checks that a package.html file exists for each package.     -->
    <!-- See
http://checkstyle.sf.net/config_javadoc.html#PackageHtml -->
    <module name="PackageHtml"/>

    <!-- Checks whether files end with a new line.                        -->
    <!-- See http://checkstyle.sf.net/config_misc.html#NewlineAtEndOfFile -->
    <!--<module name="NewlineAtEndOfFile"/>-->

    <!-- Checks that property files contain the same keys.         -->
    <!-- See http://checkstyle.sf.net/config_misc.html#Translation -->
    <!--<module name="Translation"/>-->


    <module name="TreeWalker">

        <!-- Checks for Javadoc comments.                     -->
        <!-- See http://checkstyle.sf.net/config_javadoc.html -->
        <module name="JavadocMethod"/>
        <module name="JavadocType"/>
        <module name="JavadocVariable"/>
        <module name="JavadocStyle"/>


        <!-- Checks for Naming Conventions.                  -->
        <!-- See http://checkstyle.sf.net/config_naming.html -->
  <module name="ConstantName"/>
        <module name="LocalFinalVariableName">
   <property name="format" value="^[a-z][a-zA-Z0-9]*(_[a-zA-Z0-9]+)*$"/>
  </module>
        <module name="LocalVariableName">
   <property name="format" value="^[a-z][a-zA-Z0-9]*(_[a-zA-Z0-9]+)*$"/>
  </module>
        <module name="MemberName">
   <property name="format" value="^[a-z][a-zA-Z0-9]*(_[a-zA-Z0-9]+)*$"/>
  </module>
        <module name="MethodName"/>
        <module name="PackageName"/>
        <module name="ParameterName"/>
        <module name="StaticVariableName">
   <property name="format" value="^[a-z][a-zA-Z0-9]*(_[a-zA-Z0-9]+)*$"/>
  </module>
        <module name="TypeName"/>


        <!-- Checks for Headers                                -->
        <!-- See http://checkstyle.sf.net/config_header.html   -->
        <!-- <module name="Header">                            -->
            <!-- The follow property value demonstrates the ability     -->
            <!-- to have access to ANT properties. In this case it uses -->
            <!-- the ${basedir} property to allow Checkstyle to be run  -->
            <!-- from any directory within a project. See property      -->
            <!-- expansion,                                             -->
            <!-- http://checkstyle.sf.net/config.html#properties        -->
            <!-- <property                                              -->
            <!--     name="headerFile"                                  -->
            <!--     value="${basedir}/java.header"/>                   -->
        <!-- </module> -->

        <!-- Following interprets the header file as regular expressions. -->
        <!-- <module name="RegexpHeader"/>                                -->


        <!-- Checks for imports                              -->
        <!-- See http://checkstyle.sf.net/config_import.html -->
        <module name="AvoidStarImport"/>
        <!--<module name="IllegalImport"/> --><!-- defaults to sun.* packages -->
        <module name="RedundantImport"/>
        <module name="UnusedImports"/>


        <!-- Checks for Size Violations.                    -->
        <!-- See http://checkstyle.sf.net/config_sizes.html -->
        <module name="FileLength"/>
        <!--<module name="LineLength"/>-->
  <module name="LineLength">
   <property name="max" value="120"/>
  </module>
        <module name="MethodLength"/>
        <!--<module name="ParameterNumber"/>-->


        <!-- Checks for whitespace                               -->
        <!-- See http://checkstyle.sf.net/config_whitespace.html -->
        <!--<module name="EmptyForIteratorPad"/>-->
        <module name="MethodParamPad"/>
        <module name="NoWhitespaceAfter"/>
        <module name="NoWhitespaceBefore"/>
        <module name="OperatorWrap"/>
        <module name="ParenPad"/>
        <module name="TypecastParenPad"/>
        <!--<module name="TabCharacter"/>-->
        <module name="WhitespaceAfter"/>
        <module name="WhitespaceAround"/>


        <!-- Modifier Checks                                    -->
        <!-- See http://checkstyle.sf.net/config_modifiers.html -->
        <module name="ModifierOrder"/>
        <module name="RedundantModifier"/>


        <!-- Checks for blocks. You know, those {}'s         -->
        <!-- See http://checkstyle.sf.net/config_blocks.html -->
        <module name="AvoidNestedBlocks"/>
        <module name="EmptyBlock"/>
        <module name="LeftCurly"/>
        <module name="NeedBraces"/>
        <module name="RightCurly"/>


        <!-- Checks for common coding problems               -->
        <!-- See http://checkstyle.sf.net/config_coding.html -->
        <module name="AvoidInlineConditionals"/>
        <module name="DoubleCheckedLocking"/>    <!-- MY FAVOURITE -->
        <module name="EmptyStatement"/>
        <module name="EqualsHashCode"/>
        <!--<module name="HiddenField"/>-->
        <module name="IllegalInstantiation"/>
        <module name="InnerAssignment"/>
        <!--<module name="MagicNumber"/>-->
        <module name="MissingSwitchDefault"/>
        <module name="RedundantThrows"/>
        <module name="SimplifyBooleanExpression"/>
        <module name="SimplifyBooleanReturn"/>

        <!-- Checks for class design                         -->
        <!-- See http://checkstyle.sf.net/config_design.html -->
        <!--<module name="DesignForExtension"/>-->
        <module name="FinalClass"/>
       <!-- <module name="HideUtilityClassConstructor"/>-->
        <!--<module name="InterfaceIsType"/>-->
        <module name="VisibilityModifier"/>


        <!-- Miscellaneous other checks.                   -->
        <!-- See http://checkstyle.sf.net/config_misc.html -->
        <module name="ArrayTypeStyle"/>
        <!--<module name="FinalParameters"/>-->
        <module name="GenericIllegalRegexp">
            <property name="format" value="\s+$"/>
            <property name="message" value="Line has trailing spaces."/>
        </module>
        <!--<module name="TodoComment"/>
        <module name="UpperEll"/>-->

    </module>

</module>

下面這個(gè)XML文件,與上面的一樣,不過(guò)加了些中文注釋,如下:
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
    "-//Puppy Crawl//DTD Check Configuration 1.2//EN"
    "

<!--

  Checkstyle configuration that checks the sun coding conventions from:

    - the Java Language Specification at
     
http://java.sun.com/docs/books/jls/second_edition/html/index.html

    - the Sun Code Conventions at http://java.sun.com/docs/codeconv/

    - the Javadoc guidelines at
      http://java.sun.com/j2se/javadoc/writingdoccomments/index.html

    - the JDK Api documentation http://java.sun.com/j2se/docs/api/index.html

    - some best practices

  Checkstyle is very configurable. Be sure to read the documentation at
  http://checkstyle.sf.net (or in your downloaded distribution).

  Most Checks are configurable, be sure to consult the documentation.

  To completely disable a check, just comment it out or delete it from the file.

  Finally, it is worth reading the documentation.

-->

<module name="Checker">
 <property name="severity" value="warning"/>

    <!-- Checks that a package.html file exists for each package.     -->
    <!-- See http://checkstyle.sf.net/config_javadoc.html#PackageHtml
 檢查是否有一個(gè)兄弟html文件-->
    <module name="PackageHtml"/>

    <!-- Checks whether files end with a new line.                        -->
    <!-- See http://checkstyle.sf.net/config_misc.html#NewlineAtEndOfFile
 檢查文件是否以新的一行結(jié)尾-->
    <!--<module name="NewlineAtEndOfFile"/>-->

    <!-- Checks that property files contain the same keys.         -->
    <!-- See http://checkstyle.sf.net/config_misc.html#Translation
 檢查資源文件是否一致-->
    <!--<module name="Translation"/>-->


    <module name="TreeWalker">

        <!-- Checks for Javadoc comments.                     -->
        <!-- See http://checkstyle.sf.net/config_javadoc.html
  檢查方法,類和接口,變量是否有javadoc,
  javadocStyle是檢查以上的javadoc是否規(guī)范,如以.結(jié)尾,有@param,@returnt等-->
        <module name="JavadocMethod"/>
        <module name="JavadocType"/>
        <module name="JavadocVariable"/>
        <module name="JavadocStyle"/>


        <!-- Checks for Naming Conventions.                  -->
        <!-- See http://checkstyle.sf.net/config_naming.html
  檢查變量是否符合規(guī)范(正則表達(dá)示),在全局上可以使用,但是可以變通,有warn也不用修改-->
        <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                                -->
        <!-- See http://checkstyle.sf.net/config_header.html   -->
        <!-- <module name="Header">                            -->
            <!-- The follow property value demonstrates the ability     -->
            <!-- to have access to ANT properties. In this case it uses -->
            <!-- the ${basedir} property to allow Checkstyle to be run  -->
            <!-- from any directory within a project. See property      -->
            <!-- expansion,                                             -->
            <!-- http://checkstyle.sf.net/config.html#properties        -->
            <!-- <property                                              -->
            <!--     name="headerFile"                                  -->
            <!--     value="${basedir}/java.header"/>                   -->
        <!-- </module> -->

        <!-- Following interprets the header file as regular expressions. -->
        <!-- <module name="RegexpHeader"/>                                -->


        <!-- Checks for imports                              -->
        <!-- See http://checkstyle.sf.net/config_import.html
  對(duì)導(dǎo)入的包進(jìn)行檢查。有沒(méi)有帶*的,無(wú)效的包(默認(rèn)sun.*),
  多余的導(dǎo)入(重復(fù)導(dǎo)入,導(dǎo)入的包包含在java.lang中,比如導(dǎo)入java.lang.String,類從同一個(gè)包中導(dǎo)入),
  沒(méi)有使用的導(dǎo)入-->
        <module name="AvoidStarImport"/>
        <!--<module name="IllegalImport"/> --><!-- defaults to sun.* packages -->
        <module name="RedundantImport"/>
        <module name="UnusedImports"/>


        <!-- Checks for Size Violations.                    -->
        <!-- See http://checkstyle.sf.net/config_sizes.html
  檢查長(zhǎng)度。文件長(zhǎng)度(默認(rèn)2000),行長(zhǎng)度(默認(rèn)80),方法長(zhǎng)度(默認(rèn)150),參數(shù)的數(shù)量(默認(rèn)7)-->
        <module name="FileLength"/>
        <module name="LineLength"/>
        <module name="MethodLength"/>
        <!--<module name="ParameterNumber"/>-->


        <!-- Checks for whitespace                               -->
        <!-- See http://checkstyle.sf.net/config_whitespace.html
  檢查空格,默認(rèn)的均為false,不用修改。不用太關(guān)心。用jalopy美化工具后,就可以了-->
        <!--<module name="EmptyForIteratorPad"/>-->
        <module name="MethodParamPad"/>
        <module name="NoWhitespaceAfter"/>
        <module name="NoWhitespaceBefore"/>
        <module name="OperatorWrap"/>
        <module name="ParenPad"/>
        <module name="TypecastParenPad"/>
        <!--<module name="TabCharacter"/>-->
        <module name="WhitespaceAfter"/>
        <module name="WhitespaceAround"/>


        <!-- Modifier Checks                                    -->
        <!-- See http://checkstyle.sf.net/config_modifiers.html
  檢查修飾符,是否按照順序(1. public 2. protected 3. private 4. abstract 5. static 6. final 7. transient 8. volatile 9. synchronized 10. native 11. strictfp ),
  是否在類型定義中有多余的修飾符,例:interface中,方法均不public,就不用在方法前寫明public了-->
        <module name="ModifierOrder"/>
        <module name="RedundantModifier"/>


        <!-- Checks for blocks. You know, those {}'s         -->
        <!-- See http://checkstyle.sf.net/config_blocks.html
  檢查語(yǔ)句塊。嵌套的語(yǔ)句塊,是否有空的語(yǔ)句塊,程序塊(for,else等)左括號(hào)的位置,
  程序塊兩端的括號(hào),程序塊(for,else等)右括號(hào)的位置-->
        <module name="AvoidNestedBlocks"/>
        <module name="EmptyBlock"/>
        <module name="LeftCurly"/>
        <module name="NeedBraces"/>
        <module name="RightCurly"/>


        <!-- Checks for common coding problems               -->
        <!-- See http://checkstyle.sf.net/config_coding.html
  檢查同一行中的條件語(yǔ)句,盡量不要用String b = (a==null || a.length<1) ? null : a.substring(1);
  雙重檢測(cè)鎖定,避免同步時(shí)運(yùn)行開銷
  檢測(cè)空語(yǔ)句(單獨(dú)的分號(hào);)
  檢查類覆蓋了equals()方法和hashCode()方法,若覆蓋了equals,則要覆蓋hashCode
  檢查本地變量或者參數(shù)是否要隱藏在同一個(gè)類的字段當(dāng)中,容易在參數(shù)中出現(xiàn)問(wèn)題,所以去掉
  檢查非法的實(shí)例化,當(dāng)選用工廠方法的時(shí)候
  檢查子表達(dá)式,例如在String s=Integer.toString(i=2)當(dāng)中的賦值,要在語(yǔ)句前賦值
  檢查沒(méi)有”魔數(shù)”出現(xiàn),所有的數(shù)字均要定義為常量,所以去掉。-1,0,1,和2不被認(rèn)為是”魔數(shù)“
  檢查switch語(yǔ)句有default子句
  檢查多余的在throws語(yǔ)句當(dāng)中異常聲明,比如重復(fù)的沒(méi)有檢查的的異常或者一個(gè)已經(jīng)聲明的異常的子類.
  檢查過(guò)于復(fù)雜的boolean表達(dá)式,if (b == true), b || true, !false, 等等
  檢查過(guò)于復(fù)雜的boolean返回語(yǔ)句if (valid())  return false;else return true;修改為return !valid();-->
        <module name="AvoidInlineConditionals"/>
        <module name="DoubleCheckedLocking"/>    <!-- MY FAVOURITE -->
        <module name="EmptyStatement"/>
        <module name="EqualsHashCode"/>
        <!--<module name="HiddenField"/>-->
        <module name="IllegalInstantiation"/>
        <module name="InnerAssignment"/>
        <!--<module name="MagicNumber"/>-->
        <module name="MissingSwitchDefault"/>
        <module name="RedundantThrows"/>
        <module name="SimplifyBooleanExpression"/>
        <module name="SimplifyBooleanReturn"/>

        <!-- Checks for class design                         -->
        <!-- See http://checkstyle.sf.net/config_design.html
  檢查類設(shè)計(jì)
  檢查類的擴(kuò)展性,父類的空方法,要可以被子類實(shí)現(xiàn),但是有的類,是可以自己寫一些實(shí)現(xiàn)的,并不需要全部實(shí)現(xiàn)
  檢查一個(gè)有私有構(gòu)造方法的類,要為final的
  確認(rèn)應(yīng)用工具類(類僅僅包含靜態(tài)的方法)沒(méi)有一個(gè)公有的創(chuàng)建方法
  實(shí)現(xiàn)Bloch式的,根據(jù)Bloch的原則,一個(gè)接口應(yīng)該定義為一個(gè)類型.因此如果定義一個(gè)接口里面不包含方法而僅僅包含常量是不合適的檢查類成員的可視性,基本原理就是封裝-->
        <!--<module name="DesignForExtension"/>-->
        <module name="FinalClass"/>
        <module name="HideUtilityClassConstructor"/>
        <!--<module name="InterfaceIsType"/>-->
        <module name="VisibilityModifier"/>


        <!-- Miscellaneous other checks.                   -->
        <!-- See http://checkstyle.sf.net/config_misc.html
  檢查組類型變量的聲明,要使用java型的,int[] someArray,不要使用C型的,int someArray[]
  要method,構(gòu)造方法和catch中的參數(shù)為fianl的,不能滿足
  給定義的屬性的正則表達(dá)示加信息,如果沒(méi)有,提示message
  todo注釋,現(xiàn)在沒(méi)有需要。讓人忘記想要記住的東西,
  定義一個(gè)常量,均要用大寫,沒(méi)有必要-->
        <module name="ArrayTypeStyle"/>
        <!--<module name="FinalParameters"/>-->
        <module name="GenericIllegalRegexp">
            <property name="format" value="\s+$"/>
            <property name="message" value="Line has trailing spaces."/>
        </module>
        <!--<module name="TodoComment"/>
        <module name="UpperEll"/>-->

    </module>

</module>