Getting the package name Java Custom Sonar Rule

Hi, I’m trying to create a Custom Sonar Java Rule and following the Writing Custom Java Rules 101.

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!

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