001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2020 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 * regular expression</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 * Default value is {@code "TODO:"}. 044 * </li> 045 * </ul> 046 * <p> 047 * To configure the check: 048 * </p> 049 * <pre> 050 * <module name="TodoComment"/> 051 * </pre> 052 * <p> 053 * Example: 054 * </p> 055 * <pre> 056 * i++; // TODO: do differently in future // violation 057 * i++; // todo: do differently in future // OK 058 * </pre> 059 * <p> 060 * To configure the check for comments that contain {@code TODO} and {@code FIXME}: 061 * </p> 062 * <pre> 063 * <module name="TodoComment"> 064 * <property name="format" value="(TODO)|(FIXME)"/> 065 * </module> 066 * </pre> 067 * <p> 068 * Example: 069 * </p> 070 * <pre> 071 * i++; // TODO: do differently in future // violation 072 * i++; // todo: do differently in future // OK 073 * i=i/x; // FIXME: handle x = 0 case // violation 074 * i=i/x; // FIX : handle x = 0 case // OK 075 * </pre> 076 * 077 * @since 3.0 078 */ 079@StatelessCheck 080public class TodoCommentCheck 081 extends AbstractCheck { 082 083 /** 084 * A key is pointing to the warning message text in "messages.properties" 085 * file. 086 */ 087 public static final String MSG_KEY = "todo.match"; 088 089 /** 090 * Specify pattern to match comments against. 091 */ 092 private Pattern format = Pattern.compile("TODO:"); 093 094 @Override 095 public boolean isCommentNodesRequired() { 096 return true; 097 } 098 099 /** 100 * Setter to specify pattern to match comments against. 101 * 102 * @param pattern 103 * pattern of 'todo' comment. 104 */ 105 public void setFormat(Pattern pattern) { 106 format = pattern; 107 } 108 109 @Override 110 public int[] getDefaultTokens() { 111 return getRequiredTokens(); 112 } 113 114 @Override 115 public int[] getAcceptableTokens() { 116 return getRequiredTokens(); 117 } 118 119 @Override 120 public int[] getRequiredTokens() { 121 return new int[] {TokenTypes.COMMENT_CONTENT }; 122 } 123 124 @Override 125 public void visitToken(DetailAST ast) { 126 final String[] lines = ast.getText().split("\n"); 127 128 for (int i = 0; i < lines.length; i++) { 129 if (format.matcher(lines[i]).find()) { 130 log(ast.getLineNo() + i, MSG_KEY, format.pattern()); 131 } 132 } 133 } 134 135}