Skip to content

Commit 23c3c59

Browse files
mahfouz72romani
authored andcommitted
Issue #16641: FileContents.getJavadocBefore should skip block comments only if it is alone in line
1 parent 9fb5504 commit 23c3c59

12 files changed

Lines changed: 88 additions & 12 deletions

File tree

src/main/java/com/puppycrawl/tools/checkstyle/api/FileContents.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,14 @@ private boolean lineInsideBlockComment(int lineNo) {
240240
.flatMap(List::stream)
241241
.filter(comment -> !javadocComments.containsValue(comment))
242242
.anyMatch(comment -> {
243-
return lineNo >= comment.getStartLineNo()
244-
&& lineNo <= comment.getEndLineNo();
243+
final boolean lineInSideBlockComment = lineNo >= comment.getStartLineNo()
244+
&& lineNo <= comment.getEndLineNo();
245+
boolean lineHasOnlyBlockComment = true;
246+
if (comment.getStartLineNo() == comment.getEndLineNo()) {
247+
final String line = line(comment.getStartLineNo() - 1).trim();
248+
lineHasOnlyBlockComment = line.startsWith("/*") && line.endsWith("*/");
249+
}
250+
return lineInSideBlockComment && lineHasOnlyBlockComment;
245251
});
246252
}
247253

src/test/java/com/puppycrawl/tools/checkstyle/checks/javadoc/JavadocStyleCheckTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,7 @@ public void testJavadocStyleCheck4() throws Exception {
827827
public void testJavadocStyleAboveComments() throws Exception {
828828
final String[] expected = {
829829
"13: " + getCheckMessage(MSG_NO_PERIOD),
830+
"20: " + getCheckMessage(MSG_NO_PERIOD),
830831
};
831832

832833
verifyWithInlineConfigParser(

src/test/java/com/puppycrawl/tools/checkstyle/checks/javadoc/JavadocTypeCheckTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,7 @@ public void testJavadocType2() throws Exception {
475475
public void testJavadocTypeAboveComments() throws Exception {
476476
final String[] expected = {
477477
"15:1: " + getCheckMessage(MSG_MISSING_TAG, "@author"),
478+
"41:15: " + getCheckMessage(MSG_MISSING_TAG, "@author"),
478479
};
479480
verifyWithInlineConfigParser(
480481
getPath("InputJavadocTypeAboveComments.java"), expected);

src/test/java/com/puppycrawl/tools/checkstyle/checks/javadoc/JavadocVariableCheckTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626

2727
import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
2828
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
29-
import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
3029

3130
public class JavadocVariableCheckTest
3231
extends AbstractModuleTestSupport {
@@ -376,7 +375,9 @@ public void testMethodInnerClass() throws Exception {
376375

377376
@Test
378377
public void testJavadocVariableAboveComment() throws Exception {
379-
final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
378+
final String[] expected = {
379+
"23:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
380+
};
380381
verifyWithInlineConfigParser(
381382
getPath("InputJavadocVariableAboveComment.java"),
382383
expected);

src/test/java/com/puppycrawl/tools/checkstyle/checks/javadoc/MissingJavadocMethodCheckTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,7 @@ public void missingJavadoc() throws Exception {
533533
public void testMissingJavadocMethodAboveComments() throws Exception {
534534
final String[] expected = {
535535
"18:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
536+
"36:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
536537
};
537538
verifyWithInlineConfigParser(
538539
getPath("InputMissingJavadocMethodAboveComments.java"),

src/test/java/com/puppycrawl/tools/checkstyle/checks/javadoc/MissingJavadocTypeCheckTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,7 @@ public void testQualifiedAnnotationWithParameters() throws Exception {
440440
public void testMissingJavadocTypeAboveComments() throws Exception {
441441
final String[] expected = {
442442
"13:1: " + getCheckMessage(MSG_JAVADOC_MISSING),
443+
"27:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
443444
};
444445
verifyWithInlineConfigParser(
445446
getPath("InputMissingJavadocTypeAboveComments.java"),

src/test/resources/com/puppycrawl/tools/checkstyle/checks/javadoc/javadocmethod/InputJavadocMethodAboveComments.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,21 @@ public int method4() throws Exception { // violation, 'Expected @throws tag for
9999
public int foo5() {
100100
return 0;
101101
}
102+
103+
/**
104+
* A Javadoc comment.
105+
* @return 0
106+
*/
107+
/*@ A JML Annotation */ public int foo6() { return 0; }
108+
109+
110+
public void foo7() throws Exception { }
111+
112+
/**
113+
* A Javadoc comment.
114+
* @return 0
115+
*/
116+
public int foo8() { return 0; } /* @ A JML Annotation */
117+
118+
public void foo9() throws Exception { }
102119
}

src/test/resources/com/puppycrawl/tools/checkstyle/checks/javadoc/javadocstyle/InputJavadocStyleAboveComments.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,12 @@ public class InputJavadocStyleAboveComments {
1515
field
1616
*/
1717
public String field;
18+
19+
// violation below, 'First sentence should end with a period'
20+
/**
21+
* A Javadoc comment
22+
*/
23+
/* package */ String field2;
24+
25+
private String field3;
1826
}

src/test/resources/com/puppycrawl/tools/checkstyle/checks/javadoc/javadoctype/InputJavadocTypeAboveComments.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,18 @@ public class InputJavadocTypeAboveComments {
2626
class MyClass {
2727

2828
}
29+
30+
/**
31+
* Test class for variable naming in for each clause.*
32+
* @author Mohamed Mahfouz
33+
*/
34+
class MyClass2 /* Comment */{
35+
36+
}
37+
38+
/**
39+
* Test class for variable naming in for each clause.*
40+
*/
41+
/* Comment */ class MyClass3 {
42+
// violation above, 'Type Javadoc comment is missing @author tag'
43+
}

src/test/resources/com/puppycrawl/tools/checkstyle/checks/javadoc/javadocvariable/InputJavadocVariableAboveComment.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,12 @@ public class InputJavadocVariableAboveComment {
1313
test comment
1414
*/
1515
public int variablePublic;
16+
17+
18+
/**
19+
* A package variable
20+
*/
21+
/* package */ int variablePackage;
22+
23+
public int x; // violation, 'Missing a Javadoc comment.'
1624
}

0 commit comments

Comments
 (0)