Automatic code style review

“Check style” 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.

In this article we are going to learn how to set up a project to run style check every time we ask it for (FYI you can run it each time you compile just not for this example).

We are going to create a very basic implementation of this tool. Of course you are able to do lots of extra stuff, if you are interested please check (official project site).

The codes style review we are going to set up is going to check for :

Checks for whitespace

  • GenericWhitespace
  • EmptyForIteratorPad
  • MethodParamPad
  • NoWhitespaceAfter
  • NoWhitespaceBefore
  • ParenPad
  • TypecastParenPad
  • WhitespaceAfter
  • WhitespaceAround

Lets Begin setting the project up

Now we create the following checkstyle.xml file

<?xml version="1.0"?>
<!DOCTYPE module SYSTEM "" PUBLIC "-//Puppy Crawl//DTD Check Configuration 1.2//EN">
<!--module name="NewlineAtEndOfFile"/-->
<module name="FileLength"/>
<!-- Space after 'for' and 'if' -->
-<module name="RegexpSingleline">
<property name="format" value="^\s*(for|if)[^ ]\("/>
<property name="message" value="Space needed before opening parenthesis."/>
<!-- For each spacing -->
-<module name="RegexpSingleline">
<property name="format" value="^\s*for \(.*?([^ ]:|:[^ ])"/>
<property name="message" value="Space needed around ':' character."/>
-<module name="TreeWalker">
<!-- Checks for Naming Conventions. -->
<!-- See -->
<module name="LocalFinalVariableName"/>
<module name="MemberName"/>
<module name="PackageName"/>
<module name="ParameterName"/>
<module name="TypeName"/>
<!-- Checks for imports -->
<!-- See -->
<module name="AvoidStarImport"/>
<module name="IllegalImport"/>
<module name="RedundantImport"/>
-<module name="UnusedImports">
<property name="processJavadoc" value="true"/>
<!-- Checks for whitespace -->
<!-- See -->
<module name="GenericWhitespace"/>
<module name="EmptyForIteratorPad"/>
<module name="MethodParamPad"/>
<module name="NoWhitespaceAfter"/>
<module name="NoWhitespaceBefore"/>
<module name="OperatorWrap"/>
<module name="ParenPad"/>
<module name="TypecastParenPad"/>
<module name="WhitespaceAfter"/>
<module name="WhitespaceAround"/>
<!-- Modifier Checks -->
<!-- See -->
<module name="ModifierOrder"/>
<module name="RedundantModifier"/>
<!-- Checks for blocks. You know, those {}'s -->
<!-- See -->
<!--module name="AvoidNestedBlocks"/-->
<!--module name="EmptyBlock"/-->
<module name="LeftCurly"/>
<!--module name="NeedBraces"/-->
<module name="RightCurly"/>
<!-- Checks for common coding problems -->
<!-- See -->
<!--module name="AvoidInlineConditionals"/-->
<module name="CovariantEquals"/>
<module name="EmptyStatement"/>
<!--<module name="EqualsAvoidNull"/>-->
<module name="EqualsHashCode"/>
<!--module name="HiddenField"/-->
<module name="IllegalInstantiation"/>
<!--module name="MagicNumber"/-->
<module name="MissingSwitchDefault"/>
<module name="SimplifyBooleanExpression"/>
<module name="SimplifyBooleanReturn"/>
<module name="UpperEll"/>

and save it on **\[project_root_folder]\settings\

Lets add the following lines on build.gradle testapp-checkstyle file

apply plugin: ''
apply plugin: 'checkstyle'


As following :

testapp-checkstyle (module) build.gradle file

Lets open parent build.gradle file and create a task to ask for a code review if the property STYLE is present.

CheckStyleDemo build.gradlew

Now lets add some code violations to our

Now on AS console we type the following command to ask for a code review and execute it.

add STYLE property to run Check-style

Of course this is not going to pass the code review because of the violation we added to The following error will come out.

Check-style error output

As you can see we can check on the following path for our code violations file:///C:/CheckstyleDemo/testapp-checkstyle/build/reports/checkstyle/checkstyle.html

After we clean them up , re-run gradle assembleDebug -PSTYLE and if no error comes out you are good to go.

You can download my sample project from