Class JavadocStyleCheck
- java.lang.Object
-
- com.puppycrawl.tools.checkstyle.api.AutomaticBean
-
- com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
-
- com.puppycrawl.tools.checkstyle.api.AbstractCheck
-
- com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocStyleCheck
-
- All Implemented Interfaces:
Configurable,Contextualizable
public class JavadocStyleCheck extends AbstractCheck
Custom Checkstyle Check to validate Javadoc.
-
-
Field Summary
Fields Modifier and Type Field Description private static java.util.Set<java.lang.String>ALLOWED_TAGSHTML tags that are allowed in java docs.private booleancheckEmptyJavadocIndicates if empty javadoc statements should be checked.private booleancheckFirstSentenceIndicates if the first sentence should be checked for proper end of sentence punctuation.private booleancheckHtmlIndicates if the HTML within the comment should be checked.private java.util.regex.PatternendOfSentenceFormatFormat for matching the end of a sentence.private ScopeexcludeScopeThe visibility scope where Javadoc comments shouldn't be checked.static java.lang.StringMSG_EMPTYMessage property key for the Unclosed HTML message.static java.lang.StringMSG_EXTRA_HTMLMessage property key for the Extra HTML message.static java.lang.StringMSG_INCOMPLETE_TAGMessage property key for the Unclosed HTML message.static java.lang.StringMSG_JAVADOC_MISSINGMessage property key for the Unclosed HTML message.static java.lang.StringMSG_NO_PERIODMessage property key for the Unclosed HTML message.static java.lang.StringMSG_UNCLOSED_HTMLMessage property key for the Unclosed HTML message.private ScopescopeThe scope to check.private static java.util.Set<java.lang.String>SINGLE_TAGSHTML tags that do not require a close tag.
-
Constructor Summary
Constructors Constructor Description JavadocStyleCheck()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private voidcheckComment(DetailAST ast, TextBlock comment)Performs the various checks against the Javadoc comment.private voidcheckFirstSentenceEnding(DetailAST ast, TextBlock comment)Checks that the first sentence ends with proper punctuation.private voidcheckHtmlTags(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 voidcheckJavadocIsNotEmpty(TextBlock comment)Checks that the Javadoc is not empty.private voidcheckUnclosedTags(java.util.Deque<HtmlTag> htmlStack, java.lang.String token)Checks to see if there are any unclosed tags on the stack.private static intfindTextStart(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.StringgetCommentText(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 booleanisAllowedTag(HtmlTag tag)Determines if the HtmlTag is one which is allowed in a javadoc.private static booleanisExtraHtml(java.lang.String token, java.util.Deque<HtmlTag> htmlStack)Determines if the given token is an extra HTML tag.private static booleanisSingleTag(HtmlTag tag)Determines if the HtmlTag is one which does not require a close tag.voidsetCheckEmptyJavadoc(boolean flag)Sets the flag that determines if empty Javadoc checking should be done.voidsetCheckFirstSentence(boolean flag)Sets the flag that determines if the first sentence is checked for proper end of sentence punctuation.voidsetCheckHtml(boolean flag)Sets the flag that determines if HTML checking is to be performed.voidsetEndOfSentenceFormat(java.util.regex.Pattern pattern)Set the format for matching the end of a sentence.voidsetExcludeScope(Scope excludeScope)Set the excludeScope.voidsetScope(Scope scope)Sets the scope to check.private booleanshouldCheck(DetailAST ast)Whether we should check this node.private static voidtrimTail(java.lang.StringBuilder builder)Trims any trailing whitespace or the end of Javadoc comment string.voidvisitToken(DetailAST ast)Called to process a token.-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck
beginTree, destroy, finishTree, getClassLoader, getFileContents, getLine, getLines, getTabWidth, getTokenNames, init, isCommentNodesRequired, leaveToken, log, log, setClassLoader, setFileContents, setMessages, setTabWidth, setTokens
-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, log, setId, setSeverity
-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean
configure, contextualize, finishLocalSetup, getConfiguration, setupChild
-
-
-
-
Field Detail
-
MSG_JAVADOC_MISSING
public static final java.lang.String MSG_JAVADOC_MISSING
Message property key for the Unclosed HTML message.- See Also:
- Constant Field Values
-
MSG_EMPTY
public static final java.lang.String MSG_EMPTY
Message property key for the Unclosed HTML message.- See Also:
- Constant Field Values
-
MSG_NO_PERIOD
public static final java.lang.String MSG_NO_PERIOD
Message property key for the Unclosed HTML message.- See Also:
- Constant Field Values
-
MSG_INCOMPLETE_TAG
public static final java.lang.String MSG_INCOMPLETE_TAG
Message property key for the Unclosed HTML message.- See Also:
- Constant Field Values
-
MSG_UNCLOSED_HTML
public static final java.lang.String MSG_UNCLOSED_HTML
Message property key for the Unclosed HTML message.- See Also:
- Constant Field Values
-
MSG_EXTRA_HTML
public static final java.lang.String MSG_EXTRA_HTML
Message property key for the Extra HTML message.- See Also:
- Constant Field Values
-
SINGLE_TAGS
private static final java.util.Set<java.lang.String> SINGLE_TAGS
HTML tags that do not require a close tag.
-
ALLOWED_TAGS
private static final java.util.Set<java.lang.String> ALLOWED_TAGS
HTML tags that are allowed in java docs. From https://www.w3schools.com/tags/default.asp The forms and structure tags are not allowed
-
scope
private Scope scope
The scope to check.
-
excludeScope
private Scope excludeScope
The visibility scope where Javadoc comments shouldn't be checked.
-
endOfSentenceFormat
private java.util.regex.Pattern endOfSentenceFormat
Format for matching the end of a sentence.
-
checkFirstSentence
private boolean checkFirstSentence
Indicates if the first sentence should be checked for proper end of sentence punctuation.
-
checkHtml
private boolean checkHtml
Indicates if the HTML within the comment should be checked.
-
checkEmptyJavadoc
private boolean checkEmptyJavadoc
Indicates if empty javadoc statements should be checked.
-
-
Method Detail
-
getDefaultTokens
public int[] getDefaultTokens()
Description copied from class:AbstractCheckReturns the default token a check is interested in. Only used if the configuration for a check does not define the tokens.- Specified by:
getDefaultTokensin classAbstractCheck- Returns:
- the default tokens
- See Also:
TokenTypes
-
getAcceptableTokens
public int[] getAcceptableTokens()
Description copied from class:AbstractCheckThe configurable token set. Used to protect Checks against malicious users who specify an unacceptable token set in the configuration file. The default implementation returns the check's default tokens.- Specified by:
getAcceptableTokensin classAbstractCheck- Returns:
- the token set this check is designed for.
- See Also:
TokenTypes
-
getRequiredTokens
public int[] getRequiredTokens()
Description copied from class:AbstractCheckThe tokens that this check must be registered for.- Specified by:
getRequiredTokensin classAbstractCheck- Returns:
- the token set this must be registered for.
- See Also:
TokenTypes
-
visitToken
public void visitToken(DetailAST ast)
Description copied from class:AbstractCheckCalled to process a token.- Overrides:
visitTokenin classAbstractCheck- Parameters:
ast- the token to process
-
shouldCheck
private boolean shouldCheck(DetailAST ast)
Whether we should check this node.- Parameters:
ast- a given node.- Returns:
- whether we should check a given node.
-
checkComment
private void checkComment(DetailAST ast, TextBlock comment)
Performs the various checks against the Javadoc comment.- Parameters:
ast- the AST of the element being documentedcomment- the source lines that make up the Javadoc comment.- See Also:
checkFirstSentenceEnding(DetailAST, TextBlock),checkHtmlTags(DetailAST, TextBlock)
-
checkFirstSentenceEnding
private void checkFirstSentenceEnding(DetailAST ast, TextBlock comment)
Checks that the first sentence ends with proper punctuation. This method uses a regular expression that checks for the presence of a period, question mark, or exclamation mark followed either by whitespace, an HTML element, or the end of string. This method ignores {_AT_inheritDoc} comments for TokenTypes that are valid for {_AT_inheritDoc}.- Parameters:
ast- the current nodecomment- the source lines that make up the Javadoc comment.
-
checkJavadocIsNotEmpty
private void checkJavadocIsNotEmpty(TextBlock comment)
Checks that the Javadoc is not empty.- Parameters:
comment- the source lines that make up the Javadoc comment.
-
getCommentText
private static java.lang.String getCommentText(java.lang.String... comments)
Returns the comment text from the Javadoc.- Parameters:
comments- the lines of Javadoc.- Returns:
- a comment text String.
-
findTextStart
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.- Parameters:
line- the Javadoc comment line of text to scan.- Returns:
- the int index relative to 0 for the start of text or -1 if not found.
-
trimTail
private static void trimTail(java.lang.StringBuilder builder)
Trims any trailing whitespace or the end of Javadoc comment string.- Parameters:
builder- the StringBuilder to trim.
-
checkHtmlTags
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. This code was primarily copied from the DocCheck checkHtml method.- Parameters:
ast- the node with the Javadoccomment- theTextBlockwhich represents the Javadoc comment.
-
checkUnclosedTags
private void checkUnclosedTags(java.util.Deque<HtmlTag> htmlStack, java.lang.String token)
Checks to see if there are any unclosed tags on the stack. The token represents a html tag that has been closed and has a corresponding open tag on the stack. Any tags, except single tags, that were opened (pushed on the stack) after the token are missing a close.- Parameters:
htmlStack- the stack of opened HTML tags.token- the current HTML tag name that has been closed.
-
isSingleTag
private static boolean isSingleTag(HtmlTag tag)
Determines if the HtmlTag is one which does not require a close tag.- Parameters:
tag- the HtmlTag to check.- Returns:
trueif the HtmlTag is a single tag.
-
isAllowedTag
private static boolean isAllowedTag(HtmlTag tag)
Determines if the HtmlTag is one which is allowed in a javadoc.- Parameters:
tag- the HtmlTag to check.- Returns:
trueif the HtmlTag is an allowed html tag.
-
isExtraHtml
private static boolean isExtraHtml(java.lang.String token, java.util.Deque<HtmlTag> htmlStack)Determines if the given token is an extra HTML tag. This indicates that a close tag was found that does not have a corresponding open tag.- Parameters:
token- an HTML tag id for which a close was found.htmlStack- a Stack of previous open HTML tags.- Returns:
falseif a previous open tag was found for the token.
-
setScope
public void setScope(Scope scope)
Sets the scope to check.- Parameters:
scope- a scope.
-
setExcludeScope
public void setExcludeScope(Scope excludeScope)
Set the excludeScope.- Parameters:
excludeScope- a scope.
-
setEndOfSentenceFormat
public void setEndOfSentenceFormat(java.util.regex.Pattern pattern)
Set the format for matching the end of a sentence.- Parameters:
pattern- a pattern.
-
setCheckFirstSentence
public void setCheckFirstSentence(boolean flag)
Sets the flag that determines if the first sentence is checked for proper end of sentence punctuation.- Parameters:
flag-trueif the first sentence is to be checked
-
setCheckHtml
public void setCheckHtml(boolean flag)
Sets the flag that determines if HTML checking is to be performed.- Parameters:
flag-trueif HTML checking is to be performed.
-
setCheckEmptyJavadoc
public void setCheckEmptyJavadoc(boolean flag)
Sets the flag that determines if empty Javadoc checking should be done.- Parameters:
flag-trueif empty Javadoc checking should be done.
-
-