public class LeftCurlyCheck extends AbstractCheck
Checks the placement of left curly braces on types, methods and
other blocks:
LITERAL_CATCH
, LITERAL_DO
, LITERAL_ELSE
, LITERAL_FINALLY
, LITERAL_FOR
, LITERAL_IF
, LITERAL_SWITCH
, LITERAL_SYNCHRONIZED
, LITERAL_TRY
, LITERAL_WHILE
, STATIC_INIT
,
LAMBDA
.
The policy to verify is specified using the LeftCurlyOption
class and
defaults to LeftCurlyOption.EOL
. Policies LeftCurlyOption.EOL
and LeftCurlyOption.NLOW
take into account property maxLineLength.
The default value for maxLineLength is 80.
An example of how to configure the check is:
<module name="LeftCurly"/>
An example of how to configure the check with policy
LeftCurlyOption.NLOW
and maxLineLength 120 is:
<module name="LeftCurly"> <property name="option" value="nlow"/> <property name="maxLineLength" value="120"/> < /module>
An example of how to configure the check to validate enum definitions:
<module name="LeftCurly"> <property name="ignoreEnums" value="false"/> </module>
Modifier and Type | Field and Description |
---|---|
private boolean |
ignoreEnums
If true, Check will ignore enums.
|
static java.lang.String |
MSG_KEY_LINE_BREAK_AFTER
A key is pointing to the warning message text in "messages.properties"
file.
|
static java.lang.String |
MSG_KEY_LINE_NEW
A key is pointing to the warning message text in "messages.properties"
file.
|
static java.lang.String |
MSG_KEY_LINE_PREVIOUS
A key is pointing to the warning message text in "messages.properties"
file.
|
private static java.lang.String |
OPEN_CURLY_BRACE
Open curly brace literal.
|
private LeftCurlyOption |
option
The policy to enforce.
|
Constructor and Description |
---|
LeftCurlyCheck() |
Modifier and Type | Method and Description |
---|---|
private static DetailAST |
findLastAnnotation(DetailAST modifiers)
Find the last token of type
TokenTypes.ANNOTATION
under the given set of modifiers. |
int[] |
getAcceptableTokens()
The configurable token set.
|
int[] |
getDefaultTokens()
Returns the default token a check is interested in.
|
private static DetailAST |
getFirstAnnotationOnSameLine(DetailAST annotation)
Returns first annotation on same line.
|
int[] |
getRequiredTokens()
The tokens that this check must be registered for.
|
private boolean |
hasLineBreakAfter(DetailAST leftCurly)
Checks if left curly has line break after.
|
void |
setIgnoreEnums(boolean ignoreEnums)
Sets whether check should ignore enums when left curly brace policy is EOL.
|
void |
setMaxLineLength(int maxLineLength)
Deprecated.
since 6.10 release, option is not required for the Check.
|
void |
setOption(java.lang.String optionStr)
Set the option to enforce.
|
private static DetailAST |
skipAnnotationOnlyLines(DetailAST ast)
Skip lines that only contain
TokenTypes.ANNOTATION s. |
private void |
validateEol(DetailAST brace,
java.lang.String braceLine)
Validate EOL case.
|
private void |
validateNewLinePosition(DetailAST brace,
DetailAST startToken,
java.lang.String braceLine)
Validate token on new Line position.
|
private void |
verifyBrace(DetailAST brace,
DetailAST startToken)
Verifies that a specified left curly brace is placed correctly
according to policy.
|
void |
visitToken(DetailAST ast)
Called to process a token.
|
beginTree, destroy, finishTree, getClassLoader, getFileContents, getLine, getLines, getTabWidth, getTokenNames, init, isCommentNodesRequired, leaveToken, log, log, setClassLoader, setFileContents, setMessages, setTabWidth, setTokens
getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, log, setId, setSeverity
configure, contextualize, finishLocalSetup, getConfiguration, setupChild
public static final java.lang.String MSG_KEY_LINE_NEW
public static final java.lang.String MSG_KEY_LINE_PREVIOUS
public static final java.lang.String MSG_KEY_LINE_BREAK_AFTER
private static final java.lang.String OPEN_CURLY_BRACE
private boolean ignoreEnums
private LeftCurlyOption option
public void setOption(java.lang.String optionStr)
optionStr
- string to decode option fromjava.lang.IllegalArgumentException
- if unable to decode@Deprecated public void setMaxLineLength(int maxLineLength)
maxLineLength
- the max allowed line lengthpublic void setIgnoreEnums(boolean ignoreEnums)
ignoreEnums
- check's option for ignoring enums.public int[] getDefaultTokens()
AbstractCheck
getDefaultTokens
in class AbstractCheck
TokenTypes
public int[] getAcceptableTokens()
AbstractCheck
getAcceptableTokens
in class AbstractCheck
TokenTypes
public int[] getRequiredTokens()
AbstractCheck
getRequiredTokens
in class AbstractCheck
TokenTypes
public void visitToken(DetailAST ast)
AbstractCheck
visitToken
in class AbstractCheck
ast
- the token to processprivate static DetailAST skipAnnotationOnlyLines(DetailAST ast)
TokenTypes.ANNOTATION
s.
If the received DetailAST
has annotations within its modifiers then first token on the line
of the first token after all annotations is return. This might be
an annotation.
Otherwise, the received DetailAST
is returned.ast
- DetailAST
.DetailAST
.private static DetailAST getFirstAnnotationOnSameLine(DetailAST annotation)
annotation
- last annotation on the lineprivate static DetailAST findLastAnnotation(DetailAST modifiers)
TokenTypes.ANNOTATION
under the given set of modifiers.modifiers
- DetailAST
.DetailAST
or null if there are no annotations.private void verifyBrace(DetailAST brace, DetailAST startToken)
brace
- token for left curly bracestartToken
- token for start of expressionprivate void validateEol(DetailAST brace, java.lang.String braceLine)
brace
- brace ASTbraceLine
- line contentprivate void validateNewLinePosition(DetailAST brace, DetailAST startToken, java.lang.String braceLine)
brace
- brace ASTstartToken
- start TokenbraceLine
- content of line with Braceprivate boolean hasLineBreakAfter(DetailAST leftCurly)
leftCurly
- Left curly token.