Hi, I’m trying to create a Custom Sonar Java Rule and following the Writing Custom Java Rules 101.
Writing Custom Java Rules 101
==========
You are using SonarQube and its Java Analyzer to analyze your projects, but there aren't rules that allow you to target some of your company's specific needs? Then your logical choice may be to implement your own set of custom Java rules.
This document is an introduction to custom rule writing for the SonarQube Java Analyzer. It will cover all the main concepts of static analysis required to understand and develop effective rules, relying on the API provided by the SonarSource Analyzer for Java.
## Content
* [Getting Started](#getting-started)
* [Looking at the pom](#looking-at-the-pom)
* [Writing a rule](#writing-a-rule)
* [Three files to forge a rule](#three-files-to-forge-a-rule)
* [A specification to make it right](#a-specification-to-make-it-right)
* [A test file to rule them all](#a-test-file-to-rule-them-all)
* [A test class to make it pass](#a-test-class-to-make-it-pass)
* [First version: Using syntax trees and API basics](#first-version-using-syntax-trees-and-api-basics)
* [Second version: Using semantic API](#second-version-using-semantic-api)
* [What you can use, and what you can't](#what-you-can-use-and-what-you-cant)
* [Registering the rule in the custom plugin](#registering-the-rule-in-the-custom-plugin)
This file has been truncated. show original
I’m using these versions:
<sonarqube.version>7.9.6</sonarqube.version>
<sonarjava.version>6.3.2.22818</sonarjava.version>
My problem now is trying to read the package name since my custom rule does not apply to all classes but only classes from a specific package.
I tried changing line 20 to Kind.PACKAGE but the code doesn’t reach my Test1 on line 25.
Any advice?
Thanks!
Michael
(Michael Gumowski)
August 17, 2021, 2:03pm
2
Hey,
Can you share the content of the file you use for your test?
Registering to Tree.Kind.PACKAGE
means that your code is going to be triggered every-time the rule hit a package-declaration node, which is usually at the top of a compilation unit:
package org.foo; //<<< this is the PACKAGE node
class A {
// ...
}
Hope this helps,
Michael