1 //////////////////////////////////////////////////////////////////////////////// 2 // checkstyle: Checks Java source code for adherence to a set of rules. 3 // Copyright (C) 2001-2019 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 * A check for 'TODO:' comments. To check for other patterns in Java comments, set 32 * property format. 33 * </p> 34 * <p> 35 * An example of how to configure the check is: 36 * </p> 37 * 38 * <pre> 39 * <module name="TodoComment"/> 40 * </pre> 41 * <p> 42 * An example of how to configure the check for comments that contain 43 * {@code TODO} or {@code FIXME}is: 44 * </p> 45 * 46 * <pre> 47 * <module name="TodoComment"> 48 * <property name="format" value="(TODO)|(FIXME)"/> 49 * </module> 50 * </pre> 51 */ 52 @StatelessCheck 53 public class TodoCommentCheck 54 extends AbstractCheck { 55 56 /** 57 * A key is pointing to the warning message text in "messages.properties" 58 * file. 59 */ 60 public static final String MSG_KEY = "todo.match"; 61 62 /** 63 * Regular expression pattern compiled from format. 64 */ 65 private Pattern format = Pattern.compile("TODO:"); 66 67 @Override 68 public boolean isCommentNodesRequired() { 69 return true; 70 } 71 72 /** 73 * Setter for 'todo' comment pattern. 74 * @param pattern 75 * pattern of 'todo' comment. 76 */ 77 public void setFormat(Pattern pattern) { 78 format = pattern; 79 } 80 81 @Override 82 public int[] getDefaultTokens() { 83 return getRequiredTokens(); 84 } 85 86 @Override 87 public int[] getAcceptableTokens() { 88 return getRequiredTokens(); 89 } 90 91 @Override 92 public int[] getRequiredTokens() { 93 return new int[] {TokenTypes.COMMENT_CONTENT }; 94 } 95 96 @Override 97 public void visitToken(DetailAST ast) { 98 final String[] lines = ast.getText().split("\n"); 99 100 for (int i = 0; i < lines.length; i++) { 101 if (format.matcher(lines[i]).find()) { 102 log(ast.getLineNo() + i, MSG_KEY, format.pattern()); 103 } 104 } 105 } 106 107 }