Class JavadocUtil
java.lang.Object
com.puppycrawl.tools.checkstyle.utils.JavadocUtil
Contains utility methods for working with Javadoc.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enum
The type of Javadoc tag we want returned. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final Pattern
Newline pattern.private static final Pattern
Return pattern.private static final Pattern
Tab pattern.Maps from a token name to value.Maps from a token value to name.private static final String
Exception message for unknown JavaDoc token id. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic String
escapeAllControlChars
(String text) Replace all control chars with escaped symbols.static DetailNode
findFirstToken
(DetailNode detailNode, int type) Returns the first child token that has a specified type.static List<DetailNode>
getAllNodesOfType
(DetailNode detailNode, int type) Returns all child tokens that have a specified type.static String
getBlockCommentContent
(DetailAST blockCommentBegin) Gets content of block comment.static String
getJavadocCommentContent
(DetailAST javadocCommentBegin) Get content of Javadoc comment.static JavadocTags
getJavadocTags
(TextBlock textBlock, JavadocUtil.JavadocTagType tagType) Gets validTags from a given piece of Javadoc.static DetailNode
getNextSibling
(DetailNode node, int tokenType) Gets next sibling of specified node with the specified type.static String
getTagName
(DetailNode javadocTagSection) Extracts the tag name from the given Javadoc tag section.static int
getTokenId
(String name) Returns the ID of a token for a given name.static String
getTokenName
(int id) Returns the name of a token for a given ID.static boolean
isCorrectJavadocPosition
(DetailAST blockComment) Checks Javadoc comment it's in right place.static boolean
isJavadocComment
(DetailAST blockCommentBegin) Checks block comment content starts with '*' javadoc comment identifier.static boolean
isJavadocComment
(String commentContent) Checks that commentContent starts with '*' javadoc comment identifier.static boolean
isTag
(DetailNode ast, String expectedTagName) Checks whether the given AST node is an HTML element with the specified tag name.
-
Field Details
-
TOKEN_NAME_TO_VALUE
Maps from a token name to value. -
TOKEN_VALUE_TO_NAME
Maps from a token value to name. -
UNKNOWN_JAVADOC_TOKEN_ID_EXCEPTION_MESSAGE
Exception message for unknown JavaDoc token id.- See Also:
-
NEWLINE
Newline pattern. -
RETURN
Return pattern. -
TAB
Tab pattern.
-
-
Constructor Details
-
JavadocUtil
private JavadocUtil()Prevent instantiation.
-
-
Method Details
-
getJavadocTags
Gets validTags from a given piece of Javadoc.- Parameters:
textBlock
- the Javadoc comment to process.tagType
- the type of validTags we're interested in- Returns:
- all standalone validTags from the given javadoc.
-
isJavadocComment
Checks that commentContent starts with '*' javadoc comment identifier.- Parameters:
commentContent
- content of block comment- Returns:
- true if commentContent starts with '*' javadoc comment identifier.
-
isJavadocComment
Checks block comment content starts with '*' javadoc comment identifier.- Parameters:
blockCommentBegin
- block comment AST- Returns:
- true if block comment content starts with '*' javadoc comment identifier.
-
getBlockCommentContent
Gets content of block comment.- Parameters:
blockCommentBegin
- block comment AST.- Returns:
- content of block comment.
-
getJavadocCommentContent
Get content of Javadoc comment.- Parameters:
javadocCommentBegin
- Javadoc comment AST- Returns:
- content of Javadoc comment.
-
findFirstToken
Returns the first child token that has a specified type.- Parameters:
detailNode
- Javadoc AST nodetype
- the token type to match- Returns:
- the matching token, or null if no match
-
getAllNodesOfType
Returns all child tokens that have a specified type.- Parameters:
detailNode
- Javadoc AST nodetype
- the token type to match- Returns:
- the matching tokens, or an empty list if no match
-
isTag
Checks whether the given AST node is an HTML element with the specified tag name. This method ignore void elements.- Parameters:
ast
- the AST node to check (must be of typeJavadocCommentsTokenTypes.HTML_ELEMENT
)expectedTagName
- the tag name to match (case-insensitive)- Returns:
true
if the node has the given tag name,false
otherwise
-
getNextSibling
Gets next sibling of specified node with the specified type.- Parameters:
node
- DetailNodetokenType
- javadoc token type- Returns:
- next sibling.
-
getTokenName
Returns the name of a token for a given ID.- Parameters:
id
- the ID of the token name to get- Returns:
- a token name
- Throws:
IllegalArgumentException
- if an unknown token ID was specified.
-
getTokenId
Returns the ID of a token for a given name.- Parameters:
name
- the name of the token ID to get- Returns:
- a token ID
- Throws:
IllegalArgumentException
- if an unknown token name was specified.
-
getTagName
Extracts the tag name from the given Javadoc tag section.- Parameters:
javadocTagSection
- the node representing a Javadoc tag section. This node must be of typeJavadocCommentsTokenTypes.JAVADOC_BLOCK_TAG
orJavadocCommentsTokenTypes.JAVADOC_INLINE_TAG
.- Returns:
- the tag name (e.g., "param", "return", "link")
-
escapeAllControlChars
Replace all control chars with escaped symbols.- Parameters:
text
- the String to process.- Returns:
- the processed String with all control chars escaped.
-
isCorrectJavadocPosition
Checks Javadoc comment it's in right place.From Javadoc util documentation: "Placement of comments - Documentation comments are recognized only when placed immediately before class, interface, constructor, method, field or annotation field declarations -- see the class example, method example, and field example. Documentation comments placed in the body of a method are ignored."
If there are many documentation comments per declaration statement, only the last one will be recognized.
- Parameters:
blockComment
- Block comment AST- Returns:
- true if Javadoc is in right place
- See Also:
-