1 ///////////////////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3 // Copyright (C) 2001-2026 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 *
43 * @since 3.0
44 */
45 @StatelessCheck
46 public class TodoCommentCheck
47 extends AbstractCheck {
48
49 /**
50 * A key is pointing to the warning message text in "messages.properties"
51 * file.
52 */
53 public static final String MSG_KEY = "todo.match";
54
55 /**
56 * Specify pattern to match comments against.
57 */
58 private Pattern format = Pattern.compile("TODO:");
59
60 /**
61 * Creates a new {@code TodoCommentCheck} instance.
62 */
63 public TodoCommentCheck() {
64 // no code by default
65 }
66
67 @Override
68 public boolean isCommentNodesRequired() {
69 return true;
70 }
71
72 /**
73 * Setter to specify pattern to match comments against.
74 *
75 * @param pattern
76 * pattern of 'todo' comment.
77 * @since 3.0
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", -1);
101
102 for (String line : lines) {
103 if (format.matcher(line).find()) {
104 log(ast, MSG_KEY, format.pattern());
105 }
106 }
107 }
108
109 }