Class IndentationCheck
- 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.indentation.IndentationCheck
-
- All Implemented Interfaces:
Configurable
,Contextualizable
public class IndentationCheck extends AbstractCheck
Checks correct indentation of Java code.The idea behind this is that while pretty printers are sometimes convenient for bulk reformats of legacy code, they often either aren't configurable enough or just can't anticipate how format should be done. Sometimes this is personal preference, other times it is practical experience. In any case, this check should just ensure that a minimal set of indentation rules is followed.
Basic offset indentation is used for indentation inside code blocks. For any lines that span more than 1, line wrapping indentation is used for those lines after the first. Brace adjustment, case, and throws indentations are all used only if those specific identifiers start the line. If, for example, a brace is used in the middle of the line, its indentation will not take effect. All indentations have an accumulative/recursive effect when they are triggered. If during a line wrapping, another code block is found and it doesn't end on that same line, then the subsequent lines afterwards, in that new code block, are increased on top of the line wrap and any indentations above it.
Example:
if ((condition1 && condition2) || (condition3 && condition4) // line wrap with bigger indentation ||!(condition5 && condition6)) { // line wrap with bigger indentation field.doSomething() // basic offset .doSomething() // line wrap .doSomething( c -> { // line wrap return c.doSome(); // basic offset }); }
-
Property
arrayInitIndent
- Specify how far an array initialization should be indented when on next line. Type isint
. Default value is4
. -
Property
basicOffset
- Specify how far new indentation level should be indented when on the next line. Type isint
. Default value is4
. -
Property
braceAdjustment
- Specify how far a braces should be indented when on the next line. Type isint
. Default value is0
. -
Property
caseIndent
- Specify how far a case label should be indented when on next line. Type isint
. Default value is4
. -
Property
forceStrictCondition
- Force strict indent level in line wrapping case. If value is true, line wrap indent have to be same as lineWrappingIndentation parameter. If value is false, line wrap indent could be bigger on any value user would like. Type isboolean
. Default value isfalse
. -
Property
lineWrappingIndentation
- Specify how far continuation line should be indented when line-wrapping is present. Type isint
. Default value is4
. -
Property
throwsIndent
- Specify how far a throws clause should be indented when on next line. Type isint
. Default value is4
.
Parent is
com.puppycrawl.tools.checkstyle.TreeWalker
Violation Message Keys:
-
indentation.child.error
-
indentation.child.error.multi
-
indentation.error
-
indentation.error.multi
- Since:
- 3.1
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
AbstractAutomaticBean.OutputStreamOptions
-
-
Field Summary
Fields Modifier and Type Field Description private int
arrayInitIndent
Specify how far an array initialization should be indented when on next line.private int
basicOffset
Specify how far new indentation level should be indented when on the next line.private int
braceAdjustment
Specify how far a braces should be indented when on the next line.private int
caseIndent
Specify how far a case label should be indented when on next line.private static int
DEFAULT_INDENTATION
Default indentation amount - based on Sun.private boolean
forceStrictCondition
Force strict indent level in line wrapping case.private HandlerFactory
handlerFactory
Factory from which handlers are distributed.private Deque<AbstractExpressionHandler>
handlers
Handlers currently in use.private Set<Integer>
incorrectIndentationLines
Lines logged as having incorrect indentation.private LineWrappingHandler
lineWrappingHandler
Instance of line wrapping handler to use.private int
lineWrappingIndentation
Specify how far continuation line should be indented when line-wrapping is present.static String
MSG_CHILD_ERROR
A key is pointing to the warning message text in "messages.properties" file.static String
MSG_CHILD_ERROR_MULTI
A key is pointing to the warning message text in "messages.properties" file.static String
MSG_ERROR
A key is pointing to the warning message text in "messages.properties" file.static String
MSG_ERROR_MULTI
A key is pointing to the warning message text in "messages.properties" file.private int
throwsIndent
Specify how far a throws clause should be indented when on next line.
-
Constructor Summary
Constructors Constructor Description IndentationCheck()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
beginTree(DetailAST ast)
Called before the starting to process a tree.int[]
getAcceptableTokens()
The configurable token set.int
getArrayInitIndent()
Getter to query how far an array initialization should be indented when on next line.int
getBasicOffset()
Getter to query how far new indentation level should be indented when on the next line.int
getBraceAdjustment()
Getter to query how far a braces should be indented when on the next line.int
getCaseIndent()
Getter to query how far a case label should be indented when on next line.int[]
getDefaultTokens()
Returns the default token a check is interested in.HandlerFactory
getHandlerFactory()
Accessor for the handler factory.int
getIndentationTabWidth()
Get the width of a tab.LineWrappingHandler
getLineWrappingHandler()
Accessor for the line wrapping handler.int
getLineWrappingIndentation()
Getter to query how far continuation line should be indented when line-wrapping is present.int[]
getRequiredTokens()
The tokens that this check must be registered for.int
getThrowsIndent()
Getter to query how far a throws clause should be indented when on next line.void
indentationLog(DetailAST ast, String key, Object... args)
Log a violation message.boolean
isForceStrictCondition()
Getter to query strict indent level in line wrapping case.void
leaveToken(DetailAST ast)
Called after all the child nodes have been process.void
setArrayInitIndent(int arrayInitIndent)
Setter to specify how far an array initialization should be indented when on next line.void
setBasicOffset(int basicOffset)
Setter to specify how far new indentation level should be indented when on the next line.void
setBraceAdjustment(int adjustmentAmount)
Setter to specify how far a braces should be indented when on the next line.void
setCaseIndent(int amount)
Setter to specify how far a case label should be indented when on next line.void
setForceStrictCondition(boolean value)
Setter to force strict indent level in line wrapping case.void
setLineWrappingIndentation(int lineWrappingIndentation)
Setter to specify how far continuation line should be indented when line-wrapping is present.void
setThrowsIndent(int throwsIndent)
Setter to specify how far a throws clause should be indented when on next line.void
visitToken(DetailAST ast)
Called to process a token.-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck
clearViolations, destroy, finishTree, getFileContents, getFilePath, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, init, isCommentNodesRequired, log, log, log, setFileContents, setTabWidth, setTokens
-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverity
-
Methods inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
configure, contextualize, getConfiguration, setupChild
-
-
-
-
Field Detail
-
MSG_ERROR
public static final String MSG_ERROR
A key is pointing to the warning message text in "messages.properties" file.- See Also:
- Constant Field Values
-
MSG_ERROR_MULTI
public static final String MSG_ERROR_MULTI
A key is pointing to the warning message text in "messages.properties" file.- See Also:
- Constant Field Values
-
MSG_CHILD_ERROR
public static final String MSG_CHILD_ERROR
A key is pointing to the warning message text in "messages.properties" file.- See Also:
- Constant Field Values
-
MSG_CHILD_ERROR_MULTI
public static final String MSG_CHILD_ERROR_MULTI
A key is pointing to the warning message text in "messages.properties" file.- See Also:
- Constant Field Values
-
DEFAULT_INDENTATION
private static final int DEFAULT_INDENTATION
Default indentation amount - based on Sun.- See Also:
- Constant Field Values
-
handlers
private final Deque<AbstractExpressionHandler> handlers
Handlers currently in use.
-
lineWrappingHandler
private final LineWrappingHandler lineWrappingHandler
Instance of line wrapping handler to use.
-
handlerFactory
private final HandlerFactory handlerFactory
Factory from which handlers are distributed.
-
incorrectIndentationLines
private Set<Integer> incorrectIndentationLines
Lines logged as having incorrect indentation.
-
basicOffset
private int basicOffset
Specify how far new indentation level should be indented when on the next line.
-
caseIndent
private int caseIndent
Specify how far a case label should be indented when on next line.
-
braceAdjustment
private int braceAdjustment
Specify how far a braces should be indented when on the next line.
-
throwsIndent
private int throwsIndent
Specify how far a throws clause should be indented when on next line.
-
arrayInitIndent
private int arrayInitIndent
Specify how far an array initialization should be indented when on next line.
-
lineWrappingIndentation
private int lineWrappingIndentation
Specify how far continuation line should be indented when line-wrapping is present.
-
forceStrictCondition
private boolean forceStrictCondition
Force strict indent level in line wrapping case. If value is true, line wrap indent have to be same as lineWrappingIndentation parameter. If value is false, line wrap indent could be bigger on any value user would like.
-
-
Constructor Detail
-
IndentationCheck
public IndentationCheck()
-
-
Method Detail
-
isForceStrictCondition
public boolean isForceStrictCondition()
Getter to query strict indent level in line wrapping case. If value is true, line wrap indent have to be same as lineWrappingIndentation parameter. If value is false, line wrap indent could be bigger on any value user would like.- Returns:
- forceStrictCondition value.
-
setForceStrictCondition
public void setForceStrictCondition(boolean value)
Setter to force strict indent level in line wrapping case. If value is true, line wrap indent have to be same as lineWrappingIndentation parameter. If value is false, line wrap indent could be bigger on any value user would like.- Parameters:
value
- user's value of forceStrictCondition.- Since:
- 6.3
-
setBasicOffset
public void setBasicOffset(int basicOffset)
Setter to specify how far new indentation level should be indented when on the next line.- Parameters:
basicOffset
- the number of tabs or spaces to indent- Since:
- 3.1
-
getBasicOffset
public int getBasicOffset()
Getter to query how far new indentation level should be indented when on the next line.- Returns:
- the number of tabs or spaces to indent
-
setBraceAdjustment
public void setBraceAdjustment(int adjustmentAmount)
Setter to specify how far a braces should be indented when on the next line.- Parameters:
adjustmentAmount
- the brace offset- Since:
- 3.1
-
getBraceAdjustment
public int getBraceAdjustment()
Getter to query how far a braces should be indented when on the next line.- Returns:
- the positive offset to adjust braces
-
setCaseIndent
public void setCaseIndent(int amount)
Setter to specify how far a case label should be indented when on next line.- Parameters:
amount
- the case indentation level- Since:
- 3.1
-
getCaseIndent
public int getCaseIndent()
Getter to query how far a case label should be indented when on next line.- Returns:
- the case indentation level
-
setThrowsIndent
public void setThrowsIndent(int throwsIndent)
Setter to specify how far a throws clause should be indented when on next line.- Parameters:
throwsIndent
- the throws indentation level- Since:
- 5.7
-
getThrowsIndent
public int getThrowsIndent()
Getter to query how far a throws clause should be indented when on next line.- Returns:
- the throws indentation level
-
setArrayInitIndent
public void setArrayInitIndent(int arrayInitIndent)
Setter to specify how far an array initialization should be indented when on next line.- Parameters:
arrayInitIndent
- the array initialization indentation level- Since:
- 5.8
-
getArrayInitIndent
public int getArrayInitIndent()
Getter to query how far an array initialization should be indented when on next line.- Returns:
- the initialization indentation level
-
getLineWrappingIndentation
public int getLineWrappingIndentation()
Getter to query how far continuation line should be indented when line-wrapping is present.- Returns:
- the line-wrapping indentation level
-
setLineWrappingIndentation
public void setLineWrappingIndentation(int lineWrappingIndentation)
Setter to specify how far continuation line should be indented when line-wrapping is present.- Parameters:
lineWrappingIndentation
- the line-wrapping indentation level- Since:
- 5.9
-
indentationLog
public void indentationLog(DetailAST ast, String key, Object... args)
Log a violation message.- Parameters:
ast
- the ast for which error to be loggedkey
- the message that describes the violationargs
- the details of the message- See Also:
MessageFormat
-
getIndentationTabWidth
public int getIndentationTabWidth()
Get the width of a tab.- Returns:
- the width of a tab
-
getDefaultTokens
public int[] getDefaultTokens()
Description copied from class:AbstractCheck
Returns the default token a check is interested in. Only used if the configuration for a check does not define the tokens.- Specified by:
getDefaultTokens
in classAbstractCheck
- Returns:
- the default tokens
- See Also:
TokenTypes
-
getAcceptableTokens
public int[] getAcceptableTokens()
Description copied from class:AbstractCheck
The 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:
getAcceptableTokens
in classAbstractCheck
- Returns:
- the token set this check is designed for.
- See Also:
TokenTypes
-
getRequiredTokens
public int[] getRequiredTokens()
Description copied from class:AbstractCheck
The tokens that this check must be registered for.- Specified by:
getRequiredTokens
in classAbstractCheck
- Returns:
- the token set this must be registered for.
- See Also:
TokenTypes
-
beginTree
public void beginTree(DetailAST ast)
Description copied from class:AbstractCheck
Called before the starting to process a tree. Ideal place to initialize information that is to be collected whilst processing a tree.- Overrides:
beginTree
in classAbstractCheck
- Parameters:
ast
- the root of the tree
-
visitToken
public void visitToken(DetailAST ast)
Description copied from class:AbstractCheck
Called to process a token.- Overrides:
visitToken
in classAbstractCheck
- Parameters:
ast
- the token to process
-
leaveToken
public void leaveToken(DetailAST ast)
Description copied from class:AbstractCheck
Called after all the child nodes have been process.- Overrides:
leaveToken
in classAbstractCheck
- Parameters:
ast
- the token leaving
-
getLineWrappingHandler
public LineWrappingHandler getLineWrappingHandler()
Accessor for the line wrapping handler.- Returns:
- the line wrapping handler
-
getHandlerFactory
public final HandlerFactory getHandlerFactory()
Accessor for the handler factory.- Returns:
- the handler factory
-
-