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
Mar 6, 2020
1 parent
c7ae67e
commit 0543546
Showing
27 changed files
with
426 additions
and
14 deletions.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -205,7 +205,7 @@ | |
* | ||
* public int foo4() {return 4;} // violation | ||
* } | ||
*</pre> | ||
* </pre> | ||
* | ||
* @since 3.1 | ||
*/ | ||
|
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
134 changes: 134 additions & 0 deletions
134
...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,134 @@ | ||
//////////////////////////////////////////////////////////////////////////////// | ||
// 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.DetailAST; | ||
import com.puppycrawl.tools.checkstyle.api.DetailNode; | ||
import com.puppycrawl.tools.checkstyle.api.JavadocTokenTypes; | ||
import com.puppycrawl.tools.checkstyle.utils.JavadocUtil; | ||
import com.puppycrawl.tools.checkstyle.utils.TokenUtil; | ||
|
||
/** | ||
* <p> | ||
* Checks that at there is 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> | ||
* class TestClass { | ||
* /** | ||
* *This is invalid java doc. | ||
* */ | ||
* void invalidJavaDocMethod() { | ||
* } | ||
* /** | ||
* * This is valid java doc. | ||
* */ | ||
* void validJavaDocMethod() { | ||
* } | ||
* /**This is invalid single linejava doc. */ | ||
* void InvalidSingleLineJavaDocMethod() { | ||
* } | ||
* } | ||
* </pre> | ||
* | ||
* @since 8.31 | ||
*/ | ||
@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[] getAcceptableJavadocTokens() { | ||
return getDefaultJavadocTokens(); | ||
} | ||
|
||
@Override | ||
public int[] getRequiredJavadocTokens() { | ||
return getDefaultJavadocTokens(); | ||
} | ||
|
||
@Override | ||
public void visitJavadocToken(DetailNode detailNode) { | ||
if (detailNode.getType() == JavadocTokenTypes.JAVADOC | ||
&& isSingleLineJavadoc(getBlockCommentAst())) { | ||
checkWhitespaceAfterAsterisk(JavadocUtil.getFirstChild(detailNode)); | ||
} | ||
else { | ||
final DetailNode nextSibling = JavadocUtil.getNextSibling(detailNode); | ||
if (nextSibling != null && nextSibling.getType() != JavadocTokenTypes.EOF) { | ||
checkWhitespaceAfterAsterisk(nextSibling); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Checks if there is at least one whitespace after leading asterisk. | ||
* | ||
* @param node the node after the leading asterisk. | ||
*/ | ||
private void checkWhitespaceAfterAsterisk(DetailNode node) { | ||
final String tagText = node.getText(); | ||
|
||
if (!Character.isWhitespace(tagText.charAt(0))) { | ||
log(node.getLineNumber(), node.getColumnNumber(), MSG_KEY); | ||
} | ||
} | ||
|
||
/** | ||
* Checks if comment is single line comment. | ||
* | ||
* @param blockCommentStart the AST tree in which a block comment starts | ||
* @return true, if comment is single line comment. | ||
*/ | ||
private static boolean isSingleLineJavadoc(DetailAST blockCommentStart) { | ||
final DetailAST blockCommentEnd = blockCommentStart.getLastChild(); | ||
return TokenUtil.areOnSameLine(blockCommentStart, blockCommentEnd); | ||
} | ||
|
||
} |
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
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.