com.puppycrawl.tools.checkstyle.checks.indentation

Class IndentationCheck

public class IndentationCheck extends Check

Checks correct indentation of Java Code.

The basic idea behind this is that while pretty printers are sometimes convienent 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 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 mHandlers 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 suggestedChildLevel(), 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
 

Author: jrichard o_sukhodolsky

UNKNOWN: misc

Constructor Summary
IndentationCheck()
Creates a new instance of IndentationCheck.
Method Summary
voidbeginTree(DetailAST aAst)
{@inheritDoc}
intgetBasicOffset()
Get the basic offset.
intgetBraceAdjustement()
Get the brace adjustment amount.
intgetCaseIndent()
Get the case indentation level.
int[]getDefaultTokens()
Get the tokens that this check will handle.
intgetIndentationTabWidth()
Get the width of a tab.
voidindentationLog(int aLine, String aKey, Object[] aArgs)
Log an error message.
voidleaveToken(DetailAST aAST)
{@inheritDoc}
voidsetBasicOffset(int aBasicOffset)
Set the basic offset.
voidsetBraceAdjustment(int aAdjustmentAmount)
Adjusts brace indentation (positive offset).
voidsetCaseIndent(int aAmount)
Set the case indentation level.
voidvisitToken(DetailAST aAST)
{@inheritDoc}

Constructor Detail

IndentationCheck

public IndentationCheck()
Creates a new instance of IndentationCheck.

Method Detail

beginTree

public void beginTree(DetailAST aAst)
{@inheritDoc}

getBasicOffset

public int getBasicOffset()
Get the basic offset.

Returns: the number of tabs or spaces to indent

getBraceAdjustement

public int getBraceAdjustement()
Get the brace adjustment amount.

Returns: the positive offset to adjust braces

getCaseIndent

public int getCaseIndent()
Get the case indentation level.

Returns: the case indentation level

getDefaultTokens

public int[] getDefaultTokens()
Get the tokens that this check will handle.

Returns: the array of tokens that this check handles

getIndentationTabWidth

public int getIndentationTabWidth()
Get the width of a tab.

Returns: the width of a tab

indentationLog

public void indentationLog(int aLine, String aKey, Object[] aArgs)
Log an error message.

Parameters: aLine the line number where the error was found aKey the message that describes the error aArgs the details of the message

See Also: java.text.MessageFormat

leaveToken

public void leaveToken(DetailAST aAST)
{@inheritDoc}

setBasicOffset

public void setBasicOffset(int aBasicOffset)
Set the basic offset.

Parameters: aBasicOffset the number of tabs or spaces to indent

setBraceAdjustment

public void setBraceAdjustment(int aAdjustmentAmount)
Adjusts brace indentation (positive offset).

Parameters: aAdjustmentAmount the brace offset

setCaseIndent

public void setCaseIndent(int aAmount)
Set the case indentation level.

Parameters: aAmount the case indentation level

visitToken

public void visitToken(DetailAST aAST)
{@inheritDoc}