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); }
 
  • Property validateComments - Control whether to validate whitespaces surrounding comments. Type is boolean. Default value is false.

To configure the check:

 <module name="SingleSpaceSeparator"/>
 

Example:

 int foo()   { // violation, 3 whitespaces
   return  1; // violation, 2 whitespaces
 }
 int fun1() { // OK, 1 whitespace
   return 3; // OK, 1 whitespace
 }
 void  fun2() {} // violation, 2 whitespaces
 

To configure the check so that it validates comments:

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

Example:

 void fun1() {}  // violation, 2 whitespaces before the comment starts
 void fun2() { return; }  /* violation here, 2 whitespaces before the comment starts */

 /* violation, 2 whitespaces after the comment ends */  int a;

 String s; /* OK, 1 whitespace */

 /**
 * This is a Javadoc comment
 */  int b; // violation, 2 whitespaces after the javadoc comment ends

 float f1; // OK, 1 whitespace

 /**
 * OK, 1 white space after the doc comment ends
 */ float f2;
 

Parent is com.puppycrawl.tools.checkstyle.TreeWalker

Violation Message Keys:

  • single.space.separator
Since:
6.19
  • Field Details

    • MSG_KEY

      public static final String MSG_KEY
      A key is pointing to the warning message text in "messages.properties" file.
      See Also:
  • Constructor Details

    • SingleSpaceSeparatorCheck

      public SingleSpaceSeparatorCheck()
  • Method Details

    • setValidateComments

      public void setValidateComments(boolean validateComments)
      Setter to control whether to validate whitespaces surrounding 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:
    • 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:
    • 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:
    • 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