View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2017 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle.checks;
21  
22  import java.util.regex.Pattern;
23  
24  import com.puppycrawl.tools.checkstyle.StatelessCheck;
25  import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
26  import com.puppycrawl.tools.checkstyle.api.DetailAST;
27  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
28  
29  /**
30   * <p>
31   * A check for 'TODO:' comments. To check for other patterns in Java comments, set
32   * property format.
33   * </p>
34   * <p>
35   * An example of how to configure the check is:
36   * </p>
37   *
38   * <pre>
39   * &lt;module name="TodoComment"/&gt;
40   * </pre>
41   * <p>
42   * An example of how to configure the check for comments that contain
43   * {@code TODO} or {@code FIXME}is:
44   * </p>
45   *
46   * <pre>
47   * &lt;module name="TodoComment"&gt;
48   *    &lt;property name="format" value="(TODO)|(FIXME)"/&gt;
49   * &lt;/module&gt;
50   * </pre>
51   * @author Oliver Burn
52   * @author Baratali Izmailov
53   */
54  @StatelessCheck
55  public class TodoCommentCheck
56          extends AbstractCheck {
57  
58      /**
59       * A key is pointing to the warning message text in "messages.properties"
60       * file.
61       */
62      public static final String MSG_KEY = "todo.match";
63  
64      /**
65       * Regular expression pattern compiled from format.
66       */
67      private Pattern format = Pattern.compile("TODO:");
68  
69      @Override
70      public boolean isCommentNodesRequired() {
71          return true;
72      }
73  
74      /**
75       * Setter for 'todo' comment pattern.
76       * @param pattern
77       *        pattern of 'todo' comment.
78       */
79      public void setFormat(Pattern pattern) {
80          format = pattern;
81      }
82  
83      @Override
84      public int[] getDefaultTokens() {
85          return getRequiredTokens();
86      }
87  
88      @Override
89      public int[] getAcceptableTokens() {
90          return getRequiredTokens();
91      }
92  
93      @Override
94      public int[] getRequiredTokens() {
95          return new int[] {TokenTypes.COMMENT_CONTENT };
96      }
97  
98      @Override
99      public void visitToken(DetailAST ast) {
100         final String[] lines = ast.getText().split("\n");
101 
102         for (int i = 0; i < lines.length; i++) {
103             if (format.matcher(lines[i]).find()) {
104                 log(ast.getLineNo() + i, MSG_KEY, format.pattern());
105             }
106         }
107     }
108 }