LinesOfCodeVisitor: ClassNotFoundException

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 am using:

  • Windows 10 OS
  • SonarQube 7.9.4 (with Java plugin version 5.13)

I’d appreciate any help in resolving this issue.

Hi,

Welcome to the community!

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.

 
HTH,
Ann

1 Like

Hi,

Basically, you can’t use this class. As stated on the " Writing Custom Java Rules 101" doc:

What you can use, and what you can’t

When writing custom Java rules, you can only use classes from package org.sonar.plugins.java.api .

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.

2 Likes

Thank you for your answer. So, if I wanted to count the number of lines in a custom rule, there’s no way to do so?

Sure there is! But you’ll have to implement it yourself. (Nothing says you can’t copy/paste, or at least be “inspired” by that Visitor.)

 
:smiley:
Ann

1 Like

I will try to do. Thank you all for the answers :smiley: