SONARJAVA-6944: Implement rule S9387 TestNG Javadoc tags should be converted to annotations - #6124
Conversation
…nverted to annotations Detect TestNG-specific Javadoc tags (@test, @beforeMethod, @afterMethod, etc.) used as configuration markers instead of proper TestNG annotations (@test, @BeforeMethod, @AfterMethod, etc.). Case-insensitive matching with support for all 15 TestNG tags and secondary locations for multiple tags per method. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Addresses code review feedback: 1. Single-line Javadoc `/** @test */` is now detected by updating BLOCK_TAG_PATTERN to match tags following the `/**` opener 2. False positives from annotation examples in `<pre>` and `{@code}` blocks are eliminated by removing those regions before pattern matching 3. Issue and secondary locations now report precisely on the method name using `simpleName()` and avoid redundant operations 4. Type-only tags (`@beforeClass`, `@afterClass`, `@parameters`, `@listeners`) removed from method-level checks since they cannot be applied to methods Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…icate test @BeforeClass and @afterclass are method-level annotations in TestNG (@target(METHOD)), not type-only. The previous commit incorrectly removed them from the tag map. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Revert manually added @beforeClass/@afterclass entries in S9387.html (auto-generated from RSPEC, should not be hand-edited) - Move @beforeClass/@afterclass noncompliant test cases above the "Compliant cases" section for clarity - Add secondary location assertion to multipleTestNGTags test case Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Replace \s with [\t ] in regex patterns to avoid super-linear backtracking (SonarQube S8786). Use tree.simpleName() for secondary locations to match the primary issue location scope. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…in S9387 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
nathsou
left a comment
There was a problem hiding this comment.
The rule is well structured. I left four non-blocking coverage and false-positive concerns.
| "ruleSpecification": "RSPEC-9387", | ||
| "sqKey": "S9387", | ||
| "scope": "All", | ||
| "quickfix": "unknown", |
There was a problem hiding this comment.
This is a TestNG test-configuration rule, but All also runs it on production methods where tags such as @factory may be ordinary documentation. Sibling checks under checks/tests use the Tests scope. Could we switch this to Tests and regenerate the derived metadata/profile files?
|
|
||
| while (matcher.find()) { | ||
| String tagName = matcher.group(1); | ||
| String annotation = TESTNG_TAGS_TO_ANNOTATIONS.get(tagName.toLowerCase(Locale.ROOT)); |
There was a problem hiding this comment.
Lowercasing makes annotation names indistinguishable from the legacy case-sensitive tags. For example, documentation containing @Test is the annotation used by TestNG is treated as obsolete @test. Could we match the documented legacy spellings exactly (@test, @beforeMethod, etc.) and keep uppercase annotation names compliant?
|
|
||
| private static final Pattern BLOCK_TAG_PATTERN = Pattern.compile("(?:^|/\\*\\*)[\\t ]*+\\*?[\\t ]*+@(\\w+)", Pattern.MULTILINE); | ||
| private static final Pattern PRE_BLOCK_PATTERN = Pattern.compile("<pre>.*?</pre>", Pattern.DOTALL); | ||
| private static final Pattern CODE_TAG_PATTERN = Pattern.compile("\\{@code[\\t ][^}]*+\\}"); |
There was a problem hiding this comment.
| .filter(trivia -> trivia.isComment(SyntaxTrivia.CommentKind.JAVADOC)) | ||
| .forEach(trivia -> checkJavadoc((MethodTree) tree, trivia)); | ||
| } | ||
|
|
There was a problem hiding this comment.
Java 23 Markdown documentation comments are represented as SyntaxTrivia.CommentKind.MARKDOWN, so /// @test is currently skipped even though it is still a documentation-based declaration. If the rule is intended to cover all Java documentation comments, could we inspect MARKDOWN trivia too, as the existing documentation checks do?
This comment has been minimized.
This comment has been minimized.
Add missing 'tests' tag for Tests-scoped rule, move test sample to test sources path, and remove ineffective DOTALL flag from regex. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
✅ All code review findings resolved.
Code Review ✅ Approved 10 closed / 10 findingsImplements rule S9387 to detect TestNG-specific Javadoc tags ( ✅ 10 closed✅ Bug: Single-line Javadoc
|
| Auto-apply | Compact |
|
|
Was this helpful? React with 👍 / 👎 | Gitar
|




Summary
@test,@beforeMethod,@afterMethod,@beforeClass,@afterClass,@dataProvider, etc.) used as configuration markers instead of proper TestNG annotationsTest plan
🤖 Generated with Claude Code
Agent workflow
Tool link: Tool link: https://github.com/SonarSource/languages-experimental-tooling/tree/romain/my-tickets/personal/romain-brenguier
Addressed review comments in pr_report_6124.md using
uv run address_reviews.py pr_report_6124.md