Class SingleSpaceSeparatorCheck

  • All Implemented Interfaces:
    Configurable, Contextualizable

    public class SingleSpaceSeparatorCheck
    extends AbstractCheck

    Checks that non-whitespace characters are separated by no more than one whitespace. Separating characters by tabs or multiple spaces will be reported. Currently the check doesn't permit horizontal alignment. To inspect whitespaces before and after comments, set the property validateComments to true.

    Setting validateComments to false will ignore cases like:

     int i;  // Multiple whitespaces before comment tokens will be ignored.
     private void foo(int  /* whitespaces before and after block-comments will be
     ignored */  i) {
     

    Sometimes, users like to space similar items on different lines to the same column position for easier reading. This feature isn't supported by this check, so both braces in the following case will be reported as violations.

     public long toNanos(long d)  { return d;             }  // 2 violations
     public long toMicros(long d) { return d / (C1 / C0); }
     

    Check have following options:

    • validateComments - Boolean when set to true, whitespaces surrounding comments will be ignored. Default value is false.

    To configure the check:

     <module name="SingleSpaceSeparator"/>
     

    To configure the check so that it validates comments:

     <module name="SingleSpaceSeparator">
     <property name="validateComments" value="true"/>
     </module>
     
    • Field Detail

      • MSG_KEY

        public static final java.lang.String MSG_KEY
        A key is pointing to the warning message text in "messages.properties" file.
        See Also:
        Constant Field Values
      • validateComments

        private boolean validateComments
        Indicates if whitespaces surrounding comments will be ignored.
    • Constructor Detail

      • SingleSpaceSeparatorCheck

        public SingleSpaceSeparatorCheck()
    • Method Detail

      • setValidateComments

        public void setValidateComments​(boolean validateComments)
        Sets whether or not to validate surrounding whitespaces at comments.
        Parameters:
        validateComments - true to validate surrounding whitespaces at comments.
      • 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
      • isCommentNodesRequired

        public boolean isCommentNodesRequired()
        Description copied from class: AbstractCheck
        Whether comment nodes are required or not.
        Overrides:
        isCommentNodesRequired in class AbstractCheck
        Returns:
        false as a default value.
      • beginTree

        public void beginTree​(DetailAST rootAST)
        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:
        rootAST - the root of the tree
      • visitEachToken

        private void visitEachToken​(DetailAST node)
        Examines every sibling and child of node for violations.
        Parameters:
        node - The node to start examining.
      • isTextSeparatedCorrectlyFromPrevious

        private boolean isTextSeparatedCorrectlyFromPrevious​(java.lang.String line,
                                                             int columnNo)
        Checks if characters in line at and around columnNo has the correct number of spaces. to return true the following conditions must be met:
        - the character at columnNo is the first in the line.
        - the character at columnNo is not separated by whitespaces from the previous non-whitespace character.
        - the character at columnNo is separated by only one whitespace from the previous non-whitespace character.
        - validateComments is disabled and the previous text is the end of a block comment.
        Parameters:
        line - The line in the file to examine.
        columnNo - The column position in the line to examine.
        Returns:
        true if the text at columnNo is separated correctly from the previous token.
      • isSingleSpace

        private static boolean isSingleSpace​(java.lang.String line,
                                             int columnNo)
        Checks if the line at columnNo is a single space, and not preceded by another space.
        Parameters:
        line - The line in the file to examine.
        columnNo - The column position in the line to examine.
        Returns:
        true if the character at columnNo is a space, and not preceded by another space.
      • isSpace

        private static boolean isSpace​(java.lang.String line,
                                       int columnNo)
        Checks if the line at columnNo is a space.
        Parameters:
        line - The line in the file to examine.
        columnNo - The column position in the line to examine.
        Returns:
        true if the character at columnNo is a space.
      • isPrecededByMultipleWhitespaces

        private static boolean isPrecededByMultipleWhitespaces​(java.lang.String line,
                                                               int columnNo)
        Checks if the line at columnNo is preceded by at least 2 whitespaces.
        Parameters:
        line - The line in the file to examine.
        columnNo - The column position in the line to examine.
        Returns:
        true if there are at least 2 whitespace characters before columnNo.
      • isWhitespace

        private static boolean isWhitespace​(java.lang.String line,
                                            int columnNo)
        Checks if the line at columnNo is a whitespace character.
        Parameters:
        line - The line in the file to examine.
        columnNo - The column position in the line to examine.
        Returns:
        true if the character at columnNo is a whitespace.
      • isFirstInLine

        private static boolean isFirstInLine​(java.lang.String line,
                                             int columnNo)
        Checks if the line up to and including columnNo is all non-whitespace text encountered.
        Parameters:
        line - The line in the file to examine.
        columnNo - The column position in the line to examine.
        Returns:
        true if the column position is the first non-whitespace text on the line.
      • isBlockCommentEnd

        private static boolean isBlockCommentEnd​(java.lang.String line,
                                                 int columnNo)
        Checks if the line at columnNo is the end of a comment, '*/'.
        Parameters:
        line - The line in the file to examine.
        columnNo - The column position in the line to examine.
        Returns:
        true if the previous text is a end comment block.