DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Trending

  • Vision-Language Models in 2026: From Research Breakthroughs to Cloud-Native Production Systems
  • The Window Closes: Why I Switched to Kotlin Before the Codebase Got Too Big
  • PostgreSQL vs Oracle: When Higher Cost Means Better Performance
  • Going All In On the JVM 25 Virtual Threads

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

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.

By 
Raquel Pau
·
Mar. 21, 16 · Opinion
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
12.3K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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] - ** STARTING TRANSFORMATIONS CHAINS **
-----------------------------------------------------------
INFO [main] - >>com.mycompany.MyClass
INFO [main] - >>com.mycompany.dto.Entity
....
------------------------------------------------------------

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

Opinions expressed by DZone contributors are their own.

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook