Custom plugin java rules don't detect any bugs

Hello eveybody !

I have implemented a Sonar Plugin (from Sonar version 6.7) for creating a new rule.
In a nutshell this rule should detect if a third party library has been used in some classes (e.g. importing some specific API packages). If yes AND the class using this third party API does not implement a particular interface or does not extend another abstract class, then a bug is fired.
This is working perfectly in the unit tests. The rules are working fine.

Then I deploy the plugin and restart the Sonar Server (Version 6.7.6 (build 38781)).
I can see the rules and their descriptions in the Web UI Management.
I attach them to a quality profile which inherits from the “Sonar way - java” and activate them.
I restart the server.
As a first test, I create a small java project which contains one class which is known for generating the bugs related to my new rules. This is a Class I have also tested in my plugin unit tests.
I attach also this project to my quality profile which contains my rules.
After a successful maven “mvn clean install sonar:sonar” run I get a look at the result in sonar: none of my rules has generated a bug.

I see in the sonar logs that the right quality profile was used ([INFO] Quality profile for java: fintech).
I have tried with several projects, known also for containing buggy classes: same thing. My plugin rules do not detect any problem.

So what I have missed? I have followed the following resources for creating this plugin:

My Plugin is set with the sonar-packaging-maven-plugin version 1.17

I have followed the example from https://github.com/SonarSource/sonar-custom-rules-examples/tree/master/java-custom-rules/ :
I have a Plugin entry class (com.axonivy.sonar.DocFactorySonarPlugin), which adds an extension to the context. The extension loads the rules definition which lists the JavaCheck rules list.

Is that a well known “beginner” error? Is there something special to do for activating these new rules?

Thanks a lot for your help
Emmanuel

Hi everybody,

I have found the origin of the issue: I have forgotten to register the CheckRegistrar in the Plugin Context.
I think that this point is may be missing in the https://docs.sonarqube.org/display/PLUG/Writing+Custom+Java+Rules+101#WritingCustomJavaRules101-RuleActivation paragraph.
Thanks to the ones who have developed the great Sonar plugin examples :wink:
Here is the code which has the solution:
https://github.com/SonarSource/sonar-custom-rules-examples/blob/master/java-custom-rules/src/main/java/org/sonar/samples/java/MyJavaRulesPlugin.java

I will definitely have to dive a little bit more in the API for getting more confident with writing new plugins.

Cheers
Emmanuel

1 Like

Hey @ultimatemanu,

Thanks a lot for the feedback, and congrats on solving your issue on your side.
Based on your comment, I updated the tutorial page to add a small part about the CheckRegistrar.

Happy rule coding,

Michael

1 Like

Hi @Michael
Thanks a lot! That’s perfect and it will surely save some times to others newbies like me :wink:
Cheers
Emmanuel