Over a million developers have joined DZone.

Fixing Sonar Violations Automatically With Walkmod

DZone user Raquel Pau shares her open source tool walkmod to automatically fix errors that violate rules in Sonar.

· DevOps Zone

Discover how to optimize your DevOps workflows with our cloud-based automated testing infrastructure, brought to you in partnership with Sauce Labs

Code conventions are programming guidelines to improve the code readability, reduce bugs and thus, reduce maintenance costs. Currently, developers can detect automatically which parts of your code are violating a specific code convention through tools such as PMD, Checkstyle, Findbugs or Sonar.

However, after having a detailed report about which parts of the code contain bad programming practices, developers need to fix them manually using their favorite editor over and over again even if the solution is completely simple and deterministic. Let's see a very common code convention that can be configured through Sonar:

Declarations should use Java collection interfaces such as "List" rather than specific implementation classes such as "LinkedList" 

How many times have you fix code that violates this rule? I have done it a lot.

For this reason, and in order to enforce the DRY (Don't Repeat Yourself) principle, I have built an open source tool called walkmod which helps to automatize and share these kind of quick fixes with the rest of project contributors. Internally, these quick fixes are implemented as code transformations packaged in walkmod plugins. The current plugins only supports projects written in Java (including Android), but the walkmod is not restricted to any specific programming language.

Projects only need to create a configuration file with the quick fixes to apply inside the root directory of the project. After that, Walkmod can be applied over the entire code base or only over the latest changes. This last scenario is usually preferred when there are not enough tests to validate the code modifications. Now, let's see how to use walkmod to automatically fix Sonar rules.

Configuring Sonar Rules

First of all, you need to install walkmod from homebrew (Mac users) or using the installer of the official web page. Then, go to the root directory of your project (the directory of your pom.xml or your gradle.build) and execute the command walkmod plugins.

walkmod plugins

│ setter-getter                 │     │ Walkmod plugin to add setters and getters into   │
│                               │     │ Java source files.                               │
│                               │     │                                                  │
│                               │     │ URL:https://github.com/rpau/walkmod-setter-gette │
│                               │     │ r-plugin                                         │
│ sonar                         │     │ Walkmod plugin to automatically fix Sonar rule   │
│                               │     │ violations.                                      │
│                               │     │                                                  │
│                               │     │ URL:https://github.com/walkmod/walkmod-sonar-plu │
│                               │     │ gin                                              │

This will show you, the available walkmod plugins, and among them, the walkmod-sonar-plugin. But, which are the available quick fixes to apply? Executing walkmod inspect we will see the list of available transformations.

walkmod inspect sonar
│ org.walkmod:walkmod-sonar-plugin:StringCheckOnLe │ transformation │                           │ unavailable │
│ ft                                               │                │                           │             │
│ org.walkmod:walkmod-sonar-plugin:UseCollectionIs │ transformation │                           │ unavailable │
│ Empty                                            │                │                           │             │
│ org.walkmod:walkmod-sonar-plugin:DeclarationsSho │ transformation │                           │ unavailable │
│ uldUseCollectionInterfaces   

Now, let's choose our preferred ones. In order to apply the quick fix of the example, it is necessary to execute:

walkmod add -R sonar:DeclarationsShouldUseCollectionInterfaces 

Code transformations are identified by the "groupId:artifactId:beanName". However, if the groupId is "org.walkmod", it can be ommitted. For another hand, artifacts ids always start with "walkmod-" and end with "-plugin", which also can be ommited. Therefore, "org.walkmod:walkmod-sonar-plugin:DeclarationsShouldUseCollectionInterfaces" is the same than "sonar:DeclarationsShouldUseCollectionInterfaces"

Clean Code Generation 

Now, after selecting which are the required code conventions for our project, it is time to see the results. You will see the results only running the following command:

walkmod apply

Walkmod will report you the modified files with the ">>" mark.

INFO [main] - >>com.mycompany.MyClass
INFO [main] - >>com.mycompany.dto.Entity

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 : 10

However, in order to analyze every single change, we recommend to use git diff or your favourite control version tool.

Contributing to Other Sonar Quick Fixes 

Currently, the github.com/walkmod/walkmod-sonar-plugin contains a few of the available Sonar quick fixes. We encourage developers to to participate in this Github project, sending us pull requests with new ones or creating an issue for those you would like to have. 

Download “The DevOps Journey - From Waterfall to Continuous Delivery” to learn learn about the importance of integrating automated testing into the DevOps workflow, brought to you in partnership with Sauce Labs.

clean code ,code conventions ,sonar

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}