Class JavadocUtilizingTrailingSpaceCheck
- All Implemented Interfaces:
Configurable,Contextualizable
Every line of a Javadoc comment is compared with lineLimit, the preferred line
length. A line is reported when it is longer than the limit, or when it stops early although
the next line would still have fit on it. Blank lines are never reported.
The examples on this page use the default limit of 80 characters, and show the Javadoc
of a method, indented by four spaces.
Lines that stop too early. A line is reported when the whole next line would still fit on it. The fix is to join the two lines, so the comment gets one line shorter. When only a part of the next line would fit, the line is not reported, because moving words up would not make the comment shorter. Lines are never joined with a line that is blank, or that starts with a block tag, an inline tag, a URL or an HTML tag, so the line before such a line is not reported.
// violation, the second line would fit on the first line
/**
* Not part of the API. This is returned when a type
* is not known.
*/
// ok, only a part of the second line would fit on the first line
/**
* Not part of the API. This is returned when a type
* not known to this wrapper is returned.
*/
Lines that could take more words. When validateOnlyJoinableLines is set
to false, words are expected to fill a line up to the limit, so a line is also
reported when only the first word of the next line would still fit on it. The fix is to
move words up until the next word does not fit.
// violation, "not" would fit on the first line
/**
* Not part of the API. This is returned when a type
* not known to this wrapper is returned.
*/
// ok
/**
* Not part of the API. This is returned when a type not known to this
* wrapper is returned.
*/
Lines that end with punctuation. A line that matches ignoreTooShortPattern
is not reported as stopping too early, and no words are moved up to it. By default this is
every line that ends with a period, a colon or a comma, so a sentence, or a part of it,
can end before the limit. Trailing whitespace of the line is ignored when the pattern is
applied. The line after such a line is checked on its own. Such lines can still be
reported as too long.
// ok, the first line ends with a period
/**
* Not part of the API.
* This is returned when a type not known to this wrapper is returned.
*/
// violation, the third line would fit on the second line
/**
* Not part of the API.
* This is returned when a type not
* known to this wrapper is returned.
*/
Lines that are too long. A line is reported when it is longer than the limit. The fix is to wrap it at a word.
// violation, longer than 80 characters
/**
* Creates a client for the service and loads its settings from the given file path.
*/
// ok
/**
* Creates a client for the service and loads its settings from the given
* file path.
*/
Block tags. The value of a block tag, such as @param, @return
or @throws, is checked like any other text. A line that has only the name of
the block tag is not reported, so the value can start on the next line. A line that
starts with a block tag is never joined to the line before it.
// violation, longer than 80 characters
/**
* @return Date the company was closed or dissolved, see {@link #getStatus()}.
*/
// ok, the value starts on the next line
/**
* @return
* Date the company was closed or dissolved, see {@link #getStatus()}.
*/
// ok, wrapped before the inline tag
/**
* @return Date the company was closed or dissolved, see
* {@link #getStatus()}.
*/
Inline tags. An inline tag, such as a link to a class, cannot be wrapped. A line that starts with an inline tag and has nothing else on it is not reported, even when it is longer than the limit. When other words follow on the same line, the line is reported, because those words can move to the next line. A block tag never gets this exception for its value.
// ok, a tag alone on a line may be longer than 80 characters
/**
* Refer to the method
* {@link com.example.registry.status.CompanyStatusRegistry#findStatus(String)}
*/
// violation, other words follow the tag
/**
* {@link com.example.registry.status.CompanyStatusRegistry#findStatus(String)} finds it.
*/
URLs and links. A URL cannot be wrapped, so a line that matches
ignorePattern is not reported as too long. By default this is every line that has
a URL or the href attribute of a link. A block tag line whose value starts with a
link, such as @see <a href="...">, is therefore not reported, whether the link ends
on that line or on a later one. Such lines can still be reported as stopping too early.
// ok, lines that have a URL or a link may be longer than 80 characters
/**
* See https://example.com/documentation/company-registry/api/v2/status-endpoints/details
*
* @see <a href="https://example.com/documentation/company-registry/api/v2/status">
* Status endpoints</a>
*/
HTML. A line that starts with an HTML tag, such as <p> or </li>, is
not reported as too short, and it is not joined to the line before it, so tags can be
placed on their own lines. Such a line, and the text inside an HTML element, must still
not be longer than the limit.
// ok, a line that starts with an HTML tag is never too short
/**
* <p>Companies House allows you to query
* information on registered companies in Britain.</p>
*/
// violation, text inside an HTML element is checked
/**
* <ul>
* <li>Static configuration reads files from the classpath or a given path.</li>
* </ul>
*/
// ok
/**
* <ul>
* <li>Static configuration reads files from the classpath or a given
* path.</li>
* </ul>
*/
Pre and code elements. The content of <pre> and <code> elements,
and HTML comments, is ignored, so code samples can keep their own formatting.
// ok, the content of a pre element is ignored
/**
* Example:
* <pre>
* new CompanyRegistry().findStatus("00000006").orElseThrow(NotRegisteredException::new);
* </pre>
*/
- Since:
- 14.4.0
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final classRepresents a logical line in a Javadoc comment.Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
AbstractAutomaticBean.OutputStreamOptions -
Field Summary
FieldsModifier and TypeFieldDescriptionCurrent line being built.private static final intDefault maximum line length.private PatternPattern for lines that are not reported as too long.private PatternPattern for lines that are not reported as too short.private static final PatternPattern for the indentation and the leading asterisk of a Javadoc line.private intConfigurable line length limit.private final List<JavadocUtilizingTrailingSpaceCheck.JavadocLine> Collects logical Javadoc lines for the current Javadoc comment.static final StringMessage key for too long Javadoc line.static final StringMessage key for too short Javadoc line.private DetailNodeThe node whose subtree is currently being skipped, or null.private static final PatternPattern that recognizes URLs.private booleanControl whether a line is reported as too short only when the whole next line fits on it.Fields inherited from class com.puppycrawl.tools.checkstyle.checks.javadoc.AbstractJavadocCheck
MSG_JAVADOC_PARSE_RULE_ERROR, MSG_KEY_UNCLOSED_HTML_TAG -
Constructor Summary
ConstructorsConstructorDescriptionCreates a newJavadocUtilizingTrailingSpaceCheckinstance. -
Method Summary
Modifier and TypeMethodDescriptionvoidbeginJavadocTree(DetailNode rootAst) Called before the starting to process a tree.private booleancanPullFromNextLine(int currentIndex) Checks if content can be pulled from the next line.private voidCommits the current line to the lines list and clears it.voidfinishJavadocTree(DetailNode rootAst) Called after finished processing a tree.int[]Returns the default javadoc token types a check is interested in.private intReturns the length of the content that has to fit on the line before the given line, for that line to be too short.int[]The javadoc tokens that this check must be registered for.private voidhandleBlockTag(DetailNode node) Handles a Javadoc block tag node.private voidhandleHtmlElement(DetailNode element) Handles an HTML element.private voidhandleHtmlTag(DetailNode node) Handles an HTML start tag, end tag or void element.private voidhandleInlineTag(DetailNode node) Handles a Javadoc inline tag node.private voidhandleNewline(DetailNode node) Handles a newline token.private voidhandleText(DetailNode node) Handles a TEXT node.private voidinitCurrentLine(int lineNumber) Initializes the current line if needed.private booleanisTooShort(int index) Decides whether the line at the given index is too short (i.e. we should have pulled content from the next line).private static DetailNodelastLeaf(DetailNode node) Finds the last leaf of the subtree of the given node.voidCalled after all the child nodes have been process.voidsetIgnorePattern(Pattern pattern) Setter to specify pattern for lines that are not reported as too long.voidsetIgnoreTooShortPattern(Pattern pattern) Setter to specify pattern for lines that are not reported as too short.voidsetLineLimit(int limit) Setter to specify the line length limit.voidsetValidateOnlyJoinableLines(boolean validateOnlyJoinableLines) Setter to control whether a line is reported as too short only when the whole next line fits on it.private voidskipNode(DetailNode node) Skips the subtree of the given node.private voidupdateLengthToEnd(DetailNode node) Extends the current line to the end of the given node, if the node ends on the line where it starts.private voidValidates all collected lines for violations.voidCalled to process a Javadoc token.Methods inherited from class com.puppycrawl.tools.checkstyle.checks.javadoc.AbstractJavadocCheck
acceptJavadocWithNonTightHtml, beginTree, destroy, getAcceptableJavadocTokens, getAcceptableTokens, getBlockCommentAst, getDefaultTokens, getRequiredTokens, init, isCommentNodesRequired, log, setJavadocTokens, setViolateExecutionOnNonTightHtml, visitTokenMethods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck
clearViolations, finishTree, getFileContents, getFilePath, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, leaveToken, log, log, log, setFileContents, setTabWidth, setTokensMethods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverityMethods inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
configure, contextualize, getConfiguration, setupChild
-
Field Details
-
MSG_TOO_SHORT
Message key for too short Javadoc line.- See Also:
-
MSG_TOO_LONG
Message key for too long Javadoc line.- See Also:
-
URL_PATTERN
Pattern that recognizes URLs. -
LINE_PREFIX
Pattern for the indentation and the leading asterisk of a Javadoc line. -
DEFAULT_LINE_LIMIT
Default maximum line length.- See Also:
-
lines
Collects logical Javadoc lines for the current Javadoc comment. -
lineLimit
Configurable line length limit. -
ignorePattern
Pattern for lines that are not reported as too long. -
ignoreTooShortPattern
Pattern for lines that are not reported as too short. -
validateOnlyJoinableLines
Control whether a line is reported as too short only when the whole next line fits on it. -
currentLine
Current line being built. -
skipRoot
The node whose subtree is currently being skipped, or null.
-
-
Constructor Details
-
JavadocUtilizingTrailingSpaceCheck
public JavadocUtilizingTrailingSpaceCheck()Creates a newJavadocUtilizingTrailingSpaceCheckinstance.
-
-
Method Details
-
getDefaultJavadocTokens
Description copied from class:AbstractJavadocCheckReturns the default javadoc token types a check is interested in.- Specified by:
getDefaultJavadocTokensin classAbstractJavadocCheck- Returns:
- the default javadoc token types
- See Also:
-
getRequiredJavadocTokens
Description copied from class:AbstractJavadocCheckThe javadoc tokens that this check must be registered for.- Overrides:
getRequiredJavadocTokensin classAbstractJavadocCheck- Returns:
- the javadoc token set this must be registered for.
- See Also:
-
setLineLimit
Setter to specify the line length limit.- Parameters:
limit- the maximum length to target for Javadoc lines- Since:
- 14.4.0
-
setIgnorePattern
Setter to specify pattern for lines that are not reported as too long.- Parameters:
pattern- a pattern.- Since:
- 14.4.0
-
setIgnoreTooShortPattern
Setter to specify pattern for lines that are not reported as too short.- Parameters:
pattern- a pattern.- Since:
- 14.4.0
-
setValidateOnlyJoinableLines
Setter to control whether a line is reported as too short only when the whole next line fits on it.- Parameters:
validateOnlyJoinableLines- true if a line is reported as too short only when the whole next line fits on it, false if the first word of the next line is enough.- Since:
- 14.4.0
-
beginJavadocTree
Description copied from class:AbstractJavadocCheckCalled before the starting to process a tree.- Overrides:
beginJavadocTreein classAbstractJavadocCheck- Parameters:
rootAst- the root of the tree
-
finishJavadocTree
Description copied from class:AbstractJavadocCheckCalled after finished processing a tree.- Overrides:
finishJavadocTreein classAbstractJavadocCheck- Parameters:
rootAst- the root of the tree
-
visitJavadocToken
Description copied from class:AbstractJavadocCheckCalled to process a Javadoc token.- Specified by:
visitJavadocTokenin classAbstractJavadocCheck- Parameters:
ast- the token to process
-
leaveJavadocToken
Description copied from class:AbstractJavadocCheckCalled after all the child nodes have been process.- Overrides:
leaveJavadocTokenin classAbstractJavadocCheck- Parameters:
ast- the token leaving
-
validateLines
Validates all collected lines for violations. -
isTooShort
Decides whether the line at the given index is too short (i.e. we should have pulled content from the next line).- Parameters:
index- the line index to check- Returns:
- true if the line is too short
-
handleNewline
Handles a newline token.- Parameters:
node- the newline node
-
handleBlockTag
Handles a Javadoc block tag node. A line that starts a block tag cannot be merged into the previous line, but its value is still validated.- Parameters:
node- the block tag node
-
handleText
Handles a TEXT node.- Parameters:
node- the text node
-
handleInlineTag
Handles a Javadoc inline tag node. The tag is measured as a whole and its content is skipped.- Parameters:
node- the inline tag node
-
handleHtmlElement
Handles an HTML element. The content ofpreandcodeelements is skipped.- Parameters:
element- the HTML element node
-
handleHtmlTag
Handles an HTML start tag, end tag or void element. A line that starts with such a tag is never reported as too short, but it can still be too long.- Parameters:
node- the HTML tag node
-
skipNode
Skips the subtree of the given node. If the node spans several lines, the line where it starts is marked, because the lines after it are not adjacent to the lines that were skipped.- Parameters:
node- the node to skip
-
updateLengthToEnd
Extends the current line to the end of the given node, if the node ends on the line where it starts.- Parameters:
node- the node to measure
-
lastLeaf
Finds the last leaf of the subtree of the given node.- Parameters:
node- the root of the subtree- Returns:
- the last leaf, which is the node itself if it has no children
-
canPullFromNextLine
Checks if content can be pulled from the next line.- Parameters:
currentIndex- current line index- Returns:
- true if the content to pull from the next content line fits within lineLimit
-
getPullLength
Returns the length of the content that has to fit on the line before the given line, for that line to be too short.- Parameters:
nextLine- the line to pull content from- Returns:
- the length of the whole content of the line if only joinable lines are validated, the length of its first word otherwise
-
initCurrentLine
Initializes the current line if needed.- Parameters:
lineNumber- the source line number
-
commitCurrentLine
Commits the current line to the lines list and clears it.
-