Class 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 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
    • Constructor Detail

      • IndentationCheck

        public IndentationCheck()
    • 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,
                                   String key,
                                   Object... args)
        Log an error message.
        Parameters:
        line - the line number where the error was found
        key - the message that describes the error
        args - 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 class AbstractCheck
        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 class AbstractCheck
        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 class AbstractCheck
        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 class AbstractCheck
        Parameters:
        ast - the root of the tree
      • leaveToken

        public void leaveToken​(DetailAST ast)
        Description copied from class: AbstractCheck
        Called after all the child nodes have been process.
        Overrides:
        leaveToken in class AbstractCheck
        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