Question

Hello i was trying to find a way to implement a custom java rule for SonarQube 4.1.

From the website i found the example plugin on github with an ExampleCheck based on JavaFileScanner.

However i have several serious problems to get the things to work. For now i hope one can at least help me to get the sample to work in SonarQube 4.1.

compiling and deploying does not work as it depends on sonar-plugin-api 4.1.1 and so on startup SonarQube complaints about exactly that

ERROR [o.s.s.p.PlatformLifecycleListener]  Fail to start server
java.lang.IllegalStateException: Plugin javacustomrules needs a more recent version of SonarQube than 4.1. At least 4.1.1 is expected

changing the dependency to 4.1 builds and let sonarqube start. I can select the rule in the QualityProfile and run:

mvn sonar:sonar

But this ends in the following error:

...
Caused by: org.sonar.squid.api.AnalysisException: SonarQube is unable to analyze file : 'C:\projects\...MyClass.java'
        at org.sonar.java.ast.AstScanner.scan(AstScanner.java:102)
        at org.sonar.java.JavaSquid.scanSources(JavaSquid.java:135)
        at org.sonar.java.JavaSquid.scan(JavaSquid.java:129)
    Caused by: java.lang.ClassCastException: org.sonar.java.model.JavaTree$CompilationUnitTreeImpl cannot be cast to org.sonar.java.model.JavaTree$CompilationUnitTreeImpl
            at org.sonar.samples.java.PrinterVisitor.scan(PrinterVisitor.java:66)
            at org.sonar.samples.java.PrinterVisitor.print(PrinterVisitor.java:34)
            at org.sonar.samples.java.ExampleCheck.scanFile(ExampleCheck.java:49)
            at org.sonar.java.model.VisitorsBridge.visitFile(VisitorsBridge.java:87)
            at com.sonar.sslr.impl.ast.AstWalker.walkAndVisit(AstWalker.java:67)
            at org.sonar.java.ast.AstScanner.scan(AstScanner.java:95)
  ... 62 more

the corresponding code looks ok for me, as there is a cast from JavaTree.CompilationUnitTreeImpl to JavaTree.

There was no history of the TestCase on GitHub so i don't see what might have changed. Or are there serious problems with SonarQube 4.1 and it is highly recommended to update?

Was it helpful?

Solution

I am the one who wrote this example and in fact it is messy in many ways.

Short answer to your question is : no major trouble with version 4.1, get latest version of example (corrected a few minutes ago) and you should be fine : https://github.com/SonarSource/sonar-examples/tree/master/plugins/java-custom-rules

To understand what was happening:

The AST in the java plugin is an API that is not complete. In order to provide a nice way to visualize the AST I provided a PrinterVisitor in the example and intended to display line numbers associated with the nodes (hence the cast into JavaTree) however I messed up with the dependency and this raise the trouble you encountered (basically, not loading the class from the correct dependency), moreover the trouble is that in order to get this line I had to leak the ASTNode abstraction into this example and this is definitely not intended to be part of public API.

Therefore the quick correction here was to remove this display of line number to make this example works correctly. The longer correction will take some more time to think of the best API design for this need. We plan to make PrinterVisitor have its way back in public API at some point of time.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top