Skip to content

SONARCH-708, SONARJAVA-5497: Extend SonarJava CheckRegistrar API for registering custom file scanner hooks#5083

Merged
kaufco merged 6 commits intomasterfrom
marco/SONARCH-708
May 5, 2025
Merged

SONARCH-708, SONARJAVA-5497: Extend SonarJava CheckRegistrar API for registering custom file scanner hooks#5083
kaufco merged 6 commits intomasterfrom
marco/SONARCH-708

Conversation

@kaufco
Copy link
Copy Markdown
Contributor

@kaufco kaufco commented Apr 11, 2025

@kaufco kaufco force-pushed the marco/SONARCH-708 branch from 1a75e68 to b6c30f1 Compare April 11, 2025 16:15
@kaufco kaufco marked this pull request as ready for review April 11, 2025 16:43
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the change looks simple enough to integrated as-is, but there are a couple of questions I have left for you to look at.

In addition, I thin that he ticket attached to this PR should be a SONARJAVA ticket because this change implies a modification of the public API and should be properly documented as part of the release

Comment thread java-frontend/src/test/java/org/sonar/plugins/java/api/CheckRegistrarTest.java Outdated
Comment thread java-frontend/src/test/java/org/sonar/plugins/java/api/CheckRegistrarTest.java Outdated
Comment thread java-frontend/src/main/java/org/sonar/java/SonarComponents.java Outdated
@kaufco kaufco changed the title SONARCH-708 SonarJava: extend CheckRegistrar API for registering custom file scanner hooks SONARCH-708, SONARJAVA-5497: Extend SonarJava CheckRegistrar API for registering custom file scanner hooks Apr 17, 2025
@kaufco kaufco force-pushed the marco/SONARCH-708 branch 2 times, most recently from b223127 to c20a76d Compare April 17, 2025 15:27
@sonarqube-next
Copy link
Copy Markdown

@dorian-burihabwa-sonarsource
Copy link
Copy Markdown
Contributor

I think what bothers me most is the need to test an empty method and the lack of guidance for what a good implementation of registerCustomFileScanner should look like.

After discussing with @kaufco, it looks like we have 2 options to improve the current proposal.

  1. Improve the documentation of the new method to describe what a good implementation of registerCustomFileScanner should do and keep the test of the of the empty method (with a name that indicates that there is no expected behavior of the default implementation)
  2. Enforce some sort of contract by setting a return type and/or an exception type to registerCustomFileScanner

My preference goes for option 2 because it enables us to write a test where we can implement a child class and test what the default behavior is.

@Test
void registerCustomFileScanner_default_implementation_returns_expected_value() {
  class Child extends CheckRegistrar.RegistrarContext {
    // Nothing overridden here
  }
  var context = new Child();
  assertThat(context.registerCustomFileScanner(null, null)).isEqualTo(EXPECTED_VALUE);
}

@kaufco kaufco force-pushed the marco/SONARCH-708 branch from c20a76d to e486513 Compare April 25, 2025 15:20
@kaufco kaufco force-pushed the marco/SONARCH-708 branch from e486513 to e3731be Compare April 25, 2025 15:32
@sonarqube-next
Copy link
Copy Markdown

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, let's just change the test in SonarComponents to an end-to-end check that the scanner is actually invoked when registered through the new API

Comment thread java-frontend/src/test/java/org/sonar/java/SonarComponentsTest.java
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for coming back to this. I just left a question about the Javadoc and a small typo

* Registers a custom file scanner not related to any rule or repository.
* CheckRegistrars call this function to register a custom file scanner for execution during the analysis
* on all source files that match the given rule scope (MAIN, TEST or ALL).
* Custom file scanners reporting an issue will have no effect, since no rule is associated.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will have no effect: meaning that it is safe to report and not trigger any issue at runtime?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly. There's also a test for this. Reason: there was a null check somewhere in the implementation chain of reportIssue / addIssue (I don't remember in which class/function exactly) triggering the underlying low-level function only if ruleKey != null, doing nothing otherwise.

JavaFileScanner customScanner = scannerContext -> {
// empty
};
CheckRegistrar registrar =registrarContext ->
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
CheckRegistrar registrar =registrarContext ->
CheckRegistrar registrar = registrarContext ->

@sonarqube-next
Copy link
Copy Markdown

sonarqube-next Bot commented May 5, 2025

@kaufco kaufco merged commit db9bf9e into master May 5, 2025
14 of 15 checks passed
@kaufco kaufco deleted the marco/SONARCH-708 branch May 5, 2025 14:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants