Difference between revisions of "CheckStyle"

From HaFrWiki
Jump to: navigation, search
(Created page with "{{TOCright}} == Installation in Maven == To set CheckStyle in Maven or {{Maven 2]] add the following line of code to the settings of the project.xml, project.properties. ...")
 
m (Pre-requirement)
 
(19 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
{{TOCright}}
 
{{TOCright}}
 +
'''Checkstyle''' <ref name="home">[http://checkstyle.sourceforge.net Sourceforge] CheckStyle home page.</ref> is a tool for checking Code Standards Violations. It can be used different environments:
 +
* Standalone (not covered here)
 +
* [[#Installation_in_Ant|Ant]]
 +
* [[#Installation_in_Maven|Maven]]
 +
* IDE
 +
 +
== Description ==
 +
Checkstyle is a development tool to help programmers write Java code that adheres to a coding standard. It automates the process of checking Java code to spare humans of this boring (but important) task. This makes it ideal for projects that want to enforce a coding standard.
 +
 +
Checkstyle is highly configurable and can be made to support almost any coding standard. An example configuration files are supplied supporting the Sun Code Conventions, Google Java Style.
 +
 +
For downloading the software see the homepage of CheckStyle on SourceForge <ref name="home"/>.
 +
 +
== Pre-requirements ==
 +
* All
 +
* Ant
 +
** Installed version of Apache Ant <ref name="ant">[[Ant]], Apache Ant information for installing and using Ant</ref>.
 +
** Dowloaded and installed standalone version. See the SourceForge Website of CheckStyle <ref name="home"/>.
 +
* Maven
 +
** Internet Connection for running Maven.
 +
** Installed version of Apache Maven <ref name="maven">[[Maven]], Apache Maven information for installing and using maven</ref>.
 +
 +
== Installation in Ant ==
 +
To set CheckStyle in [[Ant]] add the following line of code to the build.xml.
 +
<syntaxhighlight lang="xml" line>
 +
  <!-- The tools home-directories -->
 +
  <property name = "checkstyle.home.dir" location = "." />
 +
  ...
 +
  <!-- Reference to the checkstyle plugin 6.8.1. -->
 +
  <taskdef resource  = "com/puppycrawl/tools/checkstyle/ant/checkstyle-ant-task.properties" classpath = "checkstyle-6.8.1-all.jar" />
 +
  ...
 +
  <!-- Checkstyle properties -->
 +
  <property name="checkstyle.sourcepath"    value="src"                      />
 +
  <property name="checkstyle.report.path"  value="."                        />
 +
  <property name="checkstyle.report.style"  value="checkstyle-simple.xsl"    />
 +
  <property name="checkstyle.report.xml"    value="checkstyle-report.xml"    />
 +
  <property name="checkstyle.report.html"  value="checkstyle-report.html"  />
 +
  ...
 +
  <!-- ========================================================================= -->
 +
  <!-- CheckStyle report generation.                                            -->
 +
  <!-- ========================================================================= -->
 +
  <target name        = "check"
 +
          description = "Generates a report of code convention violations.">
 +
 +
      <!-- Creates the output directory -->
 +
      <mkdir dir  = "${checkstyle.report.path}" />
 +
 +
      <checkstyle config          = "${checkstyle.report.config}"
 +
                  failureProperty = "checkstyle.failure"
 +
                  failOnViolation = "false">
 +
        <formatter type  = "xml"
 +
                    tofile = "${checkstyle.report.xml}"/>
 +
        <fileset dir      = "${checkstyle.sourcepath}"
 +
                  includes = "**/*.java, **/*.xml, **/*.properties"
 +
                  excludes = "checkstyle/" />
 +
      </checkstyle>
 +
      <!-- Transformation XML-2-HTML -->
 +
      <xslt basedir = "${checkstyle.report.path}"
 +
            destdir = "${checkstyle.report.path}"
 +
            style  = "${checkstyle.report.style}" />
 +
  </target>
 +
</syntaxhighlight>
 +
{| class="wikitableharm" width="1100"
 +
! width="070" style="text-align:center;" | Line
 +
! width="630" style="text-align:left;"      | Description
 +
! width="200" style="text-align:left;"      | Default
 +
! width="200" style="text-align:left"      | Global
 +
|-
 +
| style="text-align:center;" | 1-2
 +
| Property for the location of all the checkstyle-jar files. It must contain checkstyle-6.8.1-all.jar (See below).
 +
| Current Directory
 +
| /usr/local/bin/checkstyle-6.8.1
 +
|-
 +
| style="text-align:center;" | 4-5
 +
| Reference to the checkstyle resource
 +
| colspan="2" style="text-align:center;" | checkstyle-6.8.1-all.jar
 +
|-
 +
| style="text-align:center;" | 7-12
 +
| colspan="3" | References to the checkstyle properties.
 +
|-
 +
| style="text-align:center;" | 14-36
 +
| colspan="3" | Ant-Target definition, creating the report path, running checkstyle and formats the resulting xml-report to HTML using XSLT.
 +
|}
 +
 
== Installation in Maven ==
 
== Installation in Maven ==
To set CheckStyle in [[Maven]] or {{Maven 2]] add the following line of code to the settings of the project.xml, project.properties.
+
To set CheckStyle in [[Maven]] or [[Maven 2]] add the following lines of code to the settings of the project.xml, project.properties.
  
 
=== Project.xml ===
 
=== Project.xml ===
<pre>
+
<syntaxhighlight lang="xml">
 
   <reports>
 
   <reports>
 
     ...
 
     ...
Line 10: Line 94:
 
     ...
 
     ...
 
   </reports>
 
   </reports>
</pre>
+
</syntaxhighlight>
 +
 
  
 
=== Project.properties ===
 
=== Project.properties ===
 
Add the name of the file containing the checkstyle settings:
 
Add the name of the file containing the checkstyle settings:
<pre>
+
<syntaxhighlight lang="bash">
 
# Checkstyle settings #
 
# Checkstyle settings #
 
maven.checkstyle.excludes=**/*generated*/**
 
maven.checkstyle.excludes=**/*generated*/**
 
maven.checkstyle.properties=${basedir}/AAB_checks.xml
 
maven.checkstyle.properties=${basedir}/AAB_checks.xml
</pre>
+
</syntaxhighlight>
 +
 
  
 
== Configuration ==
 
== Configuration ==
Line 27: Line 113:
 
The AAB uses its own CheckStyle convention (AAB_checks.xml), which is configured for each project separately.  
 
The AAB uses its own CheckStyle convention (AAB_checks.xml), which is configured for each project separately.  
 
The format of the configuration file:
 
The format of the configuration file:
<pre>
+
<syntaxhighlight lang="xml">
 
<?xml version="1.0" encoding="UTF-8"?>
 
<?xml version="1.0" encoding="UTF-8"?>
 
<!DOCTYPE module PUBLIC "-//Puppy Crawl//DTD Check Configuration 1.1//EN" "http://www.puppycrawl.com/dtds/configuration_1_1.dtd">
 
<!DOCTYPE module PUBLIC "-//Puppy Crawl//DTD Check Configuration 1.1//EN" "http://www.puppycrawl.com/dtds/configuration_1_1.dtd">
Line 48: Line 134:
 
         ...
 
         ...
 
   </module>
 
   </module>
</module></pre>
+
</module>
 +
</syntaxhighlight>
 +
 
  
 
== Problem ==
 
== Problem ==
Line 55: Line 143:
 
|-
 
|-
 
|
 
|
{|border="1" style="width:950px;font-size:8pt;height:80%;" cellpadding="1" cellspacing="0" align="left"  
+
{|border="1" style="width:1250px;font-size:8pt;height:80%;" cellpadding="1" cellspacing="0" align="left"  
 
|-
 
|-
! style="width:12%" bgcolor="#cfcfcf" align="left"   |CheckStyle
+
! style="width:12%" bgcolor="#cfcfcf" align="left"     | CheckStyle
! style="width:08%" bgcolor="#cfcfcf" align="center" |HTML-Size<br>Kb
+
! style="width:08%" bgcolor="#cfcfcf" align="center" | HTML-Size<br>Kb
! style="width:08%" bgcolor="#cfcfcf" align="center" |HTML<br>Reduction %
+
! style="width:08%" bgcolor="#cfcfcf" align="center" | HTML<br>Reduction %
! style="width:08%" bgcolor="#cfcfcf" align="center" |HTML-Lines<br>K
+
! style="width:08%" bgcolor="#cfcfcf" align="center" | HTML-Lines<br>K
! style="width:08%" bgcolor="#cfcfcf" align="center" |Line<br>Reduction %
+
! style="width:08%" bgcolor="#cfcfcf" align="center" | Line<br>Reduction %
! style="width:56%" bgcolor="#cfcfcf" align="left"   |Description
+
! style="width:56%" bgcolor="#cfcfcf" align="left"     | Description
 
|-
 
|-
 
|valign="top"    |All
 
|valign="top"    |All
Line 90: Line 178:
 
| align="center" |37,378
 
| align="center" |37,378
 
| align="center" |0.05%
 
| align="center" |0.05%
| No ''com.puppycrawl.tools.checkstyle.checks.whitespace.MethodParamPadCheck''<br>tokens: CTOR_DEF, LITERAL_NEW, METHOD_CALL, METHOD_DEF, SUPER_CTOR_CALL
+
| No ''com.puppycrawl.tools.checkstyle.checks.whitespace.MethodParamPadCheck''<br>Tokens: CTOR_DEF, LITERAL_NEW, METHOD_CALL, METHOD_DEF, SUPER_CTOR_CALL
 
|-
 
|-
 
|valign="top"    |No whitespace After  
 
|valign="top"    |No whitespace After  
Line 97: Line 185:
 
| align="center" | 36,795
 
| align="center" | 36,795
 
| align="center" | 0.62%
 
| align="center" | 0.62%
| No ''com.puppycrawl.tools.checkstyle.checks.whitespace.NoWhitespaceAfterCheck''<br>tokens: ARRAY_INIT, BNOT, DEC, DOT, INC, LNOT, UNARY_MINUS, UNARY_PLUS
+
| No ''com.puppycrawl.tools.checkstyle.checks.whitespace.NoWhitespaceAfterCheck''<br>Tokens: ARRAY_INIT, BNOT, DEC, DOT, INC, LNOT, UNARY_MINUS, UNARY_PLUS
 
|-
 
|-
 
|valign="top" |No whitespace Before  
 
|valign="top" |No whitespace Before  
Line 104: Line 192:
 
| align="center" |36.773
 
| align="center" |36.773
 
| align="center" |0.03%
 
| align="center" |0.03%
| No ''com.puppycrawl.tools.checkstyle.checks.whitespace.WhitespaceAfterCheck''<br>tokens: SEMI, POST_DEC, POST_INC
+
| No ''com.puppycrawl.tools.checkstyle.checks.whitespace.WhitespaceAfterCheck''<br>Tokens: SEMI, POST_DEC, POST_INC
 
|-
 
|-
 
|valign="top"    |No whitespace After  
 
|valign="top"    |No whitespace After  
Line 111: Line 199:
 
| align="center" |25,286
 
| align="center" |25,286
 
| align="center" |12.32%
 
| align="center" |12.32%
| No ''com.puppycrawl.tools.checkstyle.checks.whitespace.NoWhitespaceAfterCheck''<br>tokens: COMMA, SEMI, TYPECAST
+
| No ''com.puppycrawl.tools.checkstyle.checks.whitespace.NoWhitespaceAfterCheck''<br>Tokens: COMMA, SEMI, TYPECAST
  
 
|-
 
|-
Line 119: Line 207:
 
| align="center" |15,276
 
| align="center" |15,276
 
| align="center" | 10.69%
 
| align="center" | 10.69%
| No ''com.puppycrawl.tools.checkstyle.checks.whitespace.WhitespaceAroundCheck'' <br>tokens: ASSIGN, BAND, BAND_ASSIGN, BOR, BOR_ASSIGN, BSR, BSR_ASSIGN, BXOR, BXOR_ASSIGN, COLON, DIV, DIV_ASSIGN, EQUAL, GE, GT, LAND, LCURLY, LE, LITERAL_ASSERT, LITERAL_CATCH, LITERAL_DO, LITERAL_ELSE, LITERAL_FINALLY, LITERAL_FOR, LITERAL_IF, LITERAL_RETURN, LITERAL_SYNCHRONIZED, LITERAL_TRY, LITERAL_WHILE, LOR, LT, MINUS, MINUS_ASSIGN, MOD, MOD_ASSIGN, NOT_EQUAL, PLUS, PLUS_ASSIGN, QUESTION, RCURLY, SL, SLIST, SL_ASSIGN, SR, SR_ASSIGN, STAR, STAR_ASSIGN
+
| No ''com.puppycrawl.tools.checkstyle.checks.whitespace.WhitespaceAroundCheck'' <br>Tokens: ASSIGN, BAND, BAND_ASSIGN, BOR, BOR_ASSIGN, BSR, BSR_ASSIGN, BXOR, BXOR_ASSIGN, COLON, DIV, DIV_ASSIGN, EQUAL, GE, GT, LAND, LCURLY, LE, LITERAL_ASSERT, LITERAL_CATCH, LITERAL_DO, LITERAL_ELSE, LITERAL_FINALLY, LITERAL_FOR, LITERAL_IF, LITERAL_RETURN, LITERAL_SYNCHRONIZED, LITERAL_TRY, LITERAL_WHILE, LOR, LT, MINUS, MINUS_ASSIGN, MOD, MOD_ASSIGN, NOT_EQUAL, PLUS, PLUS_ASSIGN, QUESTION, RCURLY, SL, SLIST, SL_ASSIGN, SR, SR_ASSIGN, STAR, STAR_ASSIGN
 
|}
 
|}
 
|}
 
|}
 +
 +
== See also ==
 +
<span class="editsection">[[#content|top]]</span>
 +
* [[Software Static Test Software Overview]] contains more information on other Static Software Tools.
 +
 +
== Reference ==
 +
<span class="editsection">[[#content|top]]</span>
 +
<references/>
 +
 
[[Category:Index]]
 
[[Category:Index]]
 
[[Category:Java]]
 
[[Category:Java]]
 
[[Category:Programming]]
 
[[Category:Programming]]
 +
[[Category:Metrics]]
 
[[Category:Tools]]
 
[[Category:Tools]]
 
[[Category:Utility]]
 
[[Category:Utility]]

Latest revision as of 17:33, 15 August 2015

Checkstyle [1] is a tool for checking Code Standards Violations. It can be used different environments:

  • Standalone (not covered here)
  • Ant
  • Maven
  • IDE

Description

Checkstyle is a development tool to help programmers write Java code that adheres to a coding standard. It automates the process of checking Java code to spare humans of this boring (but important) task. This makes it ideal for projects that want to enforce a coding standard.

Checkstyle is highly configurable and can be made to support almost any coding standard. An example configuration files are supplied supporting the Sun Code Conventions, Google Java Style.

For downloading the software see the homepage of CheckStyle on SourceForge [1].

Pre-requirements

  • All
  • Ant
    • Installed version of Apache Ant [2].
    • Dowloaded and installed standalone version. See the SourceForge Website of CheckStyle [1].
  • Maven
    • Internet Connection for running Maven.
    • Installed version of Apache Maven [3].

Installation in Ant

To set CheckStyle in Ant add the following line of code to the build.xml. <syntaxhighlight lang="xml" line>

  <property name = "checkstyle.home.dir" location = "." />
  ...
  <taskdef resource  = "com/puppycrawl/tools/checkstyle/ant/checkstyle-ant-task.properties" classpath = "checkstyle-6.8.1-all.jar" />
  ...
  <property name="checkstyle.sourcepath"    value="src"                      />
  <property name="checkstyle.report.path"   value="."                        />
  <property name="checkstyle.report.style"  value="checkstyle-simple.xsl"    />
  <property name="checkstyle.report.xml"    value="checkstyle-report.xml"    />
  <property name="checkstyle.report.html"   value="checkstyle-report.html"   />
  ...
  <target name        = "check"
          description = "Generates a report of code convention violations.">
     <mkdir dir   = "${checkstyle.report.path}" />
     <checkstyle config          = "${checkstyle.report.config}"
                 failureProperty = "checkstyle.failure"
                 failOnViolation = "false">
        <formatter type   = "xml"
                   tofile = "${checkstyle.report.xml}"/>
        <fileset dir      = "${checkstyle.sourcepath}"
                 includes = "**/*.java, **/*.xml, **/*.properties"
                 excludes = "checkstyle/" />
     </checkstyle>
     <xslt basedir = "${checkstyle.report.path}"
           destdir = "${checkstyle.report.path}"
           style   = "${checkstyle.report.style}" />
  </target>

</syntaxhighlight>

Line Description Default Global
1-2 Property for the location of all the checkstyle-jar files. It must contain checkstyle-6.8.1-all.jar (See below). Current Directory /usr/local/bin/checkstyle-6.8.1
4-5 Reference to the checkstyle resource checkstyle-6.8.1-all.jar
7-12 References to the checkstyle properties.
14-36 Ant-Target definition, creating the report path, running checkstyle and formats the resulting xml-report to HTML using XSLT.

Installation in Maven

To set CheckStyle in Maven or Maven 2 add the following lines of code to the settings of the project.xml, project.properties.

Project.xml

<syntaxhighlight lang="xml">

 <reports>
    ...
    <report>maven-checkstyle-plugin</report>
    ...
 </reports>

</syntaxhighlight>


Project.properties

Add the name of the file containing the checkstyle settings: <syntaxhighlight lang="bash">

  1. Checkstyle settings #

maven.checkstyle.excludes=**/*generated*/** maven.checkstyle.properties=${basedir}/AAB_checks.xml </syntaxhighlight>


Configuration

The default CheckStyle is highly configurable. The installation come with sample configuration files:

  • Apache Avalon (avalon_checks.xml)
  • Sun coding conventions (sun_checks.xml)
  • Apache Turbine Framework (turbine_checks.xml), this is the base of Frame

The AAB uses its own CheckStyle convention (AAB_checks.xml), which is configured for each project separately. The format of the configuration file: <syntaxhighlight lang="xml"> <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE module PUBLIC "-//Puppy Crawl//DTD Check Configuration 1.1//EN" "http://www.puppycrawl.com/dtds/configuration_1_1.dtd"> <module name="Checker">

   <module name="TreeWalker">
       <module name="com.puppycrawl.tools.checkstyle.checks.GenericIllegalRegexpCheck">
           <property name="format" value="System.err"/>
           <property name="ignoreCase" value="false"/>
           <property name="ignoreComments" value="false"/>
           <property name="message" value="Do not use System.err"/>
           <property name="severity" value="warning"/>
       </module>
       ...
       <module name="com.puppycrawl.tools.checkstyle.checks.sizes.LineLengthCheck">
           <property name="ignorePattern" value="^$"/>
           <property name="max" value="110"/>
           <property name="severity" value="warning"/>
           <property name="tabWidth" value="4"/>
       </module>
       ...
  </module>

</module> </syntaxhighlight>


Problem

The CheckStyle report becomes very long and difficult to handel. There are a few checks which delivers very much lines of report lines. A comparison of the size of checkstyle-report.html file for the different checkstyles

CheckStyle HTML-Size
Kb
HTML
Reduction %
HTML-Lines
K
Line
Reduction %
Description
All 3,881  0 90,142 0,00% Default checks
No LineLength 3,545  336 82,104 8.66% No com.puppycrawl.tools.checkstyle.checks.sizes.LineLengthCheck (AAB_1_checks.xml)
No indentation 1,563 1,982 37,411 51.07% No com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck (AAB_1_checks.xml)
No whitespace Param Pad 1,561  2 37,378 0.05% No com.puppycrawl.tools.checkstyle.checks.whitespace.MethodParamPadCheck
Tokens: CTOR_DEF, LITERAL_NEW, METHOD_CALL, METHOD_DEF, SUPER_CTOR_CALL
No whitespace After 1,537  24 36,795 0.62% No com.puppycrawl.tools.checkstyle.checks.whitespace.NoWhitespaceAfterCheck
Tokens: ARRAY_INIT, BNOT, DEC, DOT, INC, LNOT, UNARY_MINUS, UNARY_PLUS
No whitespace Before 1,536  1 36.773 0.03% No com.puppycrawl.tools.checkstyle.checks.whitespace.WhitespaceAfterCheck
Tokens: SEMI, POST_DEC, POST_INC
No whitespace After 1,058 478 25,286 12.32% No com.puppycrawl.tools.checkstyle.checks.whitespace.NoWhitespaceAfterCheck
Tokens: COMMA, SEMI, TYPECAST
No whitespace Around 643 415 15,276 10.69% No com.puppycrawl.tools.checkstyle.checks.whitespace.WhitespaceAroundCheck
Tokens: ASSIGN, BAND, BAND_ASSIGN, BOR, BOR_ASSIGN, BSR, BSR_ASSIGN, BXOR, BXOR_ASSIGN, COLON, DIV, DIV_ASSIGN, EQUAL, GE, GT, LAND, LCURLY, LE, LITERAL_ASSERT, LITERAL_CATCH, LITERAL_DO, LITERAL_ELSE, LITERAL_FINALLY, LITERAL_FOR, LITERAL_IF, LITERAL_RETURN, LITERAL_SYNCHRONIZED, LITERAL_TRY, LITERAL_WHILE, LOR, LT, MINUS, MINUS_ASSIGN, MOD, MOD_ASSIGN, NOT_EQUAL, PLUS, PLUS_ASSIGN, QUESTION, RCURLY, SL, SLIST, SL_ASSIGN, SR, SR_ASSIGN, STAR, STAR_ASSIGN

See also

top

Reference

top

  1. 1.0 1.1 1.2 Sourceforge CheckStyle home page.
  2. Ant, Apache Ant information for installing and using Ant
  3. Maven, Apache Maven information for installing and using maven