001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2025 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/17/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 * Notes:
039 * Using {@code TODO:} comments is a great way to keep track of tasks that need to be done.
040 * Having them reported by Checkstyle makes it very hard to forget about them.
041 * </p>
042 * <ul>
043 * <li>
044 * Property {@code format} - Specify pattern to match comments against.
045 * Type is {@code java.util.regex.Pattern}.
046 * Default value is {@code "TODO:"}.
047 * </li>
048 * </ul>
049 *
050 * <p>
051 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
052 * </p>
053 *
054 * <p>
055 * Violation Message Keys:
056 * </p>
057 * <ul>
058 * <li>
059 * {@code todo.match}
060 * </li>
061 * </ul>
062 *
063 * @since 3.0
064 */
065@StatelessCheck
066public class TodoCommentCheck
067        extends AbstractCheck {
068
069    /**
070     * A key is pointing to the warning message text in "messages.properties"
071     * file.
072     */
073    public static final String MSG_KEY = "todo.match";
074
075    /**
076     * Specify pattern to match comments against.
077     */
078    private Pattern format = Pattern.compile("TODO:");
079
080    @Override
081    public boolean isCommentNodesRequired() {
082        return true;
083    }
084
085    /**
086     * Setter to specify pattern to match comments against.
087     *
088     * @param pattern
089     *        pattern of 'todo' comment.
090     * @since 3.0
091     */
092    public void setFormat(Pattern pattern) {
093        format = pattern;
094    }
095
096    @Override
097    public int[] getDefaultTokens() {
098        return getRequiredTokens();
099    }
100
101    @Override
102    public int[] getAcceptableTokens() {
103        return getRequiredTokens();
104    }
105
106    @Override
107    public int[] getRequiredTokens() {
108        return new int[] {TokenTypes.COMMENT_CONTENT };
109    }
110
111    @Override
112    public void visitToken(DetailAST ast) {
113        final String[] lines = ast.getText().split("\n");
114
115        for (String line : lines) {
116            if (format.matcher(line).find()) {
117                log(ast, MSG_KEY, format.pattern());
118            }
119        }
120    }
121
122}