Class WhitespaceAfterCheck

  • All Implemented Interfaces:
    Configurable, Contextualizable

    public class WhitespaceAfterCheck
    extends AbstractCheck

    Checks that a token is followed by whitespace, with the exception that it does not check for whitespace after the semicolon of an empty for iterator. Use Check EmptyForIteratorPad to validate empty for iterators.

    To configure the check:

     <module name="WhitespaceAfter"/>
     

    Example:

      public void myTest() {
          if (foo) {              // OK
                  //...
          } else if(bar) {        // violation
                  //...
          }
    
          testMethod(foo, bar);   // OK
          testMethod(foo,bar);    // violation
    
          for (;;){}               // OK
          for(;;){}                // violation, space after 'for' is required
    
          try (InputStream ignored = System.in) {} // OK
          try(InputStream ignored = System.in) {} // violation ''try' is not followed by whitespace'
    
          try {} catch (Exception e){} // OK
          try{} catch (Exception e){} // violation ''try' is not followed by whitespace'
    
          try {} finally {} // OK
          try {} finally{} // violation ''finally' is not followed by whitespace'
    
          try {} catch (Error e){} finally {} // OK
          try {} catch (Error e){} finally{} // violation ''finally' is not followed by whitespace'
    
          try {} catch (Exception e){} // OK
          try {} catch(Exception e){} // violation ''catch' is not followed by whitespace'
    
          synchronized (this) { } // OK
          synchronized(this) { } // violation ''synchronized' is not followed by whitespace'
      }
      public String testOne() {
          return ("a" + "b"); // OK
      }
      public String testTwo() {
          return("a" + "b"); // violation 'return' is not followed by whitespace'
      }
      public static void main(String[] args) {
        int a = switch (args[0]) {
          case "got":
            yield (1); // OK
          case "my":
            yield(3); // violation ''yield' is not followed by whitespace'
          default:
            yield 2;
        };
      }
     

    To configure the check for whitespace only after COMMA and SEMI tokens:

     <module name="WhitespaceAfter">
       <property name="tokens" value="COMMA, SEMI"/>
     </module>
     

    Example:

         public void myTest() {
             int a; int b;           // OK
             int a;int b;            // violation
    
             testMethod(foo, bar);   // OK
             testMethod(foo,bar);    // violation
    
             for(;;) {} // OK
         }
     

    Parent is com.puppycrawl.tools.checkstyle.TreeWalker

    Violation Message Keys:

    • ws.notFollowed
    • ws.typeCast
    Since:
    3.0