Sonarqube Custom Plugin API Documentation

Hi. so I’ve been following the steps here in order to create a custom rule. Everything went ok, except for the part when i tried to run the unit tests from Intelij. leaving that aside, even thought i’ve managed to write the plugin to work correctly, i’m struggling to understand the api. Like how the nodes fit together, what’s a symbol, what does qualifiedIdentifier mean and so on.

  1. Is there no documentation about all of this stuff?

  2. And since i’m here, how can i debug, like with breakpoints. I’ve tried to run a unit test in intelij and it did’t reach the breakpoints in the rule file. Thought i’m not sure if it wasn’t working or just didn’t reach there because of this:
    https://pastiebin.com/5dbabd23ec40e

Hi Alexandru,

I was researching this as well a few weeks back and may be able to answer your questions in part. Perhaps one of the developers can answer you better.

  1. The documentation is there, but it’s not obvious how to look for it. To better understand what everything is and does you need some Google-fu and to experiment with the code itself.

    Giving particulars about what you mentioned:

    • I suggest you create a rule that implements BaseTreeVisitor and have a look at the Base Tree with the debugger, see how all the different nodes are nested within each other.

    • You know how IntelliJ is able to recognise the type of an object, or take you to the declaration of an imported library? The symbol contains that information. E.g. you can compare two variable declarations of the same object in two different methods by using their symbols:

      void methodA() {
          Foo foo;
      }
      
      void methodB() {
          Foo bar;
      }
      

      Here, the symbol for foo would be “variable foo of type Foo in methodA”, the symbol for bar would be “variable bar of type Foo in methodB”. You can compare if both are of the same type.

      As with the Base Tree, have a look at the symbols to see what is it they contain.

    • I haven’t played with the qualifiedIdentifier, but it sounds like it’s related to the symbol. I’ve used qualifiedName to get a class’ full name, including its package declaration.

  2. I also had trouble debugging with IntelliJ, and I think you’re having the same problem I had. I was able to debug running the tests from Maven instead of running the tests themselves, all you need is to enable debugging when running tests. Create a new Run configuration and add the following to the command line:

    test -DforkCount=0 -DreuseForks=false -f pom.xml

    Then simply debug the new configuration:

I hope this helps a little

1 Like

Thanks for the reply. Regarding your #1 point, well i guess so because that’s what I’ve been doing when i’ve developed my plugin. And about the second point, nice, it worked :smiley: now it’ll be more easier to understand the code by simply run it. :+1:

1 Like