Skip to content

Commit c5ba78f

Browse files
Sushant Kumar Singhphilwebb
Sushant Kumar Singh
authored andcommitted
Add checkstyle rule for method javadoc empty lines
Update `SpringJavadocCheck` to ensure that method javadoc does not have any blank lines before their tags. See gh-199
1 parent f5ccce6 commit c5ba78f

File tree

4 files changed

+51
-0
lines changed

4 files changed

+51
-0
lines changed

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

+18
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ public class SpringJavadocCheck extends AbstractSpringCheck {
5353

5454
private static final Pattern SINCE_TAG_PATTERN = Pattern.compile("@since\\s+(.*)");
5555

56+
private static final Pattern PARAM_TAG_PATTERN = Pattern.compile("@param\\s+(.*)");
57+
5658
private static final Set<Integer> TOP_LEVEL_TYPES;
5759
static {
5860
Set<Integer> topLevelTypes = new HashSet<Integer>();
@@ -93,6 +95,22 @@ private void checkJavadoc(DetailAST ast, TextBlock javadoc) {
9395
checkBannedTags(ast, javadoc);
9496
checkTagCase(ast, javadoc);
9597
checkSinceTag(ast, javadoc);
98+
checkMethodJavaDoc(ast, javadoc);
99+
}
100+
101+
private void checkMethodJavaDoc(DetailAST ast, TextBlock javadoc) {
102+
if (TokenTypes.METHOD_DEF != ast.getType()) {
103+
return;
104+
}
105+
else {
106+
String[] text = javadoc.getText();
107+
for (int i = 0; i < text.length; i++) {
108+
Matcher matcher = SINCE_TAG_PATTERN.matcher(text[i]);
109+
if (matcher.find() && text[i - 1].trim().equals("*")) {
110+
log(javadoc.getStartLineNo() + i - 1, 0, "javadoc.whiteSpace");
111+
}
112+
}
113+
}
96114
}
97115

98116
private void checkBannedTags(DetailAST ast, TextBlock javadoc) {

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

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ javadoc.badCase=Javadoc element descriptions should not start with an uppercase
99
javadoc.bannedTag=Javadoc tag ''{0}'' should not be used.
1010
javadoc.missingSince=Missing Javadoc @since tag.
1111
javadoc.publicSince=Javadoc @since tag should not be used on private classes.
12+
javadoc.whiteSpace=Line matches the illegal pattern 'Trailing whitespace'.
1213
junit5.bannedImport=Import ''{0}'' should not be used in a JUnit 5 test.
1314
junit5.bannedTestAnnotation=JUnit 4 @Test annotation should not be used in a JUnit 5 test.
1415
junit5.lifecyclePrivateMethod=Lifecycle method ''{0}'' should not be private.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
+JavaDocWhiteSpace.java:25: Line matches the illegal pattern 'Trailing whitespace'.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
* Javadoc with white space.
19+
* @param <T> this is a valid param
20+
* @author Sushant Kumar Singh
21+
*/
22+
public class JavaDocWhiteSpace<T> {
23+
/**
24+
* Do something.
25+
*
26+
* @param something a lovely thing
27+
*/
28+
public void test(String something) {
29+
}
30+
31+
}

0 commit comments

Comments
 (0)