Class JavadocLinkFirstOccurrenceCheck
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.AbstractJavadocCheck
com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocLinkFirstOccurrenceCheck
- All Implemented Interfaces:
Configurable,Contextualizable
Checks that in Javadoc comments, each API name is linked with
{@link} or {@linkplain} only on its first occurrence.
Subsequent links to the same API name in the same comment are flagged.
Rationale: From the Documentation Comments style guide, links call attention to themselves by their color and underline in HTML, and by their length in source code doc comments. Linking the same name multiple times is redundant.
Two links are considered to reference the same API name if they resolve to
the same canonical name. Simple names are resolved through explicit imports,
types declared in the current file, star imports and the
java.lang package. Names containing dots are resolved through
imports of their outermost segment; otherwise they are compared as written.
- Since:
- 14.1.0
-
Nested Class Summary
Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
AbstractAutomaticBean.OutputStreamOptions -
Field Summary
FieldsModifier and TypeFieldDescriptionSet of simple names of types declared in the current file.private static final charDot.Map of imported simple names to fully qualified names.Set of reference keys already seen in the current Javadoc comment.static final StringA key is pointing to the warning message text in "messages.properties" file.Set of star import base packages.Tokens of type declarations.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 newJavadocLinkFirstOccurrenceCheckinstance. -
Method Summary
Modifier and TypeMethodDescriptionvoidbeginJavadocTree(DetailNode rootAst) Called before the starting to process a tree.voidCalled before the starting to process a tree.private voidcollectDeclaredTypeNames(DetailAST rootAST) Records the simple names of all type declarations.int[]Returns the default javadoc token types a check is interested in.private static StringgetNodeText(DetailNode node) Recursively builds the full text of a node by concatenating the text of all its leaf descendants.int[]The javadoc tokens that this check must be registered for.int[]The tokens that this check must be registered for.private voidhandleImport(DetailAST ast) Processes import statements and records imported names.private StringresolveClass(String name) Resolves a class name to its canonical name through imports, types declared in the current file, star imports and thejava.langpackage.private StringresolveOuterSegment(String name, int dotIndex) Resolves the outermost segment of a dotted class name through imports, keeping the remainder unchanged.private StringresolveReference(String reference) Resolves a reference text to a canonical key for identity comparison.private StringresolveSimpleClassName(String name) Resolves a simple class name through imports, types declared in the current file, star imports and thejava.langpackage.voidCalled to process a Javadoc token.voidvisitToken(DetailAST ast) Called to process a token.Methods inherited from class com.puppycrawl.tools.checkstyle.checks.javadoc.AbstractJavadocCheck
acceptJavadocWithNonTightHtml, destroy, finishJavadocTree, getAcceptableJavadocTokens, getAcceptableTokens, getBlockCommentAst, getDefaultTokens, init, isCommentNodesRequired, leaveJavadocToken, log, setJavadocTokens, setViolateExecutionOnNonTightHtmlMethods 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_KEY
A key is pointing to the warning message text in "messages.properties" file.- See Also:
-
DOT
Dot.- See Also:
-
TYPE_DECLARATION_TOKENS
Tokens of type declarations. -
linkedNames
Set of reference keys already seen in the current Javadoc comment. -
importedNames
Map of imported simple names to fully qualified names. -
starImports
Set of star import base packages. -
declaredTypeNames
Set of simple names of types declared in the current file.
-
-
Constructor Details
-
JavadocLinkFirstOccurrenceCheck
public JavadocLinkFirstOccurrenceCheck()Creates a newJavadocLinkFirstOccurrenceCheckinstance.
-
-
Method Details
-
getRequiredTokens
Description copied from class:AbstractCheckThe tokens that this check must be registered for.- Overrides:
getRequiredTokensin classAbstractJavadocCheck- Returns:
- the token set this must be registered for.
- See Also:
-
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:
-
beginTree
Description copied from class:AbstractCheckCalled before the starting to process a tree. Ideal place to initialize information that is to be collected whilst processing a tree.- Overrides:
beginTreein classAbstractJavadocCheck- Parameters:
rootAST- the root of the tree
-
beginJavadocTree
Description copied from class:AbstractJavadocCheckCalled before the starting to process a tree.- Overrides:
beginJavadocTreein classAbstractJavadocCheck- Parameters:
rootAst- the root of the tree
-
visitToken
Description copied from class:AbstractCheckCalled to process a token.- Overrides:
visitTokenin classAbstractJavadocCheck- Parameters:
ast- the token to process
-
visitJavadocToken
Description copied from class:AbstractJavadocCheckCalled to process a Javadoc token.- Specified by:
visitJavadocTokenin classAbstractJavadocCheck- Parameters:
ast- the token to process
-
handleImport
Processes import statements and records imported names.- Parameters:
ast- import node
-
collectDeclaredTypeNames
Records the simple names of all type declarations. The whole tree is scanned so that types declared after their references are also taken into account.- Parameters:
rootAST- the root of the tree to scan
-
resolveReference
Resolves a reference text to a canonical key for identity comparison. The class part of the reference is resolved through imports and types declared in the current file.- Parameters:
reference- the raw reference text- Returns:
- the resolved identity key
-
resolveClass
Resolves a class name to its canonical name through imports, types declared in the current file, star imports and thejava.langpackage. Names containing dots have only their outermost segment resolved; otherwise they are returned unchanged.- Parameters:
name- the class name- Returns:
- the resolved canonical name
-
resolveSimpleClassName
Resolves a simple class name through imports, types declared in the current file, star imports and thejava.langpackage.- Parameters:
name- the simple class name- Returns:
- the resolved canonical name
-
resolveOuterSegment
Resolves the outermost segment of a dotted class name through imports, keeping the remainder unchanged.- Parameters:
name- the dotted class namedotIndex- the index of the first dot in the name- Returns:
- the resolved canonical name
-
getNodeText
Recursively builds the full text of a node by concatenating the text of all its leaf descendants.- Parameters:
node- the node to get text from- Returns:
- the concatenated text, or null if the node is null
-