Class IndentationCheck
- java.lang.Object
-
- com.puppycrawl.tools.checkstyle.api.AutomaticBean
-
- 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 basic idea behind this is that while pretty printers are sometimes convenient for reformatting 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 are followed.
Implementation -- Basically, this check requests visitation for all handled token types (those tokens registered in the HandlerFactory). When visitToken is called, a new ExpressionHandler is created for the AST and pushed onto the handlers stack. The new handler then checks the indentation for the currently visiting AST. When leaveToken is called, the ExpressionHandler is popped from the stack.
While on the stack the ExpressionHandler can be queried for the indentation level it suggests for children as well as for other values.
While an ExpressionHandler checks the indentation level of its own AST, it typically also checks surrounding ASTs. For instance, a while loop handler checks the while loop as well as the braces and immediate children.
- handler class -to-> ID mapping kept in Map - parent passed in during construction - suggest child indent level - allows for some tokens to be on same line (ie inner classes OBJBLOCK) and not increase indentation level - looked at using double dispatch for getSuggestedChildIndent(), but it doesn't seem worthwhile, at least now - both tabs and spaces are considered whitespace in front of the line... tabs are converted to spaces - block parents with parens -- for, while, if, etc... -- are checked that they match the level of the parent
-
-
Field Summary
Fields Modifier and Type Field Description private int
arrayInitIndent
How much to indent an array initialization when on next line.private int
basicOffset
How many tabs or spaces to use.private int
braceAdjustment
How far brace should be indented when on next line.private int
caseIndent
How much to indent a case label.private static int
DEFAULT_INDENTATION
Default indentation amount - based on Sun.private boolean
forceStrictCondition
Force strict condition in line wrapping case.private HandlerFactory
handlerFactory
Factory from which handlers are distributed.private java.util.Deque<AbstractExpressionHandler>
handlers
Handlers currently in use.private java.util.Set<java.lang.Integer>
incorrectIndentationLines
Lines logged as having incorrect indentation.private LineWrappingHandler
lineWrappingHandler
Instance of line wrapping handler to use.private int
lineWrappingIndentation
How far continuation line should be indented when line-wrapping is present.static java.lang.String
MSG_CHILD_ERROR
A key is pointing to the warning message text in "messages.properties" file.static java.lang.String
MSG_CHILD_ERROR_MULTI
A key is pointing to the warning message text in "messages.properties" file.static java.lang.String
MSG_ERROR
A key is pointing to the warning message text in "messages.properties" file.static java.lang.String
MSG_ERROR_MULTI
A key is pointing to the warning message text in "messages.properties" file.private int
throwsIndent
How far throws 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()
Get the line-wrapping indentation level.int
getBasicOffset()
Get the basic offset.int
getBraceAdjustment()
Get the brace adjustment amount.int
getCaseIndent()
Get the case indentation level.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()
Get the array line-wrapping indentation level.int[]
getRequiredTokens()
The tokens that this check must be registered for.int
getThrowsIndent()
Get the throws indentation level.void
indentationLog(int line, java.lang.String key, java.lang.Object... args)
Log an error message.boolean
isForceStrictCondition()
Get forcing strict condition.void
leaveToken(DetailAST ast)
Called after all the child nodes have been process.void
setArrayInitIndent(int arrayInitIndent)
Set the array initialisation indentation level.void
setBasicOffset(int basicOffset)
Set the basic offset.void
setBraceAdjustment(int adjustmentAmount)
Adjusts brace indentation (positive offset).void
setCaseIndent(int amount)
Set the case indentation level.void
setForceStrictCondition(boolean value)
Set forcing strict condition.void
setLineWrappingIndentation(int lineWrappingIndentation)
Set the line-wrapping indentation level.void
setThrowsIndent(int throwsIndent)
Set the throws indentation level.void
visitToken(DetailAST ast)
Called to process a token.-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck
destroy, finishTree, getClassLoader, getFileContents, getLine, getLines, getTabWidth, getTokenNames, init, isCommentNodesRequired, log, log, setClassLoader, setFileContents, setMessages, setTabWidth, setTokens
-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, log, setId, setSeverity
-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean
configure, contextualize, finishLocalSetup, getConfiguration, setupChild
-
-
-
-
Field Detail
-
MSG_ERROR
public static final java.lang.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 java.lang.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 java.lang.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 java.lang.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 java.util.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 java.util.Set<java.lang.Integer> incorrectIndentationLines
Lines logged as having incorrect indentation.
-
basicOffset
private int basicOffset
How many tabs or spaces to use.
-
caseIndent
private int caseIndent
How much to indent a case label.
-
braceAdjustment
private int braceAdjustment
How far brace should be indented when on next line.
-
throwsIndent
private int throwsIndent
How far throws should be indented when on next line.
-
arrayInitIndent
private int arrayInitIndent
How much to indent an array initialization when on next line.
-
lineWrappingIndentation
private int lineWrappingIndentation
How far continuation line should be indented when line-wrapping is present.
-
forceStrictCondition
private boolean forceStrictCondition
Force strict condition 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 have to be not less than lineWrappingIndentation parameter.
-
-
Method Detail
-
isForceStrictCondition
public boolean isForceStrictCondition()
Get forcing strict condition.- Returns:
- forceStrictCondition value.
-
setForceStrictCondition
public void setForceStrictCondition(boolean value)
Set forcing strict condition.- Parameters:
value
- user's value of forceStrictCondition.
-
setBasicOffset
public void setBasicOffset(int basicOffset)
Set the basic offset.- Parameters:
basicOffset
- the number of tabs or spaces to indent
-
getBasicOffset
public int getBasicOffset()
Get the basic offset.- Returns:
- the number of tabs or spaces to indent
-
setBraceAdjustment
public void setBraceAdjustment(int adjustmentAmount)
Adjusts brace indentation (positive offset).- Parameters:
adjustmentAmount
- the brace offset
-
getBraceAdjustment
public int getBraceAdjustment()
Get the brace adjustment amount.- Returns:
- the positive offset to adjust braces
-
setCaseIndent
public void setCaseIndent(int amount)
Set the case indentation level.- Parameters:
amount
- the case indentation level
-
getCaseIndent
public int getCaseIndent()
Get the case indentation level.- Returns:
- the case indentation level
-
setThrowsIndent
public void setThrowsIndent(int throwsIndent)
Set the throws indentation level.- Parameters:
throwsIndent
- the throws indentation level
-
getThrowsIndent
public int getThrowsIndent()
Get the throws indentation level.- Returns:
- the throws indentation level
-
setArrayInitIndent
public void setArrayInitIndent(int arrayInitIndent)
Set the array initialisation indentation level.- Parameters:
arrayInitIndent
- the array initialisation indentation level
-
getArrayInitIndent
public int getArrayInitIndent()
Get the line-wrapping indentation level.- Returns:
- the initialisation indentation level
-
getLineWrappingIndentation
public int getLineWrappingIndentation()
Get the array line-wrapping indentation level.- Returns:
- the line-wrapping indentation level
-
setLineWrappingIndentation
public void setLineWrappingIndentation(int lineWrappingIndentation)
Set the line-wrapping indentation level.- Parameters:
lineWrappingIndentation
- the line-wrapping indentation level
-
indentationLog
public void indentationLog(int line, java.lang.String key, java.lang.Object... args)
Log an error message.- Parameters:
line
- the line number where the error was foundkey
- the message that describes the errorargs
- 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
-
-