racodond / sonar-jproperties-custom-rules-plugin

Sample plugin that defines SonarQube custom rules for Java Properties files

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Build Status AppVeyor Build Status Quality Gate

Sample plugin that defines SonarQube custom rules for Java Properties files

Description

The SonarQube Java Properties plugin can be enhanced by writing custom rules through a plugin using SonarQube Java Properties API. This sample plugin is designed to help you get started writing your own plugin and custom rules.

Usage

  1. Download and install SonarQube 5.6 or greater
  2. Install the Java Properties plugin (2.1 or greater) either by a direct download or through the Update Center.
  3. Install this sample plugin by a direct download
  4. Start SonarQube
  5. Activate some of the custom rules implemented in this sample plugin. "Forbidden keys should not be used" for example.
  6. Install your favorite analyzer (SonarQube Scanner, Maven, Ant, etc.) and analyze your code. Note that Java 8 is required to run an analysis.
  7. Browse the issues through the web interface

Writing Custom Rules

Creating a SonarQube Plugin

<basePlugin>javaProperties</basePlugin>

Implementing a Rule

There are two different ways to browse the AST:

Using DoubleDispatchVisitorCheck

To explore part of the AST, override a method from DoubleDispactchVisitor. For instance, if you want to explore key nodes, override DoubleDispactchVisitor#visitKey. This method is called each time a key node is encountered in the AST. Note: When overriding a visit method, you must call the super method in order to allow the visitor to visit the children of the node. See ForbiddenKeysCheck for example.

Using SubscriptionVisitorCheck

To explore part of the AST, override SubscriptionVisitor#nodesToVisit by returning the list of Tree#Kind nodes you want to visit. For instance, if you want to explore key nodes the method should return a list containing Tree#Kind#KEY. See ForbiddenStringCheck for example.

Creating Issues

Precise issue or file issue or line issue can be created by calling the related method in Issues.

Testing

Testing is made easy by the JavaPropertiesCheckVerifier. There are two ways to assert that an issue should be raised:

  • Through comments directly in the .properties test file
  • Or using assertions in the check class test

Examples of coding rule implementation and testing can be found in the Java Properties plugin jproperties-checks module.

About

Sample plugin that defines SonarQube custom rules for Java Properties files

License:GNU Lesser General Public License v3.0


Languages

Language:Java 97.6%Language:Shell 1.4%Language:HTML 1.0%