Class GoogleRightCurlyCheck
- All Implemented Interfaces:
Configurable,Contextualizable
'}') for code blocks, following
the
Google Java Style Guide .
For nonempty blocks the right curly brace must begin its own line,
unless it is followed by else, catch, finally, or a comma,
in which case no line break follows it.
For empty blocks, either K&R style or the concise {} form is
allowed, except within a multi-block statement (if/else, try/catch/finally).
- Since:
- 14.2.0
-
Nested Class Summary
Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
AbstractAutomaticBean.OutputStreamOptions -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic 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. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate voidcheckMultiBlockStatement(DetailAST currentBlock, DetailAST brace, DetailAST nextBlock) Checks that the right curly brace of a multi-block statement (e.g.private voidcheckRightBrace(DetailAST currentBlock, DetailAST brace) Checks that a right curly brace is placed correctly.private static booleancontentAround(DetailAST brace, DetailAST nextToken) Checks if the right curly has content around.int[]The configurable token set.int[]Returns the default token a check is interested in.private static DetailASTgetNextToken(DetailAST node) Traverses up the AST to find the next sibling token after the right curly brace.int[]The tokens that this check must be registered for.private voidChecks the right curly brace placement forcaseanddefaultblocks, covering both old stylecase X:and new stylecase X ->switch syntax.private static booleanhasContentOnLeftSide(DetailAST brace) Checks if the block is not concise and has content on left side of right brace.private static booleanhasContentOnRightSide(DetailAST brace, DetailAST nextToken) Checks if the right curly brace is part multi-block statement or no content on right side of right brace.booleanWhether comment nodes are required or not.private static booleanChecks if block is empty.private static booleanisNotConcise(DetailAST brace) Checks if the block is notK&Rstyle or concise{}.private static booleanisPartOfMultiBlock(DetailAST currentBlock, DetailAST nextBlock) Checks whether the current block is part of a multi-block statement (if/else,try/catch/finally, ordo/while).private voidlogViolations(String message, DetailAST brace) Logs violation message for given brace token.private voidverifyEmptyBlock(DetailAST brace, DetailAST nextToken) Checks empty block which should be concise and alone.voidvisitToken(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, 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_LINE_ALONE
A key is pointing to the warning message text in "messages.properties" file.- See Also:
-
MSG_KEY_LINE_BREAK_AFTER
A key is pointing to the warning message text in "messages.properties" file.- See Also:
-
MSG_KEY_LINE_BREAK_BEFORE
A key is pointing to the warning message text in "messages.properties" file.- See Also:
-
MSG_KEY_CONCISE_BLOCK
A key is pointing to the warning message text in "messages.properties" file.- See Also:
-
MSG_KEY_LINE_SAME
A key is pointing to the warning message text in "messages.properties" file.- See Also:
-
-
Constructor Details
-
GoogleRightCurlyCheck
public GoogleRightCurlyCheck()Creates a newGoogleRightCurlyCheckinstance.
-
-
Method Details
-
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.- Specified by:
getDefaultTokensin classAbstractCheck- 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.- Specified by:
getAcceptableTokensin classAbstractCheck- Returns:
- the token set this check is designed for.
- See Also:
-
getRequiredTokens
Description copied from class:AbstractCheckThe tokens that this check must be registered for.- Specified by:
getRequiredTokensin classAbstractCheck- Returns:
- the token set this must be registered for.
- See Also:
-
isCommentNodesRequired
Description copied from class:AbstractCheckWhether comment nodes are required or not.- Overrides:
isCommentNodesRequiredin classAbstractCheck- Returns:
- false as a default value.
-
visitToken
Description copied from class:AbstractCheckCalled to process a token.- Overrides:
visitTokenin classAbstractCheck- Parameters:
ast- the token to process
-
handleCaseAndDefault
Checks the right curly brace placement forcaseanddefaultblocks, covering both old stylecase X:and new stylecase X ->switch syntax.For old-style syntax, a case label may be followed by multiple
{}blocks in sequence, and each such block's right curly brace is checked. For new-style syntax, the block following the arrow (e.g.case X -> { ... }) is checked, expression with no block (e.g.case X -> expr;) is skipped.- Parameters:
ast- thecaseordefaulttoken
-
logViolations
Logs violation message for given brace token.- Parameters:
message- the violation message key.brace- the right curly brace.
-
checkRightBrace
Checks that a right curly brace is placed correctly.If the block is part of a multi-block statement (e.g.
if/else,try/catch/finally, ordo/while), the closing brace must be on the same line as the next block's starting keyword. Otherwise, the brace must be alone on its own line, unless the block is empty, in which case the concise{}form is allowed.- Parameters:
currentBlock- the block whose right curly brace is being checkedbrace- the right curly brace token
-
checkMultiBlockStatement
Checks that the right curly brace of a multi-block statement (e.g.if/else,try/catch/finally,do/while) is placed correctly relative to the next block.- Parameters:
currentBlock- the current blockbrace- the right curly bracenextBlock- the next block in multi-block statement
-
verifyEmptyBlock
Checks empty block which should be concise and alone.- Parameters:
brace- the right curly token.nextToken- the token after right curly brace.
-
contentAround
Checks if the right curly has content around.- Parameters:
brace- the right curly bracenextToken- the next token after right curly- Returns:
trueif right curly has content on its left or right.
-
isPartOfMultiBlock
Checks whether the current block is part of a multi-block statement (if/else,try/catch/finally, ordo/while).- Parameters:
currentBlock- the current blocknextBlock- the block followingast- Returns:
trueifastandnextBlockbelong to the same multi-block statement
-
hasContentOnLeftSide
Checks if the block is not concise and has content on left side of right brace.- Parameters:
brace- the right curly brace token- Returns:
trueif the brace has content on left.
-
hasContentOnRightSide
Checks if the right curly brace is part multi-block statement or no content on right side of right brace.- Parameters:
brace- the right curly brace tokennextToken- the next token of right curly.- Returns:
trueif the brace is on the same line as the previous sibling or parent if no sibling exists
-
isEmpty
Checks if block is empty.- Parameters:
brace- the right curly brace.- Returns:
trueif the block is empty.
-
isNotConcise
Checks if the block is notK&Rstyle or concise{}.- Parameters:
brace- right curly token- Returns:
trueif block is not concise.
-
getNextToken
Traverses up the AST to find the next sibling token after the right curly brace.- Parameters:
node- ast token- Returns:
- the next sibling token, or
nullif none exists
-