By Raquel Pau Fernández

There are several QA tools for Java, such as PMD or Sonar, that report which source files do not follow a set of code rules (e.g. “all fields must be private”). However, developers need to fix all rule violations manually, which can be a repetitive and time consuming task, especially if there are thousands of files to change or violations which show up frequently. 

Screen Shot 2014-11-04 at 11.38.16

Walkmod is an open source Java tool that can be used to apply code conventions automatically. It scans the code, detects violations of the code conventions and fixes them by applying a set of code transformations. It saves development time and makes your code more DRY (Don’t Repeat Yourself) compliant, because code conventions are specified in just one place and all of them are applied automatically.


Installing  walkmod  is  very  easy.  Just  download  the  binary  distribution  from, unzip the file and add the bin folder into your PATH environment variable.

Now, open your favorite prompt and type walkmod. The console should show something like this:

Screen Shot 2014-11-04 at 11.39.00

Let’s add code conventions to your project

Let’s remove the unused imports of your project using walkmod. These are the steps you need to take:

      1. Add an empty file called walkmod.xml in the root directory of your project. This file is used to configure walkmod for a specific project, and contains the majority of information required to apply the code conventions of for a project in the desired way. Therefore,  following the standard Maven project structure, the folder tree should look like this:Screen Shot 2014-11-04 at 11.39.42
      2. Copy the following text and paste it into your walkmod.xml file.
<!DOCTYPE walkmod PUBLIC "-//WALKMOD//DTD"  "" >
	    <plugin groupId="org.walkmod" artifactId="walkmod-imports-cleaner-plugin" version="2.0" />
  <chain name="example-chain">
	    <reader path="${source directory}" />
	    <transformation type="walkmod:commons:import-cleaner" />
      <writer path="${target directory}" />
    1. Replace  ${source directory} value with the parent directory of your code or packages (e.g. src/). If you cannot overwrite the source files, replace the ${target directory} for another output directory. The default path is src/main/java. If you use the same directory, you can remove the reader and writer tags.
    2. Open a prompt, go to your project directory and type “walkmod apply”. It should report which files included unused imports.

Are there other conventions available?

The previous example uses just one walkmod plugin to remove unused imports, but there  are  other  plugins  available  with  different  purposes  at Check them and select your code conventions!

Alternatively, if you cannot find a plugin that meets your needs, do not worry – you can extend walkmod to create your custom code conventions. walkmod supports three possible extension mechanisms:

1) Plugins

Plugins are  implemented in Java,  and  are used to perform complex code transformations using the Visitor pattern. All plugins must be deployed into a Maven repository to share them, which is useful when different projects share a set of conventions. The following snippet shows a skeleton to create a custom code convention for fields and methods.

package com.mycompany.mywalkmodplugin;

import org.walkmod.javalang.visitors.VisitorSupport;
import org.walkmod.walkers.VisitorContext;
import org.walkmod.javalang.ast.body.MethodDeclaration;
import org.walkmod.javalang.ast.body.FieldDeclaration;

public class FooTransformation extends VisitorSupport{

  public void visit(FieldDeclaration field, VisitorContext ctx) {

  public void visit(MethodDeclaration field, VisitorContext ctx) {


2) Templates (such as JSPs, velocity, etc.)

Templates are especially useful to generate code (e.g. to ensure a default implementation of the “equals” method in all classes) in a human readable way.  If you are experienced with code generation tools, you probably have suffered them to not being designed to allow further code modifications once the code has been generated. In such a situation, if you manually change the generated code, you assume the risk the tool may overwrite your changes if it is executed again. To solve this gap, walkmod provides a powerful merge engine, which allows the user to configure, for instance, whether to replace or not the existing fields or methods for a given class. In short, feel free to change the code generated by walkmod! It will not overwrite your changes if you do not want to. The following example shows how to build a simple facade pattern with a “create” method for each entity type (“UserAccount”,  “Group”, “Company”,  …) using walkmod. For each of them, the template engine produces always the same Java class “AppServices” with just one method called “createXXX”, where “XXX” is the name of an entity type. After executing a transformation, if it is mergeable, walk mod will merge the produced output with the existent content.

The default supported template engine is Groovy.

Screen Shot 2014-11-07 at 12.26.54

3) Scripts

Scripts are useful to perform code transformations without deploying them to the Maven repository. The default scripting language is Groovy. The following picture shows a script example whose aim is to assure all fields are private.

import org.walkmod.javalang.ast.body.ModifierSet;
import java.lang.reflect.Modifier;
import org.walkmod.javalang.ast.body.FieldDeclaration;

for(type in node.types){
  def fields = type.members.findAll({it instanceof FieldDeclaration});
  for (field in fields){
    int modifiers = ModifierSet.addModifier(field.modifiers, Modifier.PRIVATE);
    modifiers = ModifierSet.removeModifier(modifiers, Modifier.PROTECTED);
    modifiers = ModifierSet.removeModifier(modifiers, Modifier.PUBLIC);

Integrations with existing development tools

walkmod can be integrated with some of the most used development tools for Java developers, allowing devs to run walkmod from their favorite development environmentwithout switching to the prompt. The supported development tools are as follows:


Maven is a build automation tool used primarily for Java projects and it is completely customizable and extensible. In fact, most of its core functionality has the form of plugins.

 walkmod’s Maven plugin (check is arecently created plugin to allow the running of walkmod in your project life cycle without performing any installation. You just need to declare the following plugin into your pom.xml



Gradle is also a project automation tool that builds upon the concepts of Apache Ant and Maven and introduces a Groovy-based domain-specific-language (DSL) instead ofthe more traditional XML form of declaring the project configuration.

Gradle walkmod Plugin (check )is a Gradle plugin to allow to run an embedded walkmod installation from Gradle.  All you need to do is edit your build.gradle with the following contents:

buildscript {
    repositories {

    dependencies {
        classpath 'org.walkmod:gradle-walkmod-plugin:0.0.1'

apply plugin: 'walk mod'

The plugin will add the new available walkmod commands for Gradle.


Eclipse is one of the most important IDEs (Integrated Development Environment) for Java developers. Eclipse can be enriched with new menus, buttons, views, etc. by means of plugins. This IDE likely has a larger plugin catalog than most, including code versioning, issue tracking, build automation, code quality and database clients.

There is an Eclipse plugin to run walkmod from the Eclipse environment (check and  follow  the  installation instructions). It allows you to run an embedded walkmod installation or an existing one on your  computer.  With  this  plugin,  you  will  also  be  able  to  create  custom  run configurations to run walkmod completely parameterized.  The following picture shows the custom walkmod menus to run walkmod when we apply a right click the project.

Screen Shot 2014-11-07 at 12.32.13


Forge is a development tool composed by plugins that automate part of the development of Maven projects. For example, if we work with a specific framework, it automatizes its configuration and the creation of the specific components of the framework. The idea is having a set of commands for those tasks a developer usually needs to do manually or to use IDE plugins. In the case of walkmod, the plugin offers the possibility to run all walkmod commands, prepare a project or create a new walkmod plugin.

To use this plugin from forge, you just need to type:


Walkmod Tutorial: Apply Code Conventions Automatically

| Java Language| 954 views | 1 Comment
About The Author

1 Comment

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>