1717package org .sonar .java .checks ;
1818
1919import java .util .List ;
20+ import java .util .Optional ;
2021import org .sonar .check .Rule ;
2122import org .sonar .java .model .DefaultModuleScannerContext ;
2223import org .sonar .java .reporting .AnalyzerMessage ;
2324import org .sonar .plugins .java .api .IssuableSubscriptionVisitor ;
25+ import org .sonar .plugins .java .api .JavaFileScannerContext ;
26+ import org .sonar .plugins .java .api .JavaVersion ;
27+ import org .sonar .plugins .java .api .JavaVersionAwareVisitor ;
28+ import org .sonar .plugins .java .api .location .Position ;
29+ import org .sonar .plugins .java .api .location .Range ;
30+ import org .sonar .plugins .java .api .tree .SyntaxToken ;
2431import org .sonar .plugins .java .api .tree .SyntaxTrivia ;
2532import org .sonar .plugins .java .api .tree .Tree ;
2633
2734@ Rule (key = "S7476" )
28- public class CommentsMustStartWithCorrectNumberOfSlashesCheck extends IssuableSubscriptionVisitor {
35+ public class CommentsMustStartWithCorrectNumberOfSlashesCheck extends IssuableSubscriptionVisitor implements JavaVersionAwareVisitor {
2936 private static final String BEFORE_JAVA_23 = "A single-line comment should start with exactly two slashes, no more." ;
30- private static final String AFTER_JAVA_23 = "Markdown documentation should start with exactly three slashes, no more." ;
37+ private static final String JAVA_23 = "Markdown documentation should start with exactly three slashes, no more." ;
3138 private static final String INCORRECT_SLASHES_BEFORE_JAVA_23 = "///" ;
32- private static final String INCORRECT_SLASHES_AFTER_JAVA_23 = "////" ;
39+ private static final String INCORRECT_SLASHES_JAVA_23 = "////" ;
40+ private static final Position FILE_START = Position .at (Position .FIRST_LINE , Position .FIRST_COLUMN );
41+ private Position compilationUnitFirstTokenPosition = FILE_START ;
42+
43+ @ Override
44+ public void setContext (JavaFileScannerContext context ) {
45+ super .setContext (context );
46+ compilationUnitFirstTokenPosition = Optional .ofNullable (context .getTree ())
47+ .map (Tree ::firstToken )
48+ .map (SyntaxToken ::range )
49+ .map (Range ::start ).orElse (FILE_START );
50+ }
51+
52+ @ Override
53+ public void leaveFile (JavaFileScannerContext context ) {
54+ compilationUnitFirstTokenPosition = FILE_START ;
55+ super .leaveFile (context );
56+ }
3357
3458 @ Override
3559 public List <Tree .Kind > nodesToVisit () {
@@ -38,6 +62,9 @@ public List<Tree.Kind> nodesToVisit() {
3862
3963 @ Override
4064 public void visitTrivia (SyntaxTrivia syntaxTrivia ) {
65+ if (isHeader (syntaxTrivia )) {
66+ return ;
67+ }
4168 if (syntaxTrivia .isComment (SyntaxTrivia .CommentKind .LINE ) && syntaxTrivia .comment ().startsWith (INCORRECT_SLASHES_BEFORE_JAVA_23 )) {
4269 var span = LineSpan .fromComment (syntaxTrivia , 0 , 0 , INCORRECT_SLASHES_BEFORE_JAVA_23 .length ());
4370 reportIssue (span , BEFORE_JAVA_23 );
@@ -48,17 +75,26 @@ public void visitTrivia(SyntaxTrivia syntaxTrivia) {
4875 for (int idx = 0 ; idx < lines .length ; idx ++) {
4976 String line = lines [idx ];
5077
51- if (line .trim ().startsWith (INCORRECT_SLASHES_AFTER_JAVA_23 )) {
52- int startPos = line .indexOf (INCORRECT_SLASHES_AFTER_JAVA_23 );
53- var span = LineSpan .fromComment (syntaxTrivia , idx , startPos , startPos + INCORRECT_SLASHES_AFTER_JAVA_23 .length ());
54- reportIssue (span , AFTER_JAVA_23 );
78+ if (line .trim ().startsWith (INCORRECT_SLASHES_JAVA_23 )) {
79+ int startPos = line .indexOf (INCORRECT_SLASHES_JAVA_23 );
80+ var span = LineSpan .fromComment (syntaxTrivia , idx , startPos , startPos + INCORRECT_SLASHES_JAVA_23 .length ());
81+ reportIssue (span , JAVA_23 );
5582 }
5683 }
5784 }
5885 }
5986
87+ @ Override
88+ public boolean isCompatibleWithJavaVersion (JavaVersion version ) {
89+ return version .isJava17Compatible ();
90+ }
91+
92+ private boolean isHeader (SyntaxTrivia syntaxTrivia ) {
93+ return syntaxTrivia .range ().start ().isBefore (compilationUnitFirstTokenPosition );
94+ }
95+
6096 private void reportIssue (LineSpan span , String message ) {
61- ((DefaultModuleScannerContext ) this .context ).reportIssue (issueSingleLine (span ,message ));
97+ ((DefaultModuleScannerContext ) this .context ).reportIssue (issueSingleLine (span , message ));
6298 }
6399
64100 private AnalyzerMessage issueSingleLine (LineSpan span , String message ) {
0 commit comments