Class CommentsIndentationCheck

  • All Implemented Interfaces:
    Configurable, Contextualizable

    public class CommentsIndentationCheck
    extends AbstractCheck

    Controls the indentation between comments and surrounding code. Comments are indented at the same level as the surrounding code. Detailed info about such convention can be found here

    Please take a look at the following examples to understand how the check works:

    Example #1: Block comments.

     1   /*
     2    * it is Ok
     3    */
     4   boolean bool = true;
     5
     6     /* violation
     7      * (block comment should have the same indentation level as line 9)
     8      */
     9   double d = 3.14;
     

    Example #2: Comment is placed at the end of the block and has previous statement.

     1   public void foo1() {
     2     foo2();
     3     // it is OK
     4   }
     5
     6   public void foo2() {
     7     foo3();
     8       // violation (comment should have the same indentation level as line 7)
     9   }
     

    Example #3: Comment is used as a single line border to separate groups of methods.

     1   /////////////////////////////// it is OK
     2
     3   public void foo7() {
     4     int a = 0;
     5   }
     6
     7     ///////////////////////////// violation (should have the same indentation level as line 9)
     8
     9   public void foo8() {}
     

    Example #4: Comment has distributed previous statement.

     1   public void foo11() {
     2     CheckUtil
     3       .getFirstNode(new DetailAST())
     4       .getFirstChild()
     5       .getNextSibling();
     6     // it is OK
     7   }
     8
     9   public void foo12() {
     10    CheckUtil
     11      .getFirstNode(new DetailAST())
     12      .getFirstChild()
     13      .getNextSibling();
     14              // violation (should have the same indentation level as line 10)
     15  }
     

    Example #5: Single line block comment is placed within an empty code block. Note, if comment is placed at the end of the empty code block, we have Checkstyle's limitations to clearly detect user intention of explanation target - above or below. The only case we can assume as a violation is when a single line comment within the empty code block has indentation level that is lower than the indentation level of the closing right curly brace.

     1   public void foo46() {
     2     // comment
     3     // block
     4     // it is OK (we cannot clearly detect user intention of explanation target)
     5   }
     6
     7   public void foo46() {
     8  // comment
     9  // block
     10 // violation (comment should have the same indentation level as line 11)
     11  }
     

    Example #6: 'fallthrough' comments and similar.

     0   switch(a) {
     1     case "1":
     2       int k = 7;
     3       // it is OK
     4     case "2":
     5       int k = 7;
     6     // it is OK
     7     case "3":
     8       if (true) {}
     9           // violation (should have the same indentation level as line 8 or 10)
     10    case "4":
     11    case "5": {
     12      int a;
     13    }
     14    // fall through (it is OK)
     15    case "12": {
     16      int a;
     17    }
     18    default:
     19      // it is OK
     20  }
     

    Example #7: Comment is placed within a distributed statement.

     1   String breaks = "J"
     2   // violation (comment should have the same indentation level as line 3)
     3       + "A"
     4       // it is OK
     5       + "V"
     6       + "A"
     7   // it is OK
     8   ;
     

    Example #8: Comment is placed within an empty case block. Note, if comment is placed at the end of the empty case block, we have Checkstyle's limitations to clearly detect user intention of explanation target - above or below. The only case we can assume as a violation is when a single line comment within the empty case block has indentation level that is lower than the indentation level of the next case token.

     1   case 4:
     2     // it is OK
     3   case 5:
     4  // violation (should have the same indentation level as line 3 or 5)
     5   case 6:
     

    Example #9: Single line block comment has previous and next statement.

     1   String s1 = "Clean code!";
     2      s.toString().toString().toString();
     3   // single line
     4   // block
     5   // comment (it is OK)
     6   int a = 5;
     7
     8   String s2 = "Code complete!";
     9    s.toString().toString().toString();
     10            // violation (should have the same indentation level as line 11)
     11       // violation (should have the same indentation level as line 12)
     12     // violation (should have the same indentation level as line 13)
     13  int b = 18;
     

    Example #10: Comment within the block tries to describe the next code block.

     1   public void foo42() {
     2     int a = 5;
     3     if (a == 5) {
     4       int b;
     5       // it is OK
     6      } else if (a ==6) { ... }
     7   }
     8
     9   public void foo43() {
     10    try {
     11      int a;
     12     // Why do we catch exception here? - violation (not the same indentation as line 11)
     13     } catch (Exception e) { ... }
     14  }
     

    To configure the Check:

     <module name="CommentsIndentation"/>
     

    Parent is com.puppycrawl.tools.checkstyle.TreeWalker

    Violation Message Keys:

    • comments.indentation.block
    • comments.indentation.single
    Since:
    6.10
    • Field Detail

      • MSG_KEY_SINGLE

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

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

      • CommentsIndentationCheck

        public CommentsIndentationCheck()
    • Method Detail

      • 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.
      • visitToken

        public void visitToken​(DetailAST commentAst)
        Description copied from class: AbstractCheck
        Called to process a token.
        Overrides:
        visitToken in class AbstractCheck
        Parameters:
        commentAst - the token to process