Hi everyone,
currently I am trying to "fix" the "Methods should not be too complex" (
squid:MethodCyclomaticComplexity) rule for Java.
In our company, we always measured Cyclomatic Complexity NOT counting exit points (
cf. StackOverflow), and we want to stick to that mode while introducing SonarQube to everyone here.
I do not want to wait for
SONARJAVA-75 or convince the world to change their ways, so I want to implement a custom rule providing the changed calculation.
We already have our own Sonar plugin for external reporting purposes, so it seemed to be no big deal.
My naive approach was:
1. Create a new check class similar to org.sonar.java.checks.MethodComplexityCheck, with few differences:
- inherit from org.sonar.plugins.java.api.IssuableSubscriptionVisitor instead of org.sonar.java.checks.SubscriptionBaseVisitor
- include
inner class "FixedComplexityVisitor" extending
org.sonar.java.ast.visitors.ComplexityVisitor, but removing return and
throw statements from nodesToVisit()
- inline complexity calculation call to use the "FixedComplexityVisitor"
2.
Register the rule on server side (using an
org.sonar.squidbridge.annotations.AnnotationBasedRulesDefinition) and
batch side (using a org.sonar.plugins.java.api.CheckRegistrar like in
sonar-examples).
3. Activate the rule in current quality profile.
However I stumble into runtime errors like
here,
here or
here, because parts of the sonar-java-plugin are not part of the api available at runtime for me.
I
understand that publishing exactly the right java plugin api is no easy
task, so that probably still may take some time. (and that's OK, it
should be done right rather than quick)
So please help me: How can I realize my variant of
MethodCyclomaticComplexity now?
I
hope I do not have to fall back to other plugins like CheckStyle, or
re-code all non-published classes of the sonar-java-plugin.
Best regards,
Klaus Lutterjohann