Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

com.puppycrawl.tools.checkstyle.checks.indentation
Class IndentationCheck  view IndentationCheck download IndentationCheck.java

java.lang.Object
  extended bycom.puppycrawl.tools.checkstyle.api.AutomaticBean
      extended bycom.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
          extended bycom.puppycrawl.tools.checkstyle.api.Check
              extended bycom.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck
All Implemented Interfaces:
com.puppycrawl.tools.checkstyle.api.Configurable, com.puppycrawl.tools.checkstyle.api.Contextualizable

public class IndentationCheck
extends com.puppycrawl.tools.checkstyle.api.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.

Created on November 2, 2002, 10:59 PM

   - 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
 


Field Summary
private static int DEFAULT_INDENTATION
          Default indentation amount - based on Sun
private  int mBasicOffset
          how many tabs or spaces to use
private  int mBraceAdjustment
          how far brace should be indented when on next line
private  int mCaseIndentationAmount
          how much to indent a case label
private  HandlerFactory mHandlerFactory
          factory from which handlers are distributed
private  org.apache.commons.collections.ArrayStack mHandlers
          handlers currently in use
 
Fields inherited from class com.puppycrawl.tools.checkstyle.api.Check
 
Fields inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
 
Fields inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean
 
Constructor Summary
IndentationCheck()
          Creates a new instance of IndentationCheck.
 
Method Summary
 void beginTree(com.puppycrawl.tools.checkstyle.api.DetailAST aAst)
          Called before the starting to process a tree.
 int getBasicOffset()
          Get the basic offset.
 int getBraceAdjustement()
          Get the brace adjustment amount.
 int getCaseIndent()
          Get the case indentation level.
 int[] getDefaultTokens()
          Get the tokens that this check will handle.
(package private)  HandlerFactory getHandlerFactory()
          Accessor for the handler factory.
 int getIndentationTabWidth()
          Get the width of a tab.
 void indentationLog(int aLine, java.lang.String aKey, java.lang.Object[] aArgs)
          Log an error message.
 void leaveToken(com.puppycrawl.tools.checkstyle.api.DetailAST aAST)
          Called after all the child nodes have been process.
 void setBasicOffset(int aBasicOffset)
          Set the basic offset.
 void setBraceAdjustment(int aAdjustmentAmount)
          Adjusts brace indentation (positive offset).
 void setCaseIndent(int aAmount)
          Set the case indentation level.
 void visitToken(com.puppycrawl.tools.checkstyle.api.DetailAST aAST)
          Called to process a token.
 
Methods inherited from class com.puppycrawl.tools.checkstyle.api.Check
destroy, finishTree, getAcceptableTokens, getClassLoader, getFileContents, getLines, getRequiredTokens, getTabWidth, getTokenNames, init, log, log, setClassLoader, setFileContents, setMessages, setTabWidth, setTokens
 
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
getMessageBundle, getSeverity, getSeverityLevel, log, log, log, log, log, log, setSeverity
 
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean
configure, contextualize, finishLocalSetup, getConfiguration, setupChild
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_INDENTATION

private static final int DEFAULT_INDENTATION
Default indentation amount - based on Sun

See Also:
Constant Field Values

mBasicOffset

private int mBasicOffset
how many tabs or spaces to use


mCaseIndentationAmount

private int mCaseIndentationAmount
how much to indent a case label


mBraceAdjustment

private int mBraceAdjustment
how far brace should be indented when on next line


mHandlers

private org.apache.commons.collections.ArrayStack mHandlers
handlers currently in use


mHandlerFactory

private HandlerFactory mHandlerFactory
factory from which handlers are distributed

Constructor Detail

IndentationCheck

public IndentationCheck()
Creates a new instance of IndentationCheck.

Method Detail

setBasicOffset

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


getBasicOffset

public int getBasicOffset()
Get the basic offset.


setBraceAdjustment

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


getBraceAdjustement

public int getBraceAdjustement()
Get the brace adjustment amount.


setCaseIndent

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


getCaseIndent

public int getCaseIndent()
Get the case indentation level.


indentationLog

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


getIndentationTabWidth

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


getDefaultTokens

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


beginTree

public void beginTree(com.puppycrawl.tools.checkstyle.api.DetailAST aAst)
Description copied from class: com.puppycrawl.tools.checkstyle.api.Check
Called before the starting to process a tree. Ideal place to initialise information that is to be collected whilst processing a tree.


visitToken

public void visitToken(com.puppycrawl.tools.checkstyle.api.DetailAST aAST)
Description copied from class: com.puppycrawl.tools.checkstyle.api.Check
Called to process a token.


leaveToken

public void leaveToken(com.puppycrawl.tools.checkstyle.api.DetailAST aAST)
Description copied from class: com.puppycrawl.tools.checkstyle.api.Check
Called after all the child nodes have been process.


getHandlerFactory

final HandlerFactory getHandlerFactory()
Accessor for the handler factory.