View Javadoc
1   /*
2    * Copyright (c) 2011-2024 Qulice.com
3    *
4    * All rights reserved.
5    *
6    * Redistribution and use in source and binary forms, with or without
7    * modification, are permitted provided that the following conditions
8    * are met: 1) Redistributions of source code must retain the above
9    * copyright notice, this list of conditions and the following
10   * disclaimer. 2) Redistributions in binary form must reproduce the above
11   * copyright notice, this list of conditions and the following
12   * disclaimer in the documentation and/or other materials provided
13   * with the distribution. 3) Neither the name of the Qulice.com nor
14   * the names of its contributors may be used to endorse or promote
15   * products derived from this software without specific prior written
16   * permission.
17   *
18   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
20   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22   * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29   * OF THE POSSIBILITY OF SUCH DAMAGE.
30   */
31  package com.qulice.checkstyle;
32  
33  import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
34  import com.puppycrawl.tools.checkstyle.api.DetailAST;
35  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
36  import java.util.Arrays;
37  
38  /**
39   * Checks method bodies for comments. All comments in method bodies are
40   * prohibited.
41   *
42   * <p>We believe that in-code comments and empty lines are evil. If you
43   * need to use
44   * a comment inside a method - your code needs refactoring. Either move that
45   * comment to a method javadoc block or add a logging mechanism with the same
46   * text.
47   *
48   * @since 0.3
49   * @todo #260 Add handling of multiple anonymous classes inside methods by
50   *  looking at the recursive tree.
51   */
52  public final class MethodBodyCommentsCheck extends AbstractCheck {
53  
54      @Override
55      public int[] getDefaultTokens() {
56          return new int[] {
57              TokenTypes.CTOR_DEF,
58              TokenTypes.METHOD_DEF,
59          };
60      }
61  
62      @Override
63      public int[] getAcceptableTokens() {
64          return this.getDefaultTokens();
65      }
66  
67      @Override
68      public int[] getRequiredTokens() {
69          return this.getDefaultTokens();
70      }
71  
72      @Override
73      public void visitToken(final DetailAST ast) {
74          final DetailAST start = ast.findFirstToken(TokenTypes.SLIST);
75          final String[] lines = Arrays.copyOf(
76              this.getLines(), this.getLines().length
77          );
78          if (start != null) {
79              DetailAST ostart = start.findFirstToken(TokenTypes.VARIABLE_DEF);
80              final int[] tokens = {
81                  TokenTypes.ASSIGN, TokenTypes.EXPR,
82                  TokenTypes.LITERAL_NEW, TokenTypes.OBJBLOCK,
83              };
84              for (final int token : tokens) {
85                  if (ostart != null) {
86                      ostart = ostart.findFirstToken(token);
87                  }
88              }
89              if (ostart != null
90                  && ostart.getType() == tokens[tokens.length - 1]) {
91                  Arrays.fill(
92                      lines, ostart.getLineNo(),
93                      ostart.findFirstToken(TokenTypes.RCURLY).getLineNo(), ""
94                  );
95              }
96              this.checkMethod(
97                  lines,
98                  start.getLineNo(),
99                  start.findFirstToken(TokenTypes.RCURLY).getLineNo() - 1
100             );
101         }
102     }
103 
104     /**
105      * Checks method body for comments.
106      * @param lines Array of lines, containing code to check.
107      * @param start Start line of the method body.
108      * @param end End line of the method body.
109      */
110     private void checkMethod(final String[] lines, final int start,
111         final int end) {
112         final boolean oneliner = start == end - 1;
113         for (int pos = start; pos < end; ++pos) {
114             final String line = lines[pos].trim();
115             if (line.startsWith("//") || line.startsWith("/*")) {
116                 final String comment = line.substring(2).trim();
117                 if (!comment.startsWith("@checkstyle") && !oneliner) {
118                     this.log(pos + 1, "Comments in method body are prohibited");
119                 }
120             }
121         }
122     }
123 }