Installation

Walkmod is a Java tool, so you must have Java (≥ 1.7) installed in order to proceed.

  1. Download and unzip the distribution archive to the directory you wish to install walkmod.
  2. Add the WALKMOD_HOME environment variable, whose value must be the instalation directory.
  3. Add WALKMOD_HOME/bin environment variable to your PATH.
  4. Open a prompt and type walkmod --version. It should print out your installed version of walkmod, for example:
    Walkmod version "3.0"
    Java version: 1.8.0_101
    Java Home: /Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home/jre
    OS: Mac OS X, Version: 10.12.2

Apply quick fixes

WalkMod started as a framework to write code transformations. However, most of the users look for quick fixes for existing static code analysis tools. Here appear the commands to get the results.

For PMD

walkmod apply -DconfigurationFile=rulesets/java/basic.xml pmd

For CheckStyle

walkmod apply checkstyle

For SonarQube

walkmod apply -DauthToken="THIS_IS_MY_TOKEN" -DsonarHost="https://sonarqube.com" sonar

If you are you interested to selectively apply the required changes incrementally, you need to use WalkModHub. Otherwise, if you want to learn how to use more complex code transformations through a fixed configuration (walkmod.xml), read the Basic Usage.

Integration

Maven

You can run WalkMod in your build process (PMD, CheckStyle or SonarQube) by adding the following Maven WalkMod plugin in your pom.xml. This example, shows you how to apply quick fixes for PMD using Maven.
          <plugin>
            <groupId>org.walkmod.maven.plugins</groupId>
            <artifactId>walkmod-maven-plugin</artifactId>
            <version>1.0.3</version>
            <executions>
              <execution>
                <goals>
                  <goal>apply</goal>
                </goals>
                <configuration>
                  <chains>pmd</chains>
                  <properties>rulesets/java/basic.xml</properties>
                </configuration>
              </execution>
            </executions>
          </plugin>
        
And run
          mvn package
        

Gradle

You can also run WalkMod in your gradle project by adding the following Gradle WalkMod plugin in your build.gradle. This example, shows you how to apply quick fixes for PMD using Gradle.
buildscript {
    repositories {
        mavenCentral()
        jcenter()
    }

    dependencies {
        classpath 'org.walkmod:gradle-walkmod-plugin:0.0.1'
    }
}
apply plugin: 'walkmod'
walkmod {
  chains = [ 'pmd' ]
}
          
And run
            gradle walkmodApply
          

Basic Usage

Let's see how to use walkmod with a simple Java project. With this aim, we have prepared this tutorial, which executes walkmod to clean unused imports in a simple maven project. Please, before continuing with this tutorial, verify that you have installed maven and git.

Now, follow the next instructions.

  1. Open your prompt.
  2. Run git clone https://github.com/walkmod/quickstart.git

    It downloads the source code of our example.

  3. Run cd quickstart

    It enters to the root project folder. This project, as many maven projects, contains the sources in the src/main/java directory. Please, check that the file src/main/java/org/walkmod/quickstart/model/Tweet.java contains an unused import for java.util.Calendar.

  4. Run walkmod add imports-cleaner

    It creates, if it is missing, a walkmod.xml file and adds the imports-cleaner plugin in the configuration.

  5. Run walkmod apply

    It executes walkmod in the project and voila! it has removed an unused import in the class org.walkmod.quickstart.model.Tweet.

    INFO [main] - ** STARTING TRANSFORMATIONS CHAINS **
    -----------------------------------------------------------
    INFO [main] - >>org.walkmod.quickstart.model.Tweet
    ------------------------------------------------------------
    
    INFO [main] - TRANSFORMATION CHAIN SUCCESS
    ------------------------------------------------------------
    INFO [main] - Total time : 0,96 seconds
    INFO [main] - Finished at : Mon, 28 Dec 2015 10:04:06
    INFO [main] - Final memory : 65 M/ 88 M
    INFO [main] - Total modified files : 1
    
  6. Run git diff to check the differences.
  7. Run mvn package to check if it compiles.

Use Formatters

Code formatters specify the appearance of the code: indentation, when to use or avoid parenthesis or brackets, etc, but never about the contents of the code.

For many organizations, having code conventions about the code format is really important, because otherwise, people usually reformat the code created by other people continuously, and thus, becomes impossible understand the changes history.

One of the most well-known formatter tools is the Eclipse formatter. If you want to use this, run the following command:

walkmod set-writer -DconfigFile="myformatter.xml" java-formatter

Looking for plugins

With Walkmod you apply quick fixes by means of code transformations. In fact, the list of plugins allows you publishing and applying quick fixes for your favorite static code analysis tool (e.g PMD, SonarQube, CheckStule) in any project. In order to discover which plugin you need, follow the next instructions:

  1. Run walkmod plugins to print the list of available Walkmod plugins.

  2. ...
    │                   │     │                                                  │
    ├───────────────────┼─────┼──────────────────────────────────────────────────┤
    │ checkstyle        │     │ Walkmod plugin to automatically fix the reported │
    │                   │     │ problems by Checkstyle.                          │
    │                   │     │                                                  │
    │                   │     │ URL:https://github.com/walkmod/walkmod-checkstyl │
    │                   │     │ e-plugin                                         │
    ├───────────────────┼─────┼──────────────────────────────────────────────────┤
    │ pmd               │     │ Walkmod plugin to automatically fix the reported │
    │                   │     │ problems by PMD.                                 │
    │                   │     │                                                  │
    │                   │     │ URL:https://github.com/walkmod/walkmod-pmd-plugi │
    │                   │     │ n                                                │
    ├───────────────────┼─────┼──────────────────────────────────────────────────┤
    ...
    			        
  3. Run walkmod inspect $plugin-id to discover the transformations of an specific plugin. For example:
    walkmod inspect pmd
    And the contents are as follows:
    ┌─────────────────────────────────────────────┬────────────────┬──────────────────────────────┬─────────────┐
    │ TYPE NAME (ID)                              │ CATEGORY       │ PROPERTIES                   │ DESCRIPTION │
    ├─────────────────────────────────────────────┼────────────────┼──────────────────────────────┼─────────────┤
    │ pmd                                         │ transformation │     configurationFile:String │ unavailable │
    └─────────────────────────────────────────────┴────────────────┴──────────────────────────────┴─────────────┘
    
    					
    These identifiers (ID) are the parameters used in the walkmod add $ID command. Properties, such as configurationFile, needs to be passed as a dynamic argument.
    walkmod add -DonfigurationFile=XXXXX pmd
  4. Many plugins, specially those that apply refactorings (e.g PMD, SonarQube, refactoring..), require to resolve your classpath to determine the type of every single expression of your code and, in the case of method calls, which is the associated method signature. Walkmod resolves your project's classpath if it is built using Maven or Gradle. However, if it is an Apache Ant project, you should execute the following instruction after building your project (assuming that your classpath is "bin;lib/guava.jar;lib/log4j.jar"):
    walkmod add -Dclasspath="bin;lib/guava.jar;lib/log4j.jar" rawclasspath