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