Skip to content

Commit 4b8b855

Browse files
committed
Merge pull request #199 from maverick2012
* pr/199: Polish 'Add checkstyle rule for method javadoc empty lines' Add checkstyle rule for method javadoc empty lines Closes gh-17005
2 parents f5ccce6 + 1dac26e commit 4b8b855

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

+16
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 AT_TAG_PATTERN = Pattern.compile("@\\w+\\s+.*");
57+
5658
private static final Set<Integer> TOP_LEVEL_TYPES;
5759
static {
5860
Set<Integer> topLevelTypes = new HashSet<Integer>();
@@ -93,6 +95,7 @@ private void checkJavadoc(DetailAST ast, TextBlock javadoc) {
9395
checkBannedTags(ast, javadoc);
9496
checkTagCase(ast, javadoc);
9597
checkSinceTag(ast, javadoc);
98+
checkMethodJavaDoc(ast, javadoc);
9699
}
97100

98101
private void checkBannedTags(DetailAST ast, TextBlock javadoc) {
@@ -147,6 +150,19 @@ private void checkSinceTag(DetailAST ast, TextBlock javadoc) {
147150
}
148151
}
149152

153+
private void checkMethodJavaDoc(DetailAST ast, TextBlock javadoc) {
154+
if (TokenTypes.METHOD_DEF != ast.getType()) {
155+
return;
156+
}
157+
String[] text = javadoc.getText();
158+
for (int i = 0; i < text.length; i++) {
159+
Matcher matcher = AT_TAG_PATTERN.matcher(text[i]);
160+
if (matcher.find() && i > 0 && text[i - 1].trim().equals("*")) {
161+
log(javadoc.getStartLineNo() + i - 1, 0, "javadoc.emptyLineBeforeTag");
162+
}
163+
}
164+
}
165+
150166
private boolean startsWithUppercase(String description) {
151167
return description.length() > 0 && Character.isUpperCase(description.charAt(0));
152168
}

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.emptyLineBeforeTag=Method Javadoc should not have empty line before tag.
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,2 @@
1+
+Method Javadoc should not have empty line before tag.
2+
+1 error
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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 JavadocMethodEmptyLineBeforeTag<T> {
23+
24+
/**
25+
* Do something.
26+
*
27+
* @param something a lovely thing
28+
*/
29+
public void test(String something) {
30+
}
31+
32+
}

0 commit comments

Comments
 (0)