- Writing custom rules in Java via a SonarQube plugin
- Adding XPath rules directly through the SonarQube web interface
Note that the first way must be preferred when available because the support of XPath will be eventually droppedJava API, if available, will be more fully-featured than what's available for XPath, and is generally preferable.
Before implementing a new coding rule, you should consider whether it is specific to your own context or might benefit others. If it might benefit others, you can propose them on the developer list. If the SonarQube team find your proposed rules interesting, they may be implemented directly in the related language plugin. It means less maintenance for you, and benefit to others.
The rules must be written in XPath (version 1.0) to navigate the language's Abstract Syntax Tree (AST). For each language, an SSLR Toolkit is provided to help you navigate the AST. Each language's SSLR Toolkit is a standalone application that displays the AST for a piece of code source that you feed into it, allowing you to read the node names and attributes from your code sample and write your XPath expression. The proper SSLR Toolkit can be downloaded from the language plugin page. So, knowing the Knowing the XPath language is the only prerequisite, and there are a lot of tutorials on XPath online (see http://www.w3schools.com/xpath/ for example).
The proper SSLR Toolkit can be downloaded from the language plugin page or here: