分享

checkstyle rule模板

 bananarlily 2014-07-10


下面是自己在项目是使用的checkstyle rule的模板。适用于checkstyle 5.0+

因为项目是Swing的关系,所以允许了很多本应该不能忽略的检查,比如“magicNumber”,“参数个数不能超过3个”等。

如果是非swing程序,这些约束建议都放开。

 

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE module PUBLIC "-//Puppy Crawl//DTD Check Configuration 1.2//EN" "http://www./dtds/configuration_1_2.dtd">  
  3. <module name="Checker">  
  4.   
  5.       
  6.     <!--  
  7.     重复代码的检查,超过30行就认为重复,UTF-8格式 本检查一定要放在"TreeWalker"节点前,否则在  
  8.     Checkclipse中会无法使用。(在ant下可以)  
  9.     -->  
  10.     <module name="StrictDuplicateCode">  
  11.         <property name="min" value="30" />  
  12.     </module>  
  13.       
  14.     <!-- 文件长度不超过1500行 -->  
  15.     <module name="FileLength">  
  16.         <property name="max" value="1500" />  
  17.     </module>  
  18.   
  19.     <module name="TreeWalker">  
  20.   
  21.         <!-- javadoc的检查 -->  
  22.         <!-- 检查所有的interface和class -->  
  23.         <module name="JavadocType" />  
  24.   
  25.         <!-- 命名方面的检查,它们都使用了Sun官方定的规则。 -->  
  26.         <!-- 局部的final变量,包括catch中的参数的检查 -->  
  27.         <module name="LocalFinalVariableName" />  
  28.         <!-- 局部的非final型的变量,包括catch中的参数的检查 -->  
  29.         <module name="LocalVariableName" />  
  30.         <!-- 包名的检查(只允许小写字母) -->  
  31.         <module name="PackageName">  
  32.             <property name="format" value="^[a-z]+(\.[a-z][a-z0-9]*)*$" />  
  33.         </module>  
  34.         <!-- 仅仅是static型的变量(不包括static final型)的检查 -->  
  35.         <module name="StaticVariableName" />  
  36.         <!-- 类型(Class或Interface)名的检查 -->  
  37.         <module name="TypeName" />  
  38.         <!-- 非static型变量的检查 -->  
  39.         <module name="MemberName" />  
  40.         <!-- 方法名的检查 -->  
  41.         <module name="MethodName" />  
  42.         <!-- 方法的参数名 -->  
  43.         <module name="ParameterName " />  
  44.         <!-- 常量名的检查,忽略log -->  
  45.     <module name="ConstantName">  
  46.         <!-- Allow "log" as a constant - don't force LOG -->  
  47.         <property name="format" value="^log$|^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$"/>  
  48.     </module>  
  49.   
  50.         <!-- import方面的检查 -->  
  51.         <!-- import中避免星号"*" -->  
  52.         <module name="AvoidStarImport" />  
  53.         <!--  
  54.             没用的import检查,比如:1.没有被用到2.重复的3.import java.lang的4.import  
  55.             与该类在同一个package的  
  56.         -->  
  57.         <module name="UnusedImports" />  
  58.   
  59.   
  60.         <!-- 长度方面的检查, 不知道为什么下面这个忽略@see的不起作用,所以先注释掉 -->  
  61.         <!-- <module name="LineLength">   
  62.             <property name="max" value="120"/> -->  
  63.             <!-- ignore rows starting with a * @see and long word-->  
  64.             <!-- <property name="ignorePattern" value="^ *\* \@see *[^ ]+$"/>    
  65.         </module>  -->  
  66.   
  67.         <!-- 方法不超过100行 -->  
  68.         <module name="MethodLength">  
  69.             <property name="tokens" value="METHOD_DEF" />  
  70.             <property name="max" value="100" />  
  71.         </module>  
  72.         <!-- 方法的参数个数不超过6个。只所有有这么多参数,是因为Swing里面有可能就有这么长-->  
  73.         <module name="ParameterNumber">  
  74.             <property name="max" value="6" />  
  75.             <property name="tokens" value="METHOD_DEF, CTOR_DEF" />  
  76.         </module>  
  77.   
  78.         <!-- 空格检查  -->  
  79.         <!-- 允许方法名后紧跟左边圆括号"(" -->  
  80.         <module name="MethodParamPad" />  
  81.         <!-- 在类型转换时,不允许左圆括号右边有空格,也不允许与右圆括号左边有空格 -->  
  82.         <module name="TypecastParenPad" />  
  83.   
  84.         <!-- 关键字 -->  
  85.         <!--  
  86.             每个关键字都有正确的出现顺序。比如 public static final XXX 是对一个常量的声明。如果使用 static  
  87.             public final 就是错误的  
  88.         -->  
  89.         <module name="ModifierOrder" />  
  90.   
  91.         <!-- 对区域的检查 -->  
  92.         <!-- 不能出现空白区域 -->  
  93.         <module name="EmptyBlock" />  
  94.         <!-- 所有区域都要使用大括号。 -->  
  95.         <module name="NeedBraces" />  
  96.         <!-- 多余的括号 -->  
  97.         <module name="AvoidNestedBlocks">  
  98.             <property name="allowInSwitchCase" value="true" />  
  99.         </module>  
  100.   
  101.         <!-- 编码方面的检查 -->  
  102.         <!-- 不许出现空语句 -->  
  103.         <module name="EmptyStatement" />  
  104.         <!-- 每个类都实现了equals()和hashCode() -->  
  105.         <module name="EqualsHashCode" />  
  106.         <!-- 不许内部赋值 -->  
  107.         <module name="InnerAssignment" />  
  108.         <!-- 不能容忍魔法数,这里不检查了,因为Swing布局里面确实需要int,比如new Insets(5, 0, 5, 5);还有double,比如new GridBagConstraints(0, 0, 1, 1, 0.1, 0.0 -->  
  109.         <!--<module name="MagicNumber">  
  110.             <property name="tokens" value="NUM_DOUBLE, NUM_FLOAT" />  
  111.             <property name="ignoreNumbers" value="-1, 0, 0.5, 1"/>  
  112.             <property name="ignoreHashCodeMethod" value="true"/>  
  113.             <property name="ignoreAnnotation" value="true"/>  
  114.         </module>-->  
  115.         <!-- 循环控制变量不能被修改 -->  
  116.         <module name="ModifiedControlVariable" />  
  117.         <!-- 多余的throw -->  
  118.         <module name="RedundantThrows" />  
  119.         <!-- String的比较不能用!= 和 == -->  
  120.         <module name="StringLiteralEquality" />  
  121.         <!-- if最多嵌套3层 -->  
  122.         <module name="NestedIfDepth">  
  123.             <property name="max" value="3" />  
  124.         </module>  
  125.         <!-- try最多被嵌套2层 -->  
  126.         <module name="NestedTryDepth">  
  127.             <property name="max" value="2" />  
  128.         </module>  
  129.         <!-- clone方法必须调用了super.clone() -->  
  130.         <module name="SuperClone" />  
  131.         <!-- finalize 必须调用了super.finalize() -->  
  132.         <module name="SuperFinalize" />  
  133.         <!-- 确保一个类有package声明 -->  
  134.         <module name="PackageDeclaration" />  
  135.   
  136.         <!--  
  137.             根据 Sun 编码规范, class 或 interface 中的顺序如下: 1.class 声明。首先是 public,  
  138.             然后是protected , 然后是 package level (不包括access modifier ) 最后是private .  
  139.             (多个class放在一个java文件中的情况) 2.变量声明。 首先是 public, 然后是protected然后是 package  
  140.             level (不包括access modifier ) 最后是private . (多个class放在一个java文件中的情况)  
  141.             3.构造函数 4.方法  
  142.         -->  
  143.         <module name="DeclarationOrder" />  
  144.         <!-- 不许对方法的参数赋值 -->  
  145.         <module name="ParameterAssignment" />  
  146.         <!-- 确保某个class 在被使用时都已经被初始化成默认值(对象是null,数字和字符是0,boolean 变量是false.) -->  
  147.         <module name="ExplicitInitialization" />  
  148.         <!-- 不许有同样内容的String,最多出现2次,忽略:空字符串 | "," | "(" | ")"-->  
  149.         <module name="MultipleStringLiterals">  
  150.             <property name="allowedDuplicates" value="2"/>  
  151.             <property name="ignoreStringsRegexp" value='^(("")|(", ")|("\("|"\)"))$'/>  
  152.         </module>  
  153.         <!-- 同一行不能有多个声明 -->  
  154.         <module name="MultipleVariableDeclarations" />  
  155.         <!-- 不必要的圆括号(Unnecessary parentheses around identifier) -->  
  156.         <module name="UnnecessaryParentheses" />  
  157.   
  158.         <!-- 各种量度 -->  
  159.         <!-- 布尔表达式的复杂度,不超过3 -->  
  160.         <module name="BooleanExpressionComplexity" />  
  161.         <!-- 类数据的抽象耦合,不超过7。注释掉的原因是因为,在写Swing代码的时候确实有这么耦合 -->  
  162.         <!-- <module name="ClassDataAbstractionCoupling" /> -->  
  163.         <!-- 类的分散复杂度,不超过20。 注释掉的原因是因为,在写Swing代码的时候确实有这么扇出 -->  
  164.         <!-- <module name="ClassFanOutComplexity" />  -->  
  165.         <!-- 函数的分支复杂度,不超过10 -->  
  166.         <module name="CyclomaticComplexity" />  
  167.         <!-- NPath复杂度(NPath Complexity),不超过200 -->  
  168.         <module name="NPathComplexity" />  
  169.   
  170.         <!-- 杂项 -->  
  171.         <!-- 禁止使用System.out.println -->  
  172.         <module name="Regexp">  
  173.             <property name="format" value="System\.out\.println" />  
  174.             <property name="illegalPattern" value="true"/>  
  175.             <property name="ignoreComments" value="true" />  
  176.         </module>  
  177.           
  178.         <!-- 不许使用main方法 -->  
  179.         <module name="UncommentedMain" />  
  180.         <!-- 检查并确保所有的常量中的L都是大写的。因为小写的字母l跟数字1太象了 -->  
  181.         <module name="UpperEll" />  
  182.         <!-- 检查数组类型的定义是String[] args,而不是String args[] -->  
  183.         <module name="ArrayTypeStyle" />  
  184.   
  185.     </module>  
  186.       
  187.   
  188.     <!-- 检查翻译文件     -->  
  189.     <module name="Translation" />  
  190. </module>  


因为报告默认生成的xml,要生成html,必须得要扩展样式表文件 checkstyle.xls

  1. <xsl:stylesheet  xmlns:xsl="http://www./1999/XSL/Transform" version="1.0">  
  2. <xsl:output method="html" indent="yes"/>  
  3. <xsl:decimal-format decimal-separator="." grouping-separator="," />  
  4.   
  5. <!-- Checkstyle XML Style Sheet by Stephane Bailliez <sbailliez@>         -->  
  6. <!-- Part of the Checkstyle distribution found at http://checkstyle. -->  
  7. <!-- Usage (generates checkstyle_report.html):                                      -->  
  8. <!--    <checkstyle failonviolation="false" config="${check.config}">               -->  
  9. <!--      <fileset dir="${src.dir}" includes="**/*.java"/>                          -->  
  10. <!--      <formatter type="xml" toFile="${doc.dir}/checkstyle_report.xml"/>         -->  
  11. <!--    </checkstyle>                                                               -->  
  12. <!--    <style basedir="${doc.dir}" destdir="${doc.dir}"                            -->  
  13. <!--            includes="checkstyle_report.xml"                                    -->  
  14. <!--            style="${doc.dir}/checkstyle-noframes-sorted.xsl"/>                 -->  
  15.   
  16. <xsl:template match="checkstyle">  
  17.     <html>  
  18.         <head>  
  19.         <style type="text/css">  
  20.     .bannercell {  
  21.       border: 0px;  
  22.       padding: 0px;  
  23.     }  
  24.     body {  
  25.       margin-left: 10;  
  26.       margin-right: 10;  
  27.       font:normal 80% arial,helvetica,sanserif;  
  28.       background-color:#FFFFFF;  
  29.       color:#000000;  
  30.     }  
  31.     .a td {   
  32.       background: #efefef;  
  33.     }  
  34.     .b td {   
  35.       background: #fff;  
  36.     }  
  37.     th, td {  
  38.       text-align: left;  
  39.       vertical-align: top;  
  40.     }  
  41.     th {  
  42.       font-weight:bold;  
  43.       background: #ccc;  
  44.       color: black;  
  45.     }  
  46.     table, th, td {  
  47.       font-size:100%;  
  48.       border: none  
  49.     }  
  50.     table.log tr td, tr th {  
  51.         
  52.     }  
  53.     h2 {  
  54.       font-weight:bold;  
  55.       font-size:140%;  
  56.       margin-bottom: 5;  
  57.     }  
  58.     h3 {  
  59.       font-size:100%;  
  60.       font-weight:bold;  
  61.       background: #525D76;  
  62.       color: white;  
  63.       text-decoration: none;  
  64.       padding: 5px;  
  65.       margin-right: 2px;  
  66.       margin-left: 2px;  
  67.       margin-bottom: 0;  
  68.     }  
  69.         </style>  
  70.         </head>  
  71.         <body>  
  72.             <a name="top"></a>  
  73.       <!-- jakarta logo -->  
  74.       <table border="0" cellpadding="0" cellspacing="0" width="100%">  
  75.       <tr>  
  76.         <td class="bannercell" rowspan="2">  
  77.           <!--a href="http://jakarta./">  
  78.           <img src="http://jakarta./images/jakarta-logo.gif" alt="http://jakarta." align="left" border="0"/>  
  79.           </a-->  
  80.         </td>  
  81.             <td class="text-align:right"><h2>CheckStyle Audit</h2></td>  
  82.             </tr>  
  83.             <tr>  
  84.             <td class="text-align:right">Designed for use with <a href='http://checkstyle./'>CheckStyle</a> and <a href='http://jakarta.'>Ant</a>.</td>  
  85.             </tr>  
  86.       </table>  
  87.         <hr size="1"/>  
  88.               
  89.             <!-- Summary part -->  
  90.             <xsl:apply-templates select="." mode="summary"/>  
  91.             <hr size="1" width="100%" align="left"/>  
  92.               
  93.             <!-- Package List part -->  
  94.             <xsl:apply-templates select="." mode="filelist"/>  
  95.             <hr size="1" width="100%" align="left"/>  
  96.               
  97.             <!-- For each package create its part -->  
  98.             <xsl:for-each select="file">  
  99.               <xsl:sort select="@name"/>  
  100.               <xsl:apply-templates select="."/>  
  101.               <p/>  
  102.               <p/>  
  103.             </xsl:for-each>  
  104.             <hr size="1" width="100%" align="left"/>  
  105.               
  106.               
  107.         </body>  
  108.     </html>  
  109. </xsl:template>  
  110.       
  111.       
  112.       
  113.     <xsl:template match="checkstyle" mode="filelist">   
  114.         <h3>Files</h3>  
  115.         <table class="log" border="0" cellpadding="5" cellspacing="2" width="100%">  
  116.       <tr>  
  117.         <th>Name</th>  
  118.         <th>Errors</th>  
  119.       </tr>  
  120.             <xsl:for-each select="file">  
  121.                                 <xsl:sort data-type="number" order="descending" select="count(error)"/>  
  122.                 <xsl:variable name="errorCount" select="count(error)"/>                 
  123.                 <tr>  
  124.           <xsl:call-template name="alternated-row"/>  
  125.                     <td><a href="#f-{@name}"><xsl:value-of select="@name"/></a></td>  
  126.                     <td><xsl:value-of select="$errorCount"/></td>  
  127.                 </tr>  
  128.             </xsl:for-each>  
  129.         </table>        
  130.     </xsl:template>  
  131.       
  132.       
  133.     <xsl:template match="file">  
  134.     <a name="f-{@name}"></a>  
  135.     <h3>File <xsl:value-of select="@name"/></h3>  
  136.       
  137.     <table class="log" border="0" cellpadding="5" cellspacing="2" width="100%">  
  138.         <tr>  
  139.           <th>Error Description</th>  
  140.           <th>Line</th>  
  141.       </tr>  
  142.       <xsl:for-each select="error">  
  143.         <tr>  
  144.         <xsl:call-template name="alternated-row"/>  
  145.           <td><xsl:value-of select="@message"/></td>  
  146.           <td><xsl:value-of select="@line"/></td>  
  147.         </tr>  
  148.         </xsl:for-each>  
  149.     </table>  
  150.     <a href="#top">Back to top</a>  
  151.     </xsl:template>  
  152.       
  153.       
  154.     <xsl:template match="checkstyle" mode="summary">  
  155.         <h3>Summary</h3>  
  156.         <xsl:variable name="fileCount" select="count(file)"/>  
  157.         <xsl:variable name="errorCount" select="count(file/error)"/>  
  158.         <table class="log" border="0" cellpadding="5" cellspacing="2" width="100%">  
  159.         <tr>  
  160.             <th>Files</th>  
  161.             <th>Errors</th>  
  162.         </tr>  
  163.         <tr>  
  164.           <xsl:call-template name="alternated-row"/>  
  165.             <td><xsl:value-of select="$fileCount"/></td>  
  166.             <td><xsl:value-of select="$errorCount"/></td>  
  167.         </tr>  
  168.         </table>  
  169.     </xsl:template>  
  170.       
  171.   <xsl:template name="alternated-row">  
  172.     <xsl:attribute name="class">  
  173.       <xsl:if test="position() mod 2 = 1">a</xsl:if>  
  174.       <xsl:if test="position() mod 2 = 0">b</xsl:if>  
  175.     </xsl:attribute>    
  176.   </xsl:template>   
  177. </xsl:stylesheet>  


和ANT集成中的用法如下:

  1. <!-- 配置checkstyle路径 -->  
  2. <property name="checkstyle" location="${basedir}/checkstyle"/>  
  3. <property name="checkstyle.config" location="${checkstyle}/checkstyle_ezsoft.xml"/>  
  4. <property name="checkstyle.lib.classpath" location="${checkstyle}/checkstyle-5.6-all.jar"/>  
  5. <property name="checkstyle.html.style" location="${checkstyle}/checkstyle.xsl"/>  
  6. <property name="checkstyle.report" location="${prj}/buildtool/checkstyle_report"/>  
  7. <property name="checkstyle.report.xml" location="${checkstyle.report}/checkstyle.xml"/>  
  8. <property name="checkstyle.report.html" location="${checkstyle.report}/checkstyle.html"/>  
  9.   
  10.  <!-- ================================================================= -->  
  11.  <!-- 12.checkstyle -->  
  12.  <!-- ================================================================= -->  
  13. <target name="checkstyle"  
  14.         description="Generates a report of code convention violations.">  
  15.   <delete dir="${checkstyle.report}"/>  
  16.   <mkdir dir="${checkstyle.report}"/>  
  17.       
  18.   <taskdef resource="checkstyletask.properties" classpath="${checkstyle.lib.classpath}"/>      
  19.       
  20.   <checkstyle config="${checkstyle.config}"  
  21.               failureProperty="checkstyle.failure"  
  22.               failOnViolation="false">  
  23.     <formatter type="xml" tofile="${checkstyle.report.xml}"/>  
  24.     <fileset dir="${src_source}" includes="**/*.java"/>  
  25.   </checkstyle>  
  26.   
  27.   <style in="${checkstyle.report.xml}" out="${checkstyle.report.html}" style="${checkstyle.html.style}"/>  
  28.   
  29. </target>  


 

 

 

 

 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约