001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2024 the original author or authors. 004// 005// This library is free software; you can redistribute it and/or 006// modify it under the terms of the GNU Lesser General Public 007// License as published by the Free Software Foundation; either 008// version 2.1 of the License, or (at your option) any later version. 009// 010// This library is distributed in the hope that it will be useful, 011// but WITHOUT ANY WARRANTY; without even the implied warranty of 012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013// Lesser General Public License for more details. 014// 015// You should have received a copy of the GNU Lesser General Public 016// License along with this library; if not, write to the Free Software 017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 018/////////////////////////////////////////////////////////////////////////////////////////////// 019 020package com.puppycrawl.tools.checkstyle.checks; 021 022import java.util.regex.Pattern; 023 024import com.puppycrawl.tools.checkstyle.StatelessCheck; 025import com.puppycrawl.tools.checkstyle.api.AbstractCheck; 026import com.puppycrawl.tools.checkstyle.api.DetailAST; 027import com.puppycrawl.tools.checkstyle.api.TokenTypes; 028 029/** 030 * <div> 031 * Checks for {@code TODO:} comments. Actually it is a generic 032 * <a href="https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/regex/Pattern.html"> 033 * pattern</a> matcher on Java comments. To check for other patterns 034 * in Java comments, set the {@code format} property. 035 * </div> 036 * 037 * <p> 038 * Using {@code TODO:} comments is a great way to keep track of tasks that need to be done. 039 * Having them reported by Checkstyle makes it very hard to forget about them. 040 * </p> 041 * <ul> 042 * <li> 043 * Property {@code format} - Specify pattern to match comments against. 044 * Type is {@code java.util.regex.Pattern}. 045 * Default value is {@code "TODO:"}. 046 * </li> 047 * </ul> 048 * 049 * <p> 050 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} 051 * </p> 052 * 053 * <p> 054 * Violation Message Keys: 055 * </p> 056 * <ul> 057 * <li> 058 * {@code todo.match} 059 * </li> 060 * </ul> 061 * 062 * @since 3.0 063 */ 064@StatelessCheck 065public class TodoCommentCheck 066 extends AbstractCheck { 067 068 /** 069 * A key is pointing to the warning message text in "messages.properties" 070 * file. 071 */ 072 public static final String MSG_KEY = "todo.match"; 073 074 /** 075 * Specify pattern to match comments against. 076 */ 077 private Pattern format = Pattern.compile("TODO:"); 078 079 @Override 080 public boolean isCommentNodesRequired() { 081 return true; 082 } 083 084 /** 085 * Setter to specify pattern to match comments against. 086 * 087 * @param pattern 088 * pattern of 'todo' comment. 089 * @since 3.0 090 */ 091 public void setFormat(Pattern pattern) { 092 format = pattern; 093 } 094 095 @Override 096 public int[] getDefaultTokens() { 097 return getRequiredTokens(); 098 } 099 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}