public class JavadocStyleCheck extends AbstractCheck
Validates Javadoc comments to help ensure they are well formed.
The following checks are performed:
{@inheritDoc} tag are exempt from this
requirement.
@param and @return.
These checks were patterned after the checks made by the DocCheck doclet available from Sun. Note: Original Sun's DocCheck tool does not exist anymore.
scope - Specify the visibility scope where Javadoc comments are checked.
Type is com.puppycrawl.tools.checkstyle.api.Scope.
Default value is private.
excludeScope - Specify the visibility scope where
Javadoc comments are not checked.
Type is com.puppycrawl.tools.checkstyle.api.Scope.
Default value is null.
checkFirstSentence - Control whether to check the first
sentence for proper end of sentence.
Type is boolean.
Default value is true.
endOfSentenceFormat - Specify the format for matching
the end of a sentence.
Type is java.util.regex.Pattern.
Default value is "([.?!][ \t\n\r\f<])|([.?!]$)".
checkEmptyJavadoc - Control whether to check if the Javadoc
is missing a describing text.
Type is boolean.
Default value is false.
checkHtml - Control whether to check for incomplete HTML tags.
Type is boolean.
Default value is true.
tokens - tokens to check
Type is java.lang.String[].
Validation type is tokenSet.
Default value is:
ANNOTATION_DEF,
ANNOTATION_FIELD_DEF,
CLASS_DEF,
CTOR_DEF,
ENUM_CONSTANT_DEF,
ENUM_DEF,
INTERFACE_DEF,
METHOD_DEF,
PACKAGE_DEF,
VARIABLE_DEF,
RECORD_DEF,
COMPACT_CTOR_DEF.
To configure the default check:
<module name="JavadocStyle"/>
Example:
public class Test {
/**
* Some description here. // OK
*/
private void methodWithValidCommentStyle() {}
/**
* Some description here // violation, the sentence must end with a proper punctuation
*/
private void methodWithInvalidCommentStyle() {}
}
To configure the check for public scope:
<module name="JavadocStyle"> <property name="scope" value="public"/> </module>
Example:
public class Test {
/**
* Some description here // violation, the sentence must end with a proper punctuation
*/
public void test1() {}
/**
* Some description here // OK
*/
private void test2() {}
}
To configure the check for javadoc which is in private, but not in package scope:
<module name="JavadocStyle"> <property name="scope" value="private"/> <property name="excludeScope" value="package"/> </module>
Example:
public class Test {
/**
* Some description here // violation, the sentence must end with a proper punctuation
*/
private void test1() {}
/**
* Some description here // OK
*/
void test2() {}
}
To configure the check to turn off first sentence checking:
<module name="JavadocStyle"> <property name="checkFirstSentence" value="false"/> </module>
Example:
public class Test {
/**
* Some description here // OK
* Second line of description // violation, the sentence must end with a proper punctuation
*/
private void test1() {}
}
To configure the check to turn off validation of incomplete html tags:
<module name="JavadocStyle"> <property name="checkHtml" value="false"/> </module>
Example:
public class Test {
/**
* Some description here // violation, the sentence must end with a proper punctuation
* <p // OK
*/
private void test1() {}
}
To configure the check for only class definitions:
<module name="JavadocStyle"> <property name="tokens" value="CLASS_DEF"/> </module>
Example:
/**
* Some description here // violation, the sentence must end with a proper punctuation
*/
public class Test {
/**
* Some description here // OK
*/
private void test1() {}
}
Parent is com.puppycrawl.tools.checkstyle.TreeWalker
Violation Message Keys:
javadoc.empty
javadoc.extraHtml
javadoc.incompleteTag
javadoc.missing
javadoc.noPeriod
javadoc.unclosedHtml
AutomaticBean.OutputStreamOptions| Modifier and Type | Field and Description |
|---|---|
private static java.util.Set<java.lang.String> |
ALLOWED_TAGS
HTML tags that are allowed in java docs.
|
private boolean |
checkEmptyJavadoc
Control whether to check if the Javadoc is missing a describing text.
|
private boolean |
checkFirstSentence
Control whether to check the first sentence for proper end of sentence.
|
private boolean |
checkHtml
Control whether to check for incomplete HTML tags.
|
private java.util.regex.Pattern |
endOfSentenceFormat
Specify the format for matching the end of a sentence.
|
private Scope |
excludeScope
Specify the visibility scope where Javadoc comments are not checked.
|
static java.lang.String |
MSG_EMPTY
Message property key for the Empty Javadoc message.
|
static java.lang.String |
MSG_EXTRA_HTML
Message property key for the Extra HTML message.
|
static java.lang.String |
MSG_INCOMPLETE_TAG
Message property key for the Incomplete Tag message.
|
static java.lang.String |
MSG_JAVADOC_MISSING
Message property key for the Missing Javadoc message.
|
static java.lang.String |
MSG_NO_PERIOD
Message property key for the No Javadoc end of Sentence Period message.
|
static java.lang.String |
MSG_UNCLOSED_HTML
Message property key for the Unclosed HTML message.
|
private Scope |
scope
Specify the visibility scope where Javadoc comments are checked.
|
private static java.util.Set<java.lang.String> |
SINGLE_TAGS
HTML tags that do not require a close tag.
|
| Constructor and Description |
|---|
JavadocStyleCheck() |
| Modifier and Type | Method and Description |
|---|---|
private void |
checkComment(DetailAST ast,
TextBlock comment)
Performs the various checks against the Javadoc comment.
|
private void |
checkFirstSentenceEnding(DetailAST ast,
TextBlock comment)
Checks that the first sentence ends with proper punctuation.
|
private void |
checkHtmlTags(DetailAST ast,
TextBlock comment)
Checks the comment for HTML tags that do not have a corresponding close
tag or a close tag that has no previous open tag.
|
private void |
checkJavadocIsNotEmpty(TextBlock comment)
Checks that the Javadoc is not empty.
|
private void |
checkUnclosedTags(java.util.Deque<HtmlTag> htmlStack,
java.lang.String token)
Checks to see if there are any unclosed tags on the stack.
|
private static int |
findTextStart(java.lang.String line)
Finds the index of the first non-whitespace character ignoring the
Javadoc comment start and end strings (/** and */) as well as any
leading asterisk.
|
int[] |
getAcceptableTokens()
The configurable token set.
|
private static java.lang.String |
getCommentText(java.lang.String... comments)
Returns the comment text from the Javadoc.
|
int[] |
getDefaultTokens()
Returns the default token a check is interested in.
|
int[] |
getRequiredTokens()
The tokens that this check must be registered for.
|
private static boolean |
isAllowedTag(HtmlTag tag)
Determines if the HtmlTag is one which is allowed in a javadoc.
|
private static boolean |
isExtraHtml(java.lang.String token,
java.util.Deque<HtmlTag> htmlStack)
Determines if the given token is an extra HTML tag.
|
private static boolean |
isSingleTag(HtmlTag tag)
Determines if the HtmlTag is one which does not require a close tag.
|
void |
setCheckEmptyJavadoc(boolean flag)
Setter to control whether to check if the Javadoc is missing a describing text.
|
void |
setCheckFirstSentence(boolean flag)
Setter to control whether to check the first sentence for proper end of sentence.
|
void |
setCheckHtml(boolean flag)
Setter to control whether to check for incomplete HTML tags.
|
void |
setEndOfSentenceFormat(java.util.regex.Pattern pattern)
Setter to specify the format for matching the end of a sentence.
|
void |
setExcludeScope(Scope excludeScope)
Setter to specify the visibility scope where Javadoc comments are not checked.
|
void |
setScope(Scope scope)
Setter to specify the visibility scope where Javadoc comments are checked.
|
private boolean |
shouldCheck(DetailAST ast)
Whether we should check this node.
|
private static void |
trimTail(java.lang.StringBuilder builder)
Trims any trailing whitespace or the end of Javadoc comment string.
|
void |
visitToken(DetailAST ast)
Called to process a token.
|
beginTree, clearViolations, destroy, finishTree, getFileContents, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, init, isCommentNodesRequired, leaveToken, log, log, log, setFileContents, setTabWidth, setTokensfinishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverityconfigure, contextualize, getConfiguration, setupChildpublic static final java.lang.String MSG_JAVADOC_MISSING
public static final java.lang.String MSG_EMPTY
public static final java.lang.String MSG_NO_PERIOD
public static final java.lang.String MSG_INCOMPLETE_TAG
public static final java.lang.String MSG_UNCLOSED_HTML
public static final java.lang.String MSG_EXTRA_HTML
private static final java.util.Set<java.lang.String> SINGLE_TAGS
private static final java.util.Set<java.lang.String> ALLOWED_TAGS
private Scope excludeScope
private java.util.regex.Pattern endOfSentenceFormat
private boolean checkFirstSentence
private boolean checkHtml
private boolean checkEmptyJavadoc
public JavadocStyleCheck()
public int[] getDefaultTokens()
AbstractCheckgetDefaultTokens in class AbstractCheckTokenTypespublic int[] getAcceptableTokens()
AbstractCheckgetAcceptableTokens in class AbstractCheckTokenTypespublic int[] getRequiredTokens()
AbstractCheckgetRequiredTokens in class AbstractCheckTokenTypespublic void visitToken(DetailAST ast)
AbstractCheckvisitToken in class AbstractCheckast - the token to processprivate boolean shouldCheck(DetailAST ast)
ast - a given node.private void checkComment(DetailAST ast, TextBlock comment)
ast - the AST of the element being documentedcomment - the source lines that make up the Javadoc comment.checkFirstSentenceEnding(DetailAST, TextBlock),
checkHtmlTags(DetailAST, TextBlock)private void checkFirstSentenceEnding(DetailAST ast, TextBlock comment)
ast - the current nodecomment - the source lines that make up the Javadoc comment.private void checkJavadocIsNotEmpty(TextBlock comment)
comment - the source lines that make up the Javadoc comment.private static java.lang.String getCommentText(java.lang.String... comments)
comments - the lines of Javadoc.private static int findTextStart(java.lang.String line)
line - the Javadoc comment line of text to scan.private static void trimTail(java.lang.StringBuilder builder)
builder - the StringBuilder to trim.private void checkHtmlTags(DetailAST ast, TextBlock comment)
ast - the node with the Javadoccomment - the TextBlock which represents
the Javadoc comment.private void checkUnclosedTags(java.util.Deque<HtmlTag> htmlStack, java.lang.String token)
htmlStack - the stack of opened HTML tags.token - the current HTML tag name that has been closed.private static boolean isSingleTag(HtmlTag tag)
tag - the HtmlTag to check.true if the HtmlTag is a single tag.private static boolean isAllowedTag(HtmlTag tag)
tag - the HtmlTag to check.true if the HtmlTag is an allowed html tag.private static boolean isExtraHtml(java.lang.String token, java.util.Deque<HtmlTag> htmlStack)
token - an HTML tag id for which a close was found.htmlStack - a Stack of previous open HTML tags.false if a previous open tag was found
for the token.public void setScope(Scope scope)
scope - a scope.public void setExcludeScope(Scope excludeScope)
excludeScope - a scope.public void setEndOfSentenceFormat(java.util.regex.Pattern pattern)
pattern - a pattern.public void setCheckFirstSentence(boolean flag)
flag - true if the first sentence is to be checkedpublic void setCheckHtml(boolean flag)
flag - true if HTML checking is to be performed.public void setCheckEmptyJavadoc(boolean flag)
flag - true if empty Javadoc checking should be done.Copyright © 2001-2022. All Rights Reserved.