View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 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   * <div>
31   * Checks for {@code TODO:} comments. Actually it is a generic
32   * <a href="https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/regex/Pattern.html">
33   * pattern</a> matcher on Java comments. To check for other patterns
34   * in Java comments, set the {@code format} property.
35   * </div>
36   *
37   * <p>
38   * Using {@code TODO:} comments is a great way to keep track of tasks that need to be done.
39   * Having them reported by Checkstyle makes it very hard to forget about them.
40   * </p>
41   * <ul>
42   * <li>
43   * Property {@code format} - Specify pattern to match comments against.
44   * Type is {@code java.util.regex.Pattern}.
45   * Default value is {@code "TODO:"}.
46   * </li>
47   * </ul>
48   *
49   * <p>
50   * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
51   * </p>
52   *
53   * <p>
54   * Violation Message Keys:
55   * </p>
56   * <ul>
57   * <li>
58   * {@code todo.match}
59   * </li>
60   * </ul>
61   *
62   * @since 3.0
63   */
64  @StatelessCheck
65  public class TodoCommentCheck
66          extends AbstractCheck {
67  
68      /**
69       * A key is pointing to the warning message text in "messages.properties"
70       * file.
71       */
72      public static final String MSG_KEY = "todo.match";
73  
74      /**
75       * Specify pattern to match comments against.
76       */
77      private Pattern format = Pattern.compile("TODO:");
78  
79      @Override
80      public boolean isCommentNodesRequired() {
81          return true;
82      }
83  
84      /**
85       * Setter to specify pattern to match comments against.
86       *
87       * @param pattern
88       *        pattern of 'todo' comment.
89       * @since 3.0
90       */
91      public void setFormat(Pattern pattern) {
92          format = pattern;
93      }
94  
95      @Override
96      public int[] getDefaultTokens() {
97          return getRequiredTokens();
98      }
99  
100     @Override
101     public int[] getAcceptableTokens() {
102         return getRequiredTokens();
103     }
104 
105     @Override
106     public int[] getRequiredTokens() {
107         return new int[] {TokenTypes.COMMENT_CONTENT };
108     }
109 
110     @Override
111     public void visitToken(DetailAST ast) {
112         final String[] lines = ast.getText().split("\n");
113 
114         for (String line : lines) {
115             if (format.matcher(line).find()) {
116                 log(ast, MSG_KEY, format.pattern());
117             }
118         }
119     }
120 
121 }