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 * <p> 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 * </p> 036 * <p> 037 * Using {@code TODO:} comments is a great way to keep track of tasks that need to be done. 038 * Having them reported by Checkstyle makes it very hard to forget about them. 039 * </p> 040 * <ul> 041 * <li> 042 * Property {@code format} - Specify pattern to match comments against. 043 * Type is {@code java.util.regex.Pattern}. 044 * Default value is {@code "TODO:"}. 045 * </li> 046 * </ul> 047 * <p> 048 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} 049 * </p> 050 * <p> 051 * Violation Message Keys: 052 * </p> 053 * <ul> 054 * <li> 055 * {@code todo.match} 056 * </li> 057 * </ul> 058 * 059 * @since 3.0 060 */ 061@StatelessCheck 062public class TodoCommentCheck 063 extends AbstractCheck { 064 065 /** 066 * A key is pointing to the warning message text in "messages.properties" 067 * file. 068 */ 069 public static final String MSG_KEY = "todo.match"; 070 071 /** 072 * Specify pattern to match comments against. 073 */ 074 private Pattern format = Pattern.compile("TODO:"); 075 076 @Override 077 public boolean isCommentNodesRequired() { 078 return true; 079 } 080 081 /** 082 * Setter to specify pattern to match comments against. 083 * 084 * @param pattern 085 * pattern of 'todo' comment. 086 * @since 3.0 087 */ 088 public void setFormat(Pattern pattern) { 089 format = pattern; 090 } 091 092 @Override 093 public int[] getDefaultTokens() { 094 return getRequiredTokens(); 095 } 096 097 @Override 098 public int[] getAcceptableTokens() { 099 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}