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
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Why Kotlin Multiplatform is a Game-Changer for Startup Teams
  • Kotlin Code Style: Best Practices for Former Java Developers
  • Metal and the Simulated Annealing Algorithm
  • Reactive Kafka With Spring Boot

Trending

  • AWS vs GCP Security: Best Practices for Protecting Infrastructure, Data, and Networks
  • A Scalable Framework for Enterprise Salesforce Optimization: Turning Outcomes Into an Operating System
  • The Update Problem REST Doesn't Solve
  • The Agent Protocol Stack: MCP vs. A2A vs. AG-UI
  1. DZone
  2. Coding
  3. Languages
  4. A SonarQube Plugin for Kotlin

A SonarQube Plugin for Kotlin

Interested in Kotlin but worried about leaving your favorite Java tools behind? Time to make a plugin! Here, we start the process of making a SonarQube plugin for Kotlin.

By 
Nicolas Fränkel user avatar
Nicolas Fränkel
·
Jun. 14, 17 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
9.7K Views

Join the DZone community and get the full member experience.

Join For Free

Since I started my journey into Kotlin, I wanted to use the same libraries and tools I use in Java. For libraries — Spring Boot, Mockito, etc. — it’s straightforward, as Kotlin is 100% interoperable with Java. For tools, well, it depends. For example, Jenkins works flawlessly, while SonarQube lacks a dedicated plugin. The SonarSource team has limited resources: Kotlin, though on the rise — and even more so since Google I/O 17, is not in their pipe. This post series is about creating such a plugin, and this first post is about parsing Kotlin code.

ANTLR

In the realm of code parsing, ANTLR is a clear leader in the JVM world.

ANTLR (ANother Tool for Language Recognition) is a powerful parser generator for reading, processing, executing, or translating structured text or binary files. It’s widely used to build languages, tools, and frameworks. From a grammar, ANTLR generates a parser that can build and walk parse trees.

Designing the Grammar

ANTLR is able to generate parsing code for any language thanks to a dedicated grammar file. However, creating such a grammar from scratch for regular languages is not trivial. Fortunately, thanks to the power of the community, a grammar for Kotlin already exists on GitHub.

With this existing grammar, ANTLR is able to generate Java parsing code to be used by the SonarQube plugin. The steps are the following:

  • Clone the GitHub repository
    git clone [email protected]:antlr/grammars-v4.git
    


  • By default, classes will be generated under the root package, which is discouraged. To change that default: 
    • Create a src/main/antlr4/<fully>/<qualified>/<package> folder such as src/main/antlr4/ch/frankel/sonarqube/kotlin/api
    • Move the g4 files there
    • In the POM, remove the sourceDirectory and includes bits from the antlr4-maven-plugin configuration to use the default
  • Build and install the JAR in the local Maven repo 
    cd grammars-v4/kotlin
    mvn install

This should generate a KotlinLexer and a KotlinParser class, as well as several related classes in target/classes. As Maven goes, it also packages them in a JAR named kotlin-1.0-SNAPSHOT.jar in the target folder — and in the local Maven repo as well.

Testing the Parsing Code

To test the parsing code, one can use the grun command. It’s an alias for the following:

java -Xmx500M -cp "<path/to/antlr/complete/>.jar:$CLASSPATH" org.antlr.v4.Tool


Create the alias manually or install the antlr package via Homebrew on OSX.

With grun, Kotlin code can be parsed then displayed in different ways, textual and graphical. The following expects an input in the console:

cd target/classes
grun Kotlin kotlinFile -tree


After having typed valid Kotlin code, it yields its parse tree in text. By replacing the -tree option with the -gui option, it displays the tree graphically instead. For example, the following tree comes from this snippet:

fun main(args : Array<String>) { 
    val firstName : String = "Adam"
    val name : String? = firstName 
    print("$name") 
}


Image title

In order for the JAR to be used later in the SonarQube plugin, it has been deployed on Bintray. In the next post, we will be doing proper code analysis to check for violations.

Kotlin (programming language)

Published at DZone with permission of Nicolas Fränkel. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Why Kotlin Multiplatform is a Game-Changer for Startup Teams
  • Kotlin Code Style: Best Practices for Former Java Developers
  • Metal and the Simulated Annealing Algorithm
  • Reactive Kafka With Spring Boot

Partner Resources

×

Comments

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

  • 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