001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2017 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 * A check for 'TODO:' comments. To check for other patterns in Java comments, set
032 * property format.
033 * </p>
034 * <p>
035 * An example of how to configure the check is:
036 * </p>
037 *
038 * <pre>
039 * &lt;module name="TodoComment"/&gt;
040 * </pre>
041 * <p>
042 * An example of how to configure the check for comments that contain
043 * {@code TODO} or {@code FIXME}is:
044 * </p>
045 *
046 * <pre>
047 * &lt;module name="TodoComment"&gt;
048 *    &lt;property name="format" value="(TODO)|(FIXME)"/&gt;
049 * &lt;/module&gt;
050 * </pre>
051 * @author Oliver Burn
052 * @author Baratali Izmailov
053 */
054@StatelessCheck
055public class TodoCommentCheck
056        extends AbstractCheck {
057
058    /**
059     * A key is pointing to the warning message text in "messages.properties"
060     * file.
061     */
062    public static final String MSG_KEY = "todo.match";
063
064    /**
065     * Regular expression pattern compiled from format.
066     */
067    private Pattern format = Pattern.compile("TODO:");
068
069    @Override
070    public boolean isCommentNodesRequired() {
071        return true;
072    }
073
074    /**
075     * Setter for 'todo' comment pattern.
076     * @param pattern
077     *        pattern of 'todo' comment.
078     */
079    public void setFormat(Pattern pattern) {
080        format = pattern;
081    }
082
083    @Override
084    public int[] getDefaultTokens() {
085        return getRequiredTokens();
086    }
087
088    @Override
089    public int[] getAcceptableTokens() {
090        return getRequiredTokens();
091    }
092
093    @Override
094    public int[] getRequiredTokens() {
095        return new int[] {TokenTypes.COMMENT_CONTENT };
096    }
097
098    @Override
099    public void visitToken(DetailAST ast) {
100        final String[] lines = ast.getText().split("\n");
101
102        for (int i = 0; i < lines.length; i++) {
103            if (format.matcher(lines[i]).find()) {
104                log(ast.getLineNo() + i, MSG_KEY, format.pattern());
105            }
106        }
107    }
108}