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