I’m new to SonarQube, and I’ve been working on different Java custom rules lately.
One of these rules determines whether a file’s lines of code exceed a set threshold or not.
In it, I’m using the “org.sonar.java.ast.visitors.LinesOfCodeVisitor” class.
I can test and build the project with no issues. However, upon trying to use Sonar Scanner to test the new rule, I stumble upon this error:
[ERROR] Failed to execute goal org.sonarsource.scanner.maven:sonar-maven-plugin:3.9.0.2155:sonar (default-cli) on project greensight-demo: Execution default-cli of goal org.sonarsource.scanner.maven:sonar-maven-plugin:3.9.0.2155:sonar failed: A required class was missing while executing org.sonarsource.scanner.maven:sonar-maven-plugin:3.9.0.2155:sonar: org/sonar/java/ast/visitors/LinesOfCodeVisitor
Digging more into this, I found this to be the cause:
Caused by: java.lang.ClassNotFoundException: org.sonar.java.ast.visitors.LinesOfCodeVisitor
I’ve moved this to the ‘Writing rules’ category to draw the attention of the right folks. I’m not the “right folks” in this context, but I can go ahead & tell you that unless that Visitor was publicly exposed as an API (I’m skeptical) then it’s not going to be available at runtime & your rule won’t run.
When browsing the existing 600+ rules from the SonarSource Analyzer for Java, you will sometime notice use of some other utility classes, not part of the API. While these classes could be sometime extremely useful in your context, these classes are not available at runtime for custom rule plugins. It means that, while your unit tests are still going to pass when building your plugin, your rules will most likely make analysis crash at analysis time .
Others packages are not exposed to custom plugins in the runtime.