Error writing Java custom rule

This error is reported when I follow the steps to write the rules and test them.:


When I debug, I am fully powered off, but no exceptions are found at the breakpoint, including super. Visitclass (tree);
So I don’t know what’s wrong. I use jdk16. Here’s my POM environment:
<sonarqube.version>8.9.0.43852</sonarqube.version<sonarjava.version>6.15.1.26025</sonarjava.version>

In addition, the code can be built successfully, and the rules can be found in the coding_rules of sonarqube, but no errors can be scanned.

content.issues is not null, but it fails in the end

When I use the verifyNoIssues() method in the test file, my customized error message will be displayed, but the test still fails

Hello @KevinJin,

Sorry for the very late reply. Did you find a solution to this unexpected issue being reported?

Thank you for your reply. @Dorian_Burihabwa
So far, I have no solution.
Could you kindly provide me some help?

It is not easy to help you without the sources for your check or the logic for your rule, because I cannot tell how far apart they are.

Make sure that you are pointing to the right test file: the debug screenshot you posted looks unrelated from the test class screenshot (the first shows an issue on WeekDayEnumJava and the second points to LimitMethodRows.java).

It would help if you could share:

  • A description of the rule you are trying to implement
  • An example of compliant and non-compliant source code
  • The code for your check

This is the project address I posted on GitHub[sonar-java](https://github.com/lsttong/sonar-java)
Where the path of the package of the code rule is /docs/java-custom-rules-example/src/main/java/org.sonar.samples.java/checks.
The original intention of EnumClassNameCheck’s custom rule file is to find the names of all enum classes and check whether they have the suffix “Enum”.The standard naming method should be TestEnum.
Thank you for your generous reply.
@Dorian_Burihabwa

I think the customized rules are actually very simple. The code when I write the rules is also very simple. I think there should be a problem with the process or preconditions. Please help me see that my source code has been released in GitHub

Hey @KevinJin,
Sorry for the late reply. I had a quick look at the code, and it looks to me like the issues in your test code are not properly identified with a // Noncompiliant comment on the same line.

enum Country { // Noncompliant
...

With comments on the problematic lines, the testing harness can verify if issues are raised at the right location (beware of this when reformatting the file).

I hope this helps.

1 Like

Hi @Dorian_Burihabwa
I’m sorry to reply to you so late.
When I execute the compilation command “mvn clean install -f pom.xml” according to your suggestion, an error occurs. I intercept the last part of the error content. The Chinese meaning in the blue box in the figure means that the release version 16 is not supported, and the red box means my Maven version and Java version. What’s the problem?

The error message is explicit, you will need Java 16 to build the project.
Java version requirements are documented in sonar-java’s README.

I’m sorry to ask such a stupid question, because I forgot to compile with the version of 16 because of my work a long time ago. Thank you for your reply

hi @Dorian_Burihabwa
After I successfully compiled and put the jar package into the plugins, I still can’t detect the problems I expect in the code. Moreover, when I use the verify() method, the ‘issues’ attribute in his context object has the contents I expect, but the results I expect will not be reported after the process. This result occurs when I use the verifyNoIssues() method.
Does the above problem cause the bug content I want to scan the project with sonarqube to not appear?How can I make the custom rule plug-in play the effect I expect?



This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.