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
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Trending

  • Monoliths, REST, and Spring Boot Sidecars: A Real Modernization Playbook
  • Simpler Data Transfer Objects With Java Records
  • Enhancing Business Decision-Making Through Advanced Data Visualization Techniques
  • Distributed Consensus: Paxos vs. Raft and Modern Implementations

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 user avatar
Raquel Pau
·
Mar. 21, 16 · Opinion
Likes (15)
Comment
Save
Tweet
Share
12.0K Views

Join the DZone community and get the full member experience.

Join For Free

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
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!