Class JavadocStyleCheck
- java.lang.Object
-
- com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
-
- 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
Validates Javadoc comments to help ensure they are well formed.The following checks are performed:
-
Ensures the first sentence ends with proper punctuation
(That is a period, question mark, or exclamation mark, by default).
Note that this check is not applied to inline
@return
tags, because the Javadoc tools automatically appends a period to the end of the tag content. Javadoc automatically places the first sentence in the method summary table and index. Without proper punctuation the Javadoc may be malformed. All items eligible for the{@inheritDoc}
tag are exempt from this requirement. -
Check text for Javadoc statements that do not have any description.
This includes both completely empty Javadoc, and Javadoc with only tags
such as
@param
and@return
. - Check text for incomplete HTML tags. Verifies that HTML tags have corresponding end tags and issues an "Unclosed HTML tag found:" error if not. An "Extra HTML tag found:" error is issued if an end tag is found without a previous open tag.
- Check that a package Javadoc comment is well-formed (as described above).
- Check for allowed HTML tags. The list of allowed HTML tags is "a", "abbr", "acronym", "address", "area", "b", "bdo", "big", "blockquote", "br", "caption", "cite", "code", "colgroup", "dd", "del", "dfn", "div", "dl", "dt", "em", "fieldset", "font", "h1", "h2", "h3", "h4", "h5", "h6", "hr", "i", "img", "ins", "kbd", "li", "ol", "p", "pre", "q", "samp", "small", "span", "strong", "sub", "sup", "table", "tbody", "td", "tfoot", "th", "thead", "tr", "tt", "u", "ul", "var".
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.
-
Property
checkEmptyJavadoc
- Control whether to check if the Javadoc is missing a describing text. Type isboolean
. Default value isfalse
. -
Property
checkFirstSentence
- Control whether to check the first sentence for proper end of sentence. Type isboolean
. Default value istrue
. -
Property
checkHtml
- Control whether to check for incomplete HTML tags. Type isboolean
. Default value istrue
. -
Property
endOfSentenceFormat
- Specify the format for matching the end of a sentence. Type isjava.util.regex.Pattern
. Default value is"([.?!][ \t\n\r\f<])|([.?!]$)"
. -
Property
excludeScope
- Specify the visibility scope where Javadoc comments are not checked. Type iscom.puppycrawl.tools.checkstyle.api.Scope
. Default value isnull
. -
Property
scope
- Specify the visibility scope where Javadoc comments are checked. Type iscom.puppycrawl.tools.checkstyle.api.Scope
. Default value isprivate
. -
Property
tokens
- tokens to check Type isjava.lang.String[]
. Validation type istokenSet
. 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.
Parent is
com.puppycrawl.tools.checkstyle.TreeWalker
Violation Message Keys:
-
javadoc.empty
-
javadoc.extraHtml
-
javadoc.incompleteTag
-
javadoc.noPeriod
-
javadoc.unclosedHtml
- Since:
- 3.2
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
AbstractAutomaticBean.OutputStreamOptions
-
-
Field Summary
Fields Modifier and Type Field Description private static Set<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 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.private static Pattern
INLINE_RETURN_TAG_PATTERN
Specify the format for inline return Javadoc.static String
MSG_EMPTY
Message property key for the Empty Javadoc message.static String
MSG_EXTRA_HTML
Message property key for the Extra HTML message.static String
MSG_INCOMPLETE_TAG
Message property key for the Incomplete Tag message.static String
MSG_NO_PERIOD
Message property key for the No Javadoc end of Sentence Period message.static 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 Pattern
SENTENCE_SEPARATOR
Specify the format for first word in javadoc.private static Set<String>
SINGLE_TAGS
HTML 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 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(Deque<HtmlTag> htmlStack, String token)
Checks to see if there are any unclosed tags on the stack.private static int
findTextStart(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 String
getCommentText(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(String token, 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(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(StringBuilder builder)
Trims any trailing whitespace or the end of Javadoc comment string.void
visitToken(DetailAST ast)
Called to process a token.-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck
beginTree, clearViolations, destroy, finishTree, getFileContents, getFilePath, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, init, isCommentNodesRequired, leaveToken, log, log, log, setFileContents, setTabWidth, setTokens
-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverity
-
Methods inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
configure, contextualize, getConfiguration, setupChild
-
-
-
-
Field Detail
-
MSG_EMPTY
public static final String MSG_EMPTY
Message property key for the Empty Javadoc message.- See Also:
- Constant Field Values
-
MSG_NO_PERIOD
public static final String MSG_NO_PERIOD
Message property key for the No Javadoc end of Sentence Period message.- See Also:
- Constant Field Values
-
MSG_INCOMPLETE_TAG
public static final String MSG_INCOMPLETE_TAG
Message property key for the Incomplete Tag message.- See Also:
- Constant Field Values
-
MSG_UNCLOSED_HTML
public static final String MSG_UNCLOSED_HTML
Message property key for the Unclosed HTML message.- See Also:
- Constant Field Values
-
MSG_EXTRA_HTML
public static final String MSG_EXTRA_HTML
Message property key for the Extra HTML message.- See Also:
- Constant Field Values
-
SINGLE_TAGS
private static final Set<String> SINGLE_TAGS
HTML tags that do not require a close tag.
-
ALLOWED_TAGS
private static final Set<String> ALLOWED_TAGS
HTML tags that are allowed in java docs. From w3schools:
The forms and structure tags are not allowed
-
INLINE_RETURN_TAG_PATTERN
private static final Pattern INLINE_RETURN_TAG_PATTERN
Specify the format for inline return Javadoc.
-
SENTENCE_SEPARATOR
private static final Pattern SENTENCE_SEPARATOR
Specify the format for first word in javadoc.
-
excludeScope
private Scope excludeScope
Specify the visibility scope where Javadoc comments are not checked.
-
endOfSentenceFormat
private Pattern endOfSentenceFormat
Specify the format for matching the end of a sentence.
-
checkFirstSentence
private boolean checkFirstSentence
Control whether to check the first sentence for proper end of sentence.
-
checkHtml
private boolean checkHtml
Control whether to check for incomplete HTML tags.
-
checkEmptyJavadoc
private boolean checkEmptyJavadoc
Control whether to check if the Javadoc is missing a describing text.
-
-
Constructor Detail
-
JavadocStyleCheck
public JavadocStyleCheck()
-
-
Method Detail
-
getDefaultTokens
public int[] getDefaultTokens()
Description copied from class:AbstractCheck
Returns the default token a check is interested in. Only used if the configuration for a check does not define the tokens.- Specified by:
getDefaultTokens
in classAbstractCheck
- Returns:
- the default tokens
- See Also:
TokenTypes
-
getAcceptableTokens
public int[] getAcceptableTokens()
Description copied from class:AbstractCheck
The 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:
getAcceptableTokens
in classAbstractCheck
- Returns:
- the token set this check is designed for.
- See Also:
TokenTypes
-
getRequiredTokens
public int[] getRequiredTokens()
Description copied from class:AbstractCheck
The tokens that this check must be registered for.- Specified by:
getRequiredTokens
in classAbstractCheck
- Returns:
- the token set this must be registered for.
- See Also:
TokenTypes
-
visitToken
public void visitToken(DetailAST ast)
Description copied from class:AbstractCheck
Called to process a token.- Overrides:
visitToken
in 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 String getCommentText(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(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(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
- theTextBlock
which represents the Javadoc comment.
-
checkUnclosedTags
private void checkUnclosedTags(Deque<HtmlTag> htmlStack, 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:
true
if 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:
true
if the HtmlTag is an allowed html tag.
-
isExtraHtml
private static boolean isExtraHtml(String token, 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:
false
if a previous open tag was found for the token.
-
setScope
public void setScope(Scope scope)
Setter to specify the visibility scope where Javadoc comments are checked.- Parameters:
scope
- a scope.- Since:
- 3.2
-
setExcludeScope
public void setExcludeScope(Scope excludeScope)
Setter to specify the visibility scope where Javadoc comments are not checked.- Parameters:
excludeScope
- a scope.- Since:
- 3.4
-
setEndOfSentenceFormat
public void setEndOfSentenceFormat(Pattern pattern)
Setter to specify the format for matching the end of a sentence.- Parameters:
pattern
- a pattern.- Since:
- 5.0
-
setCheckFirstSentence
public void setCheckFirstSentence(boolean flag)
Setter to control whether to check the first sentence for proper end of sentence.- Parameters:
flag
-true
if the first sentence is to be checked- Since:
- 3.2
-
setCheckHtml
public void setCheckHtml(boolean flag)
Setter to control whether to check for incomplete HTML tags.- Parameters:
flag
-true
if HTML checking is to be performed.- Since:
- 3.2
-
setCheckEmptyJavadoc
public void setCheckEmptyJavadoc(boolean flag)
Setter to control whether to check if the Javadoc is missing a describing text.- Parameters:
flag
-true
if empty Javadoc checking should be done.- Since:
- 3.4
-
-