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 * 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 @Override 61 public boolean isCommentNodesRequired() { 62 return true; 63 } 64 65 /** 66 * Setter to specify pattern to match comments against. 67 * 68 * @param pattern 69 * pattern of 'todo' comment. 70 * @since 3.0 71 */ 72 public void setFormat(Pattern pattern) { 73 format = pattern; 74 } 75 76 @Override 77 public int[] getDefaultTokens() { 78 return getRequiredTokens(); 79 } 80 81 @Override 82 public int[] getAcceptableTokens() { 83 return getRequiredTokens(); 84 } 85 86 @Override 87 public int[] getRequiredTokens() { 88 return new int[] {TokenTypes.COMMENT_CONTENT }; 89 } 90 91 @Override 92 public void visitToken(DetailAST ast) { 93 final String[] lines = ast.getText().split("\n"); 94 95 for (String line : lines) { 96 if (format.matcher(line).find()) { 97 log(ast, MSG_KEY, format.pattern()); 98 } 99 } 100 } 101 102 }