Tutorial details

[TUT] Create your own CheckStyle check | App Code for Sale | Preview

[TUT] Create your own CheckStyle check | Android Tutorial

How to create your own CheckStyle check

Overview PAGE TOP

Android code is just like any Java system, you need to write tests and have automated code review checks. CheckStyle is a way of double checking your Java code.

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.

Below I outline how you can write your own custom CheckStyle checks so you can tailor CheckStyle to the way you work. This example has a check to point out the use of Hungarian Notation, specifically the ‘m’ prefix for fields in your class. This is a dated practice and has no place in the modern IDE.

What we are going to do:

  • Create a test project with the errors we want to point out
  • Create a project that will house our custom checks
  • Hook up our test project to run checkstyle
  • Run our custom check against the Test Project

The code does most the explanation here, note that this uses Maven as a build tool. You don’t need to use Maven, you could: create the projects, add checkstyle to the build path and run from the command line.

To create a custom checkstyle check you extend the puppycrawl.CheckStyle ‘Check’ class. Here we’ve split the check itself into another class that uses a reg ex to find member variables with a leading ‘m’:

AntiHungarianCheck.java

+ expand source

HungarianNotationMemberDetector.java

+ expand source

Thanks to Gnorsilva for the RegEx and adding unit testing.

The key point to this process is getting checkstyle to use your custom checks jar file.

Checkstyle needs to know what checks you want to run, for this example we just run our 1 custom check, but otherwise you would append the custom check to this list.

checkstyle-config.xml

1   <?xml version="1.0"?>
2   <!DOCTYPE module PUBLIC
3       "-//Puppy Crawl//DTD Check Configuration 1.2//EN"
4       "http://www.puppycrawl.com/dtds/configuration_1_2.dtd">
5    
6   <module name="Checker">
7     <module name="TreeWalker">
8       <!-- Blundell specific checks -->
9       <module name="com.blundell.checks.AntiHungarian" />
10    </module>
11  </module>

Then you need to tell CheckStyle to use this config, if your running from the command line it would be slightly different, but with Maven and the latest version of CheckStyle it’s like this:

1   <properties>
2       <checkstyle.config.location>properties/checkstyle-configuration.xml</checkstyle.config.location>
3     </properties>

With maven you declare that you want to use the CheckStyle plugin and that this plugin needs to use your own check jar.

1   <plugin>
2           <groupId>org.apache.maven.plugins</groupId>
3           <artifactId>maven-checkstyle-plugin</artifactId>
4           <version>2.9.1</version>
5           <dependencies>
6             <dependency>
7               <groupId>com.blundell</groupId>
8               <artifactId>blundell-checkstyle-checks</artifactId>
9               <version>1.0</version>
10            </dependency>
11          </dependencies>
12        </plugin>

Now all you need to do is run ‘mvn checkstyle:checkstyle’ and your custom check will be ran.

Running checkstyle creates a report which can be found in the TestProject/target directory, it looks something like this:

Screen-Shot-2012-12-23-at-11.46.54.png

More information can be found in the ReadMe of the GitHub project.

Sources and example:

Create Your Own CheckStyle Check : GitHub

Reference PAGE TOP

http://blog.blundell-apps.com/create-your-own-checkstyle-check/

0 Comments Leave a comment

Please login in order to leave a comment.

Newest first
!

Sign-in to your Chupamobile Account.

The Easiest way to Launch your next App or Game.

Join Chupamobile and get instant access to thousands of ready made App and Game Templates.

Creating an account means you’re okay with Chupamobile’s Terms of Service and Privacy Policy.