Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #7183: add JavadocMissingWhitespaceAfterAsteriskCheck
- Loading branch information
0blivious
committed
Apr 2, 2020
1 parent
834b505
commit c32a0f6
Showing
22 changed files
with
511 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
...uppycrawl/tools/checkstyle/checks/javadoc/JavadocMissingWhitespaceAfterAsteriskCheck.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
//////////////////////////////////////////////////////////////////////////////// | ||
// checkstyle: Checks Java source code for adherence to a set of rules. | ||
// Copyright (C) 2001-2020 the original author or authors. | ||
// | ||
// This library is free software; you can redistribute it and/or | ||
// modify it under the terms of the GNU Lesser General Public | ||
// License as published by the Free Software Foundation; either | ||
// version 2.1 of the License, or (at your option) any later version. | ||
// | ||
// This library is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
// Lesser General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Lesser General Public | ||
// License along with this library; if not, write to the Free Software | ||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
//////////////////////////////////////////////////////////////////////////////// | ||
|
||
package com.puppycrawl.tools.checkstyle.checks.javadoc; | ||
|
||
import com.puppycrawl.tools.checkstyle.StatelessCheck; | ||
import com.puppycrawl.tools.checkstyle.api.DetailNode; | ||
import com.puppycrawl.tools.checkstyle.api.JavadocTokenTypes; | ||
import com.puppycrawl.tools.checkstyle.utils.JavadocUtil; | ||
|
||
/** | ||
* <p> | ||
* Checks that there is at least one whitespace after the leading asterisk. | ||
* Although spaces after asterisks are optional in the Javadoc comments, their absence | ||
* makes the documentation difficult to read. It is the de facto standard to put at least | ||
* one whitespace after the leading asterisk. | ||
* </p> | ||
* <ul> | ||
* <li> | ||
* Property {@code violateExecutionOnNonTightHtml} - Control when to print violations | ||
* if the Javadoc being examined by this check violates the tight html rules defined at | ||
* <a href="https://checkstyle.org/writingjavadocchecks.html#Tight-HTML_rules">Tight-HTML Rules</a>. | ||
* Default value is {@code false}. | ||
* </li> | ||
* </ul> | ||
* <p> | ||
* To configure the default check: | ||
* </p> | ||
* <pre> | ||
* <module name="JavadocMissingWhitespaceAfterAsterisk"/> | ||
* </pre> | ||
* <p> | ||
* Code Example: | ||
* </p> | ||
* <pre> | ||
* /** This is valid single-line Javadoc. */ | ||
* class TestClass { | ||
* /** | ||
* *This is invalid Javadoc. | ||
* */ | ||
* int invalidJavaDoc; | ||
* /** | ||
* * This is valid Javadoc. | ||
* */ | ||
* void validJavaDocMethod() { | ||
* } | ||
* /**This is invalid single-line Javadoc. */ | ||
* void invalidSingleLineJavaDocMethod() { | ||
* } | ||
* /** This is valid single-line Javadoc. */ | ||
* void validSingleLineJavaDocMethod() { | ||
* } | ||
* } | ||
* </pre> | ||
* | ||
* @since 8.32 | ||
*/ | ||
@StatelessCheck | ||
public class JavadocMissingWhitespaceAfterAsteriskCheck extends AbstractJavadocCheck { | ||
|
||
/** | ||
* A key is pointing to the warning message text in "messages.properties" file. | ||
*/ | ||
public static final String MSG_KEY = "javadoc.missing.whitespace"; | ||
|
||
@Override | ||
public int[] getDefaultJavadocTokens() { | ||
return new int[] { | ||
JavadocTokenTypes.JAVADOC, | ||
JavadocTokenTypes.LEADING_ASTERISK, | ||
}; | ||
} | ||
|
||
@Override | ||
public int[] getRequiredJavadocTokens() { | ||
return getAcceptableJavadocTokens(); | ||
} | ||
|
||
@Override | ||
public void visitJavadocToken(DetailNode detailNode) { | ||
final DetailNode textNode; | ||
|
||
if (detailNode.getType() == JavadocTokenTypes.JAVADOC) { | ||
textNode = JavadocUtil.getFirstChild(detailNode); | ||
} | ||
else { | ||
textNode = JavadocUtil.getNextSibling(detailNode); | ||
} | ||
|
||
if (textNode != null | ||
&& textNode.getType() != JavadocTokenTypes.EOF | ||
&& !hasWhitespaceAfterAsteriskBeforeText(textNode)) { | ||
log(textNode.getLineNumber(), textNode.getColumnNumber(), MSG_KEY); | ||
} | ||
} | ||
|
||
/** | ||
* Checks if there is at least one whitespace after leading asterisks and before text. | ||
* If {@code tagText} contains no characters other than "*", it is considered the | ||
* same as there is whitespace. | ||
* | ||
* @param node the node after the leading asterisks. | ||
* @return true if there is at least one white space after asterisk and before text. | ||
* | ||
*/ | ||
private static boolean hasWhitespaceAfterAsteriskBeforeText(DetailNode node) { | ||
boolean hasWhitespaceAfterAsteriskBeforeText = false; | ||
final String tagText = node.getText(); | ||
|
||
for (int i = 0; i < tagText.length(); i++) { | ||
if (tagText.charAt(i) != '*') { | ||
if (Character.isWhitespace(tagText.charAt(i))) { | ||
hasWhitespaceAfterAsteriskBeforeText = true; | ||
} | ||
break; | ||
} | ||
if (i == tagText.length() - 1) { | ||
hasWhitespaceAfterAsteriskBeforeText = true; | ||
} | ||
} | ||
|
||
return hasWhitespaceAfterAsteriskBeforeText; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
...crawl/tools/checkstyle/checks/javadoc/JavadocMissingWhitespaceAfterAsteriskCheckTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
//////////////////////////////////////////////////////////////////////////////// | ||
// checkstyle: Checks Java source code for adherence to a set of rules. | ||
// Copyright (C) 2001-2020 the original author or authors. | ||
// | ||
// This library is free software; you can redistribute it and/or | ||
// modify it under the terms of the GNU Lesser General Public | ||
// License as published by the Free Software Foundation; either | ||
// version 2.1 of the License, or (at your option) any later version. | ||
// | ||
// This library is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
// Lesser General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Lesser General Public | ||
// License along with this library; if not, write to the Free Software | ||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
//////////////////////////////////////////////////////////////////////////////// | ||
|
||
package com.puppycrawl.tools.checkstyle.checks.javadoc; | ||
|
||
import static com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocMissingWhitespaceAfterAsteriskCheck.MSG_KEY; | ||
import static org.junit.jupiter.api.Assertions.assertArrayEquals; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport; | ||
import com.puppycrawl.tools.checkstyle.DefaultConfiguration; | ||
import com.puppycrawl.tools.checkstyle.api.JavadocTokenTypes; | ||
import com.puppycrawl.tools.checkstyle.utils.CommonUtil; | ||
|
||
public class JavadocMissingWhitespaceAfterAsteriskCheckTest | ||
extends AbstractModuleTestSupport { | ||
|
||
@Override | ||
protected String getPackageLocation() { | ||
return "com/puppycrawl/tools/checkstyle/checks/javadoc" | ||
+ "/javadocmissingwhitespaceafterasterisk"; | ||
} | ||
|
||
@Test | ||
public void testGetAcceptableTokens() { | ||
final JavadocMissingWhitespaceAfterAsteriskCheck checkObj = | ||
new JavadocMissingWhitespaceAfterAsteriskCheck(); | ||
final int[] expected = { | ||
JavadocTokenTypes.JAVADOC, | ||
JavadocTokenTypes.LEADING_ASTERISK, | ||
}; | ||
assertArrayEquals(expected, checkObj.getAcceptableJavadocTokens(), | ||
"Default tokens are invalid"); | ||
} | ||
|
||
@Test | ||
public void testGetRequiredJavadocTokens() { | ||
final JavadocMissingWhitespaceAfterAsteriskCheck checkObj = | ||
new JavadocMissingWhitespaceAfterAsteriskCheck(); | ||
final int[] expected = { | ||
JavadocTokenTypes.JAVADOC, | ||
JavadocTokenTypes.LEADING_ASTERISK, | ||
}; | ||
assertArrayEquals(expected, checkObj.getRequiredJavadocTokens(), | ||
"Default required tokens are invalid"); | ||
} | ||
|
||
@Test | ||
public void testValid() throws Exception { | ||
final DefaultConfiguration checkConfig = | ||
createModuleConfig(JavadocMissingWhitespaceAfterAsteriskCheck.class); | ||
final String[] expected = CommonUtil.EMPTY_STRING_ARRAY; | ||
|
||
verify(checkConfig, | ||
getPath("InputJavadocMissingWhitespaceAfterAsteriskValid.java"), expected); | ||
} | ||
|
||
@Test | ||
public void testInvalid() throws Exception { | ||
final DefaultConfiguration checkConfig = | ||
createModuleConfig(JavadocMissingWhitespaceAfterAsteriskCheck.class); | ||
final String[] expected = { | ||
"5:4: " + getCheckMessage(MSG_KEY), | ||
"11:7: " + getCheckMessage(MSG_KEY), | ||
"16:11: " + getCheckMessage(MSG_KEY), | ||
"23:11: " + getCheckMessage(MSG_KEY), | ||
"27:13: " + getCheckMessage(MSG_KEY), | ||
"33:7: " + getCheckMessage(MSG_KEY), | ||
"38:7: " + getCheckMessage(MSG_KEY), | ||
"43:7: " + getCheckMessage(MSG_KEY), | ||
"48:7: " + getCheckMessage(MSG_KEY), | ||
"50:7: " + getCheckMessage(MSG_KEY), | ||
"54:8: " + getCheckMessage(MSG_KEY), | ||
"57:8: " + getCheckMessage(MSG_KEY), | ||
"60:10: " + getCheckMessage(MSG_KEY), | ||
}; | ||
verify(checkConfig, | ||
getPath("InputJavadocMissingWhitespaceAfterAsteriskInvalid.java"), expected); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.