前段時間研究了一下SUN的編碼規范,公司在經過再三考慮之后,決定在我們項目組試用。
      以下是我以過對SUN的CheckStyle進行學習后修正的XML文檔,在eclipse中導入后即可使用。下面中以<module>開關的,被我注釋掉的部分為我們修正的部分。
      相關的詳細信息,可以參看下面每個module對應的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>

下面這個XML文件,與上面的一樣,不過加了些中文注釋,如下:
<?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
 檢查是否有一個兄弟html文件-->
    <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
  檢查方法,類和接口,變量是否有javadoc,
  javadocStyle是檢查以上的javadoc是否規范,如以.結尾,有@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
  檢查變量是否符合規范(正則表達示),在全局上可以使用,但是可以變通,有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
  對導入的包進行檢查。有沒有帶*的,無效的包(默認sun.*),
  多余的導入(重復導入,導入的包包含在java.lang中,比如導入java.lang.String,類從同一個包中導入),
  沒有使用的導入-->
        <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
  檢查長度。文件長度(默認2000),行長度(默認80),方法長度(默認150),參數的數量(默認7)-->
        <module name="FileLength"/>
        <module name="LineLength"/>
        <module name="MethodLength"/>
        <!--<module name="ParameterNumber"/>-->


        <!-- Checks for whitespace                               -->
        <!-- See http://checkstyle.sf.net/config_whitespace.html
  檢查空格,默認的均為false,不用修改。不用太關心。用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
  檢查語句塊。嵌套的語句塊,是否有空的語句塊,程序塊(for,else等)左括號的位置,
  程序塊兩端的括號,程序塊(for,else等)右括號的位置-->
        <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
  檢查同一行中的條件語句,盡量不要用String b = (a==null || a.length<1) ? null : a.substring(1);
  雙重檢測鎖定,避免同步時運行開銷
  檢測空語句(單獨的分號;)
  檢查類覆蓋了equals()方法和hashCode()方法,若覆蓋了equals,則要覆蓋hashCode
  檢查本地變量或者參數是否要隱藏在同一個類的字段當中,容易在參數中出現問題,所以去掉
  檢查非法的實例化,當選用工廠方法的時候
  檢查子表達式,例如在String s=Integer.toString(i=2)當中的賦值,要在語句前賦值
  檢查沒有”魔數”出現,所有的數字均要定義為常量,所以去掉。-1,0,1,和2不被認為是”魔數“
  檢查switch語句有default子句
  檢查多余的在throws語句當中異常聲明,比如重復的沒有檢查的的異?;蛘咭粋€已經聲明的異常的子類.
  檢查過于復雜的boolean表達式,if (b == true), b || true, !false, 等等
  檢查過于復雜的boolean返回語句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
  檢查類設計
  檢查類的擴展性,父類的空方法,要可以被子類實現,但是有的類,是可以自己寫一些實現的,并不需要全部實現
  檢查一個有私有構造方法的類,要為final的
  確認應用工具類(類僅僅包含靜態的方法)沒有一個公有的創建方法
  實現Bloch式的,根據Bloch的原則,一個接口應該定義為一個類型.因此如果定義一個接口里面不包含方法而僅僅包含常量是不合適的檢查類成員的可視性,基本原理就是封裝-->
        <!--<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,構造方法和catch中的參數為fianl的,不能滿足
  給定義的屬性的正則表達示加信息,如果沒有,提示message
  todo注釋,現在沒有需要。讓人忘記想要記住的東西,
  定義一個常量,均要用大寫,沒有必要-->
        <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>