Class JavadocMethodCheck
- All Implemented Interfaces:
Configurable,Contextualizable
Violates parameters and type parameters for which no param tags are present can
be suppressed by defining property allowMissingParamTags.
Violates methods which return non-void but for which no return tag is present can
be suppressed by defining property allowMissingReturnTag.
Violates exceptions which are declared to be thrown (by throws in the method
signature or by throw new in the method body), but for which no throws tag is
present by activation of property validateThrows.
Note that throw new is not checked in the following places:
- Inside a try block (with catch). It is not possible to determine if the thrown exception can be caught by the catch block as there is no knowledge of the inheritance hierarchy, so the try block is ignored entirely. However, catch and finally blocks, as well as try blocks without catch, are still checked.
- Local classes, anonymous classes and lambda expressions. It is not known when the throw statements inside such classes are going to be evaluated, so they are ignored.
ATTENTION: Checkstyle does not have information about hierarchy of exception types so usage of base class is considered as separate exception type. As workaround, you need to specify both types in javadoc (parent and exact type).
Javadoc is not required on a method that is tagged with the @Override
annotation. However, under Java 5 it is not possible to mark a method required
for an interface (this was corrected under Java 6). Hence, Checkstyle
supports using the convention of using a single {@inheritDoc} tag
instead of all the other tags.
Note that only inheritable items will allow the {@inheritDoc}
tag to be used in place of comments. Static methods at all visibilities,
private non-static methods and constructors are not inheritable.
For example, if the following method is implementing a method required by an interface, then the Javadoc could be done as:
/** {@inheritDoc} */
public int checkReturnTag(final int aTagIndex,
JavadocTag[] aTags,
int aLineNo)
- Since:
- 3.0
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final recordContains class'sToken.private static final classStores useful information about declared exception.private static final recordRepresents text element with location in the text.Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
AbstractAutomaticBean.OutputStreamOptions -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate AccessModifierOption[]Specify the access modifiers where Javadoc comments are checked.Specify annotations that allow missed documentation.private booleanControl whether to allow inline return tags.private booleanControl whether to ignore violations when a method has parameters but does not have matchingparamtags in the javadoc.private booleanControl whether to ignore violations when a method returns non-void type and does not have areturntag in the javadoc.private DetailASTJava AST node whose attached Javadoc is currently being processed.private static final StringHtml element end symbol.private static final StringHtml element start symbol.private final List<JavadocTag> Javadoc tags collected from the current Javadoc tree.static final StringA key is pointing to the warning message text in "messages.properties" file.static final StringA key is pointing to the warning message text in "messages.properties" file.static final StringA key is pointing to the warning message text in "messages.properties" file.static final StringA key is pointing to the warning message text in "messages.properties" file.static final StringA key is pointing to the warning message text in "messages.properties" file.static final StringA key is pointing to the warning message text in "messages.properties" file.static final StringA key is pointing to the warning message text in "messages.properties" file.private booleanControl whether to validatethrowstags.Fields inherited from class com.puppycrawl.tools.checkstyle.checks.javadoc.AbstractJavadocCheck
MSG_JAVADOC_PARSE_RULE_ERROR, MSG_KEY_UNCLOSED_HTML_TAG -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidbeginJavadocTree(DetailNode rootAst) Called before the starting to process a tree.private voidChecks collected Javadoc tags against the current AST node.private voidcheckParamTags(List<JavadocTag> tags, DetailAST parent, boolean reportExpectedTags) Checks a set of tags for matching parameters.private voidcheckRecordParamTags(List<JavadocTag> tags, DetailAST compactDef, boolean reportExpectedTags) Checks if all record components in a compact constructor have corresponding@paramtags.private voidcheckReturnTag(List<JavadocTag> tags, int lineNo, boolean reportExpectedTags) Checks for only one return tag.private voidcheckThrowsTags(List<JavadocTag> tags, List<JavadocMethodCheck.ExceptionInfo> throwsList, boolean reportExpectedTags) Checks a set of tags for matching throws.private voidCollects an inheritDoc tag.private voidcollectParam(DetailNode ast) Collects a param tag.private voidcollectReturn(DetailNode ast) Collects a return tag if it has a description.private voidcollectThrows(DetailNode ast, String tagName) Collects a throws or exception tag.private static List<JavadocMethodCheck.ExceptionInfo> combineExceptionInfo(Collection<JavadocMethodCheck.ExceptionInfo> first, Iterable<JavadocMethodCheck.ExceptionInfo> second) Combine ExceptionInfo collections together by matching names.findTokensInAstByType(DetailAST root, int astType) Finds node of specified type among root children, siblings, siblings children on any deep level.voidfinishJavadocTree(DetailNode rootAst) Called after finished processing a tree.int[]The configurable token set.int[]Returns the default javadoc token types a check is interested in.int[]Returns the default token a check is interested in.private static JavadocMethodCheck.ExceptionInfoGet ExceptionInfo instance.private static DetailASTGet node where class name of exception starts.getParameters(DetailAST ast) Computes the parameter nodes for a method.getRecordComponents(DetailAST recordDef) Retrieves the list of record components from a given record definition.private static DetailASTgetRecordDef(DetailAST ast) Finds the nearest ancestor record definition node for the given AST node.int[]The javadoc tokens that this check must be registered for.final int[]The tokens that this check must be registered for.private static List<JavadocMethodCheck.ExceptionInfo> getThrowed(DetailAST methodAst) Get ExceptionInfo for all exceptions that throws in method code by 'throw new'.private static List<JavadocMethodCheck.ExceptionInfo> Computes the exception nodes for a method.private booleanhasShortCircuitTag(DetailAST ast, List<JavadocTag> tags) Validates whether the Javadoc has a short circuit tag.private static booleanisClassNamesSame(String class1, String class2) Check that class names are same by short name of class.private static booleanCheck that ExceptionInfo objects are same by name.private static booleanisInIgnoreBlock(DetailAST methodBodyAst, DetailAST throwAst) Checks if a 'throw' usage is contained within a block that should be ignored.private static voidprocessThrows(Iterable<JavadocMethodCheck.ExceptionInfo> throwsIterable, String documentedClassName) Verifies that documented exception is in throws.private static booleanremoveMatchingParam(Iterable<DetailAST> params, String paramName) Remove parameter from params collection by name.private static booleansearchMatchingTypeParameter(Iterable<DetailAST> typeParams, String requiredTypeName) Returns true if required type found in type parameters.voidsetAccessModifiers(AccessModifierOption... accessModifiers) Setter to specify the access modifiers where Javadoc comments are checked.voidsetAllowedAnnotations(String... userAnnotations) Setter to specify annotations that allow missed documentation.voidsetAllowInlineReturn(boolean value) Setter to control whether to allow inline return tags.voidsetAllowMissingParamTags(boolean flag) Setter to control whether to ignore violations when a method has parameters but does not have matchingparamtags in the javadoc.voidsetAllowMissingReturnTag(boolean flag) Setter to control whether to ignore violations when a method returns non-void type and does not have areturntag in the javadoc.voidsetValidateThrows(boolean value) Setter to control whether to validatethrowstags.voidsetViolateExecutionOnNonTightHtml(boolean shouldReportViolation) Setter to control when to print violations if the Javadoc being examined by this check violates the tight html rules defined at Tight-HTML Rules.private booleanshouldCheck(DetailAST ast) Checks whether the given declaration should be validated.voidCalled to process a Javadoc token.final voidvisitToken(DetailAST ast) Called to process a token.Methods inherited from class com.puppycrawl.tools.checkstyle.checks.javadoc.AbstractJavadocCheck
acceptJavadocWithNonTightHtml, beginTree, destroy, getAcceptableJavadocTokens, getBlockCommentAst, init, isCommentNodesRequired, leaveJavadocToken, setJavadocTokensMethods 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_CLASS_INFO
A key is pointing to the warning message text in "messages.properties" file.- See Also:
-
MSG_UNUSED_TAG_GENERAL
A key is pointing to the warning message text in "messages.properties" file.- See Also:
-
MSG_INVALID_INHERIT_DOC
A key is pointing to the warning message text in "messages.properties" file.- See Also:
-
MSG_UNUSED_TAG
A key is pointing to the warning message text in "messages.properties" file.- See Also:
-
MSG_EXPECTED_TAG
A key is pointing to the warning message text in "messages.properties" file.- See Also:
-
MSG_RETURN_EXPECTED
A key is pointing to the warning message text in "messages.properties" file.- See Also:
-
MSG_DUPLICATE_TAG
A key is pointing to the warning message text in "messages.properties" file.- See Also:
-
ELEMENT_START
Html element start symbol.- See Also:
-
ELEMENT_END
Html element end symbol.- See Also:
-
javadocTags
Javadoc tags collected from the current Javadoc tree. -
allowInlineReturn
Control whether to allow inline return tags. -
accessModifiers
Specify the access modifiers where Javadoc comments are checked. -
validateThrows
Control whether to validatethrowstags. -
allowMissingParamTags
Control whether to ignore violations when a method has parameters but does not have matchingparamtags in the javadoc. -
allowMissingReturnTag
Control whether to ignore violations when a method returns non-void type and does not have areturntag in the javadoc. -
allowedAnnotations
Specify annotations that allow missed documentation. -
currentAst
Java AST node whose attached Javadoc is currently being processed.
-
-
Constructor Details
-
JavadocMethodCheck
public JavadocMethodCheck()
-
-
Method Details
-
setAllowInlineReturn
Setter to control whether to allow inline return tags.- Parameters:
value- abooleanvalue- Since:
- 10.23.0
-
setValidateThrows
Setter to control whether to validatethrowstags.- Parameters:
value- user's value.- Since:
- 6.0
-
setAllowedAnnotations
Setter to specify annotations that allow missed documentation.- Parameters:
userAnnotations- user's value.- Since:
- 6.0
-
setAccessModifiers
Setter to specify the access modifiers where Javadoc comments are checked.- Parameters:
accessModifiers- access modifiers.- Since:
- 8.42
-
setAllowMissingParamTags
Setter to control whether to ignore violations when a method has parameters but does not have matchingparamtags in the javadoc.- Parameters:
flag- aBooleanvalue- Since:
- 3.1
-
setAllowMissingReturnTag
Setter to control whether to ignore violations when a method returns non-void type and does not have areturntag in the javadoc.- Parameters:
flag- aBooleanvalue- Since:
- 3.1
-
setViolateExecutionOnNonTightHtml
Setter to control when to print violations if the Javadoc being examined by this check violates the tight html rules defined at Tight-HTML Rules.- Overrides:
setViolateExecutionOnNonTightHtmlin classAbstractJavadocCheck- Parameters:
shouldReportViolation- value to which the field shall be set to- Since:
- 8.3
-
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:
-
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.- Overrides:
getDefaultTokensin classAbstractJavadocCheck- Returns:
- the default tokens
- See Also:
-
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.- Overrides:
getAcceptableTokensin classAbstractJavadocCheck- Returns:
- the token set this check is designed for.
- See Also:
-
visitToken
Description copied from class:AbstractCheckCalled to process a token.- Overrides:
visitTokenin classAbstractJavadocCheck- Parameters:
ast- the token to process
-
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
-
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:
-
visitJavadocToken
Description copied from class:AbstractJavadocCheckCalled to process a Javadoc token.- Specified by:
visitJavadocTokenin classAbstractJavadocCheck- Parameters:
ast- the token to process
-
collectReturn
Collects a return tag if it has a description.- Parameters:
ast- the return tag node
-
collectInheritDoc
Collects an inheritDoc tag. -
collectParam
Collects a param tag.- Parameters:
ast- the param tag node
-
collectThrows
Collects a throws or exception tag.- Parameters:
ast- the throws or exception tag nodetagName- the tag name
-
checkCollectedTags
Checks collected Javadoc tags against the current AST node. -
shouldCheck
Checks whether the given declaration should be validated.The declaration is checked only when both its own access modifier and the access modifier of the surrounding type match the configured
accessModifiers.- Parameters:
ast- the method, constructor, annotation field, or compact constructor AST node to check- Returns:
trueif the declaration is inside the configured access scope
-
getRecordComponents
Retrieves the list of record components from a given record definition.- Parameters:
recordDef- the AST node representing the record definition- Returns:
- a list of AST nodes representing the record components
-
getRecordDef
Finds the nearest ancestor record definition node for the given AST node.- Parameters:
ast- the AST node to start searching from- Returns:
- the nearest
RECORD_DEFAST node, ornullif not found
-
hasShortCircuitTag
Validates whether the Javadoc has a short circuit tag. Currently, this is the inheritTag. Any violations are logged.- Parameters:
ast- the construct being checkedtags- the list of Javadoc tags associated with the construct- Returns:
- true if the construct has a short circuit tag.
-
getParameters
Computes the parameter nodes for a method.- Parameters:
ast- the method node.- Returns:
- the list of parameter nodes for ast.
-
getThrows
Computes the exception nodes for a method.- Parameters:
ast- the method node.- Returns:
- the list of exception nodes for ast.
-
getThrowed
Get ExceptionInfo for all exceptions that throws in method code by 'throw new'.- Parameters:
methodAst- method DetailAST object where to find exceptions- Returns:
- list of ExceptionInfo
-
getExceptionInfo
Get ExceptionInfo instance.- Parameters:
ast- DetailAST object where to find exceptions node;- Returns:
- ExceptionInfo
-
getFirstClassNameNode
Get node where class name of exception starts.- Parameters:
ast- DetailAST object where to find exceptions node;- Returns:
- exception node where class name starts
-
isInIgnoreBlock
Checks if a 'throw' usage is contained within a block that should be ignored. Such blocks consist of try (with catch) blocks, local classes, anonymous classes, and lambda expressions. Note that a try block without catch is not considered.- Parameters:
methodBodyAst- DetailAST node representing the method bodythrowAst- DetailAST node representing the 'throw' literal- Returns:
- true if throwAst is inside a block that should be ignored
-
combineExceptionInfo
private static List<JavadocMethodCheck.ExceptionInfo> combineExceptionInfo(Collection<JavadocMethodCheck.ExceptionInfo> first, Iterable<JavadocMethodCheck.ExceptionInfo> second) Combine ExceptionInfo collections together by matching names.- Parameters:
first- the first collection of ExceptionInfosecond- the second collection of ExceptionInfo- Returns:
- combined list of ExceptionInfo
-
findTokensInAstByType
Finds node of specified type among root children, siblings, siblings children on any deep level. -
checkRecordParamTags
private void checkRecordParamTags(List<JavadocTag> tags, DetailAST compactDef, boolean reportExpectedTags) Checks if all record components in a compact constructor have corresponding@paramtags. Reports missing or extra@paramtags in the Javadoc.- Parameters:
tags- the list of Javadoc tagscompactDef- the compact constructor AST nodereportExpectedTags- whether to report missing@paramtags
-
checkParamTags
Checks a set of tags for matching parameters.- Parameters:
tags- the tags to checkparent- the node which takes the parametersreportExpectedTags- whether we should report if do not find expected tag
-
searchMatchingTypeParameter
private static boolean searchMatchingTypeParameter(Iterable<DetailAST> typeParams, String requiredTypeName) Returns true if required type found in type parameters.- Parameters:
typeParams- collection of type parametersrequiredTypeName- name of required type- Returns:
- true if required type found in type parameters.
-
removeMatchingParam
Remove parameter from params collection by name.- Parameters:
params- collection of DetailAST parametersparamName- name of parameter- Returns:
- true if parameter found and removed
-
checkReturnTag
Checks for only one return tag. All return tags will be removed from the supplied list.- Parameters:
tags- the tags to checklineNo- the line number of the expected tagreportExpectedTags- whether we should report if do not find expected tag
-
checkThrowsTags
private void checkThrowsTags(List<JavadocTag> tags, List<JavadocMethodCheck.ExceptionInfo> throwsList, boolean reportExpectedTags) Checks a set of tags for matching throws.- Parameters:
tags- the tags to checkthrowsList- the throws to checkreportExpectedTags- whether we should report if do not find expected tag
-
processThrows
private static void processThrows(Iterable<JavadocMethodCheck.ExceptionInfo> throwsIterable, String documentedClassName) Verifies that documented exception is in throws.- Parameters:
throwsIterable- collection of throwsdocumentedClassName- documented exception class name
-
isExceptionInfoSame
private static boolean isExceptionInfoSame(JavadocMethodCheck.ExceptionInfo info1, JavadocMethodCheck.ExceptionInfo info2) Check that ExceptionInfo objects are same by name.- Parameters:
info1- ExceptionInfo objectinfo2- ExceptionInfo object- Returns:
- true is ExceptionInfo object have the same name
-
isClassNamesSame
Check that class names are same by short name of class. If some class name is fully qualified it is cut to short name.- Parameters:
class1- class nameclass2- class name- Returns:
- true is ExceptionInfo object have the same name
-