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 3, 2020
1 parent
b653ead
commit a59ead9
Showing
20 changed files
with
375 additions
and
0 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
131 changes: 131 additions & 0 deletions
131
...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,131 @@ | ||
//////////////////////////////////////////////////////////////////////////////// | ||
// 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, | ||
}; | ||
} | ||
|
||
@Override | ||
public int[] getRequiredJavadocTokens() { | ||
return getAcceptableJavadocTokens(); | ||
} | ||
|
||
@Override | ||
public void visitJavadocToken(DetailNode javadoc) { | ||
if (isSingleLineJavadoc(getBlockCommentAst())) { | ||
checkWhitespaceAfterAsterisk(JavadocUtil.getFirstChild(javadoc)); | ||
} | ||
else { | ||
for (DetailNode node : javadoc.getChildren()) { | ||
if (node.getType() == JavadocTokenTypes.LEADING_ASTERISK) { | ||
final DetailNode nextSibling = JavadocUtil.getNextSibling(node); | ||
if (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
65 changes: 65 additions & 0 deletions
65
...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,65 @@ | ||
//////////////////////////////////////////////////////////////////////////////// | ||
// 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; | ||
|
||
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, | ||
}; | ||
assertArrayEquals(expected, checkObj.getAcceptableJavadocTokens(), | ||
"Default acceptable tokens are invalid"); | ||
} | ||
|
||
@Test | ||
public void testDefault() throws Exception { | ||
final DefaultConfiguration checkConfig = | ||
createModuleConfig(JavadocMissingWhitespaceAfterAsteriskCheck.class); | ||
final String[] expected = { | ||
"16:7: " + getCheckMessage(MSG_KEY), | ||
"22:7: " + getCheckMessage(MSG_KEY), | ||
"38:8: " + getCheckMessage(MSG_KEY), | ||
"41:8: " + getCheckMessage(MSG_KEY), | ||
}; | ||
verify(checkConfig, getPath("InputJavadocMissingWhitespaceAfterAsterisk.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
46 changes: 46 additions & 0 deletions
46
...doc/javadocmissingwhitespaceafterasterisk/InputJavadocMissingWhitespaceAfterAsterisk.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,46 @@ | ||
package com.puppycrawl.tools.checkstyle.checks.javadoc.javadocmissingwhitespaceafterasterisk; | ||
class InputJavadocMissingWhitespaceAfterAsterisk | ||
{ | ||
/** | ||
* @see Something | ||
* This is ok. | ||
**/ | ||
void foo() {} | ||
|
||
/** | ||
* This is ok. | ||
*/ | ||
void foo1() {} | ||
|
||
/** | ||
*This is not ok. | ||
*/ | ||
void foo2() {} | ||
|
||
/** | ||
* This is not ok. | ||
*@see Something | ||
*/ | ||
void foo3() {} | ||
|
||
/** | ||
* | ||
* This is not ok. | ||
*/ | ||
void foo4() {} | ||
|
||
/** This is ok. */ | ||
void bar() {} | ||
|
||
/** @see Something */ | ||
void bar1() {} | ||
|
||
/**@see Something*/ | ||
void bar2() {} | ||
|
||
/**This is not ok. */ | ||
void bar4() {} | ||
|
||
/** This is ok. **/ | ||
void bar5() {} | ||
} |
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.