Skip to content

Commit d2f4ac8

Browse files
committed
Check for "(non-Javadoc)" comments
Closes gh-186
1 parent 80f66bb commit d2f4ac8

File tree

4 files changed

+90
-0
lines changed

4 files changed

+90
-0
lines changed

spring-javaformat/spring-javaformat-checkstyle/src/main/java/io/spring/javaformat/checkstyle/check/SpringJavadocCheck.java

+50
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,16 @@
1818

1919
import java.util.ArrayList;
2020
import java.util.Collections;
21+
import java.util.HashMap;
2122
import java.util.HashSet;
2223
import java.util.List;
24+
import java.util.Map;
2325
import java.util.Set;
2426
import java.util.regex.Matcher;
2527
import java.util.regex.Pattern;
2628

2729
import com.puppycrawl.tools.checkstyle.api.DetailAST;
30+
import com.puppycrawl.tools.checkstyle.api.FileContents;
2831
import com.puppycrawl.tools.checkstyle.api.TextBlock;
2932
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
3033

@@ -55,6 +58,8 @@ public class SpringJavadocCheck extends AbstractSpringCheck {
5558

5659
private static final Pattern AT_TAG_PATTERN = Pattern.compile("@\\w+\\s+.*");
5760

61+
private static final Pattern NON_JAVADOC_COMMENT = Pattern.compile("\\(non-Javadoc\\)", Pattern.CASE_INSENSITIVE);
62+
5863
private static final Set<Integer> TOP_LEVEL_TYPES;
5964
static {
6065
Set<Integer> topLevelTypes = new HashSet<Integer>();
@@ -69,6 +74,10 @@ public class SpringJavadocCheck extends AbstractSpringCheck {
6974

7075
private boolean publicOnlySinceTags;
7176

77+
private boolean allowNonJavadocComments;
78+
79+
private Map<Integer, TextBlock> blockComments;
80+
7281
@Override
7382
public int[] getDefaultTokens() {
7483
return new int[] { TokenTypes.INTERFACE_DEF, TokenTypes.CLASS_DEF, TokenTypes.ENUM_DEF,
@@ -82,13 +91,38 @@ public int[] getAcceptableTokens() {
8291
TokenTypes.ANNOTATION_FIELD_DEF };
8392
}
8493

94+
@Override
95+
public void beginTree(DetailAST rootAST) {
96+
super.beginTree(rootAST);
97+
this.blockComments = new HashMap<>();
98+
FileContents contents = getFileContents();
99+
for (List<TextBlock> blockComments : contents.getBlockComments().values()) {
100+
for (TextBlock blockComment : blockComments) {
101+
this.blockComments.put(blockComment.getEndLineNo(), blockComment);
102+
}
103+
}
104+
}
105+
85106
@Override
86107
public void visitToken(DetailAST ast) {
87108
int lineNumber = ast.getLineNo();
88109
TextBlock javadoc = getFileContents().getJavadocBefore(lineNumber);
89110
if (javadoc != null) {
90111
checkJavadoc(ast, javadoc);
91112
}
113+
if (!this.allowNonJavadocComments) {
114+
checkForNonJavadocComments(javadoc);
115+
checkForNonJavadocComments(getBlockCommentBefore(lineNumber));
116+
}
117+
}
118+
119+
public TextBlock getBlockCommentBefore(int lineNoBefore) {
120+
FileContents contents = getFileContents();
121+
int lineNo = lineNoBefore - 1;
122+
while (lineNo > 0 && (contents.lineIsBlank(lineNo) || contents.lineIsComment(lineNo))) {
123+
lineNo--;
124+
}
125+
return this.blockComments.get(lineNo);
92126
}
93127

94128
private void checkJavadoc(DetailAST ast, TextBlock javadoc) {
@@ -167,6 +201,18 @@ private boolean startsWithUppercase(String description) {
167201
return description.length() > 0 && Character.isUpperCase(description.charAt(0));
168202
}
169203

204+
private void checkForNonJavadocComments(TextBlock block) {
205+
if (block == null) {
206+
return;
207+
}
208+
String[] text = block.getText();
209+
for (int i = 0; i < text.length; i++) {
210+
if (NON_JAVADOC_COMMENT.matcher(text[i]).find()) {
211+
log(block.getStartLineNo() + i - 1, 0, "javadoc.nonJavadocComment");
212+
}
213+
}
214+
}
215+
170216
public void setRequireSinceTag(boolean requireSinceTag) {
171217
this.requireSinceTag = requireSinceTag;
172218
}
@@ -175,6 +221,10 @@ public void setPublicOnlySinceTags(boolean publicOnlySinceTags) {
175221
this.publicOnlySinceTags = publicOnlySinceTags;
176222
}
177223

224+
public void setAllowNonJavadocComments(boolean allowNonJavadocComments) {
225+
this.allowNonJavadocComments = allowNonJavadocComments;
226+
}
227+
178228
private DetailAST getInterfaceDef(DetailAST ast) {
179229
return findParent(ast, TokenTypes.INTERFACE_DEF);
180230
}

spring-javaformat/spring-javaformat-checkstyle/src/main/resources/io/spring/javaformat/checkstyle/check/messages.properties

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ javadoc.bannedTag=Javadoc tag ''{0}'' should not be used.
1111
javadoc.missingSince=Missing Javadoc @since tag.
1212
javadoc.publicSince=Javadoc @since tag should not be used on private classes.
1313
javadoc.emptyLineBeforeTag=Method Javadoc should not have empty line before tag.
14+
javadoc.nonJavadocComment=Comments should not include \"(non-Javadoc)\".
1415
junit5.bannedImport=Import ''{0}'' should not be used in a JUnit 5 test.
1516
junit5.bannedTestAnnotation=JUnit 4 @Test annotation should not be used in a JUnit 5 test.
1617
junit5.lifecyclePrivateMethod=Lifecycle method ''{0}'' should not be private.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
+23:1: Comments should not include "(non-Javadoc)"
2+
+30:1: Comments should not include "(non-Javadoc)"
3+
+2 errors
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2017-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/**
18+
* Example with non javadoc comments.
19+
*
20+
* @author Phillip Webb
21+
*/
22+
public class JavadocNonJavadocComment {
23+
24+
/* (non-Javadoc)
25+
* Example.
26+
*/
27+
public void one() {
28+
}
29+
30+
/**
31+
* (non-Javadoc).
32+
*/
33+
public void two() {
34+
}
35+
36+
}

0 commit comments

Comments
 (0)