SONARJAVA-6827: Implement S9357 Anonymous classes on functional interfaces should be lambdas - #5995
Conversation
…faces should be lambdas Detects anonymous inner classes implementing functional interfaces (single abstract method) that can be replaced with lambda expressions. Mirrors the detection logic of S1604 but applies to all code (main and test scope). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This comment has been minimized.
This comment has been minimized.
|
❌ Ruling needs updating. A fix PR has been created: #5996 Please review and merge it into your branch. |
Extract shared detection logic into AbstractAnonymousClassToLambdaCheck base class used by both AnonymousClassOnFunctionalInterfaceCheck (S9357) and AnonymousClassShouldBeLambdaCheck (S1604). Change S9357 scope from "All" to "Tests" to avoid double-reporting on main code where S1604 already applies. Move S9357 test samples to test source path accordingly. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
❌ Ruling needs updating. A fix PR has been created: #5996 Please review and merge it into your branch. |
Rules with scope "Tests" must include the "tests" tag to pass GeneratedCheckListTest validation. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Ruling Diff SummaryDetected changes in 3 rule files: 0 issues removed, 71 issues added. S9357 (
|
The abstract base class extracted in the refactoring commit is counted by the file-scanning logic but excluded from the generated check list (no @rule annotation), causing a count mismatch in GeneratedCheckListTest. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
| import org.sonar.plugins.java.api.tree.Tree; | ||
| import org.sonar.plugins.java.api.tree.TypeTree; | ||
|
|
||
| public abstract class AbstractAnonymousClassToLambdaCheck extends BaseTreeVisitor implements JavaFileScanner, JavaVersionAwareVisitor { |
There was a problem hiding this comment.
Don't declare any abstract class. This introduce coupling between rules. To avoid duplicating code, you may use utility classes.
…upling Replace AbstractAnonymousClassToLambdaCheck with AnonymousClassToLambdaUtils utility class. Both AnonymousClassShouldBeLambdaCheck (S1604) and AnonymousClassOnFunctionalInterfaceCheck (S9357) are now standalone checks that delegate shared logic to the utility class. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…upling Move the shared visitor logic from AnonymousClassShouldBeLambdaCheck and AnonymousClassOnFunctionalInterfaceCheck into a static method in AnonymousClassToLambdaUtils. Both checks are now thin wrappers that delegate to the utility, eliminating code duplication without introducing an abstract class coupling. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
|
||
| @Override | ||
| public boolean isCompatibleWithJavaVersion(JavaVersion version) { | ||
| return version.isJava8Compatible(); |
There was a problem hiding this comment.
[P2] Exclude S9357 from AutoScan\n\nAutoScan generally lacks sonar.java.source, so an unspecified version is treated as Java 8-compatible. S9357 may therefore recommend lambdas in Java 7 test code, where lambdas are unsupported. S1604 is already excluded from AutoScan for this exact reason. Add AnonymousClassOnFunctionalInterfaceCheck.class alongside it in JAVA_CHECKS_NOT_WORKING_FOR_AUTOSCAN.
AutoScan lacks sonar.java.source, so unspecified version is treated as Java 8-compatible. S9357 may recommend lambdas in Java 7 test code where lambdas are unsupported. This mirrors the existing S1604 exclusion. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Code Review ✅ Approved 3 resolved / 3 findingsImplements rule S9357 to flag anonymous classes on functional interfaces in test code, addressing the check duplication and double-reporting findings by extracting shared base logic with S1604. No issues found. ✅ 3 resolved✅ Quality: New check duplicates S1604 logic verbatim
✅ Bug: S9357 double-reports with S1604 on main code
✅ Bug: S9357 check class may not be registered in the check list
Implementation Status ✅ 1 of 1 objectives covered✅ SONARJAVA-6827 - 1 of 1 objectives coveredThis PR implements rule S9357 to detect anonymous classes on functional interfaces that should be lambdas. ✅ 1 covered here
OptionsAuto-apply is off → Gitar will not commit updates to this branch. Comment with these commands to change the behavior for this request:
Was this helpful? React with 👍 / 👎 | Gitar |
|




Summary
Implement S9357: Anonymous classes on functional interfaces should be lambdas.
AbstractAnonymousClassToLambdaCheckbase class, used by both S9357 and S1604, eliminating code duplicationTest plan
Function<Object, Date>test case unrelated to this change)JAVA_TEST_CHECKSand S1604 inJAVA_MAIN_CHECKS