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.javadoc;
021
022import java.util.regex.Matcher;
023import java.util.regex.Pattern;
024
025import com.puppycrawl.tools.checkstyle.StatelessCheck;
026import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
027import com.puppycrawl.tools.checkstyle.api.DetailAST;
028import com.puppycrawl.tools.checkstyle.api.FileContents;
029import com.puppycrawl.tools.checkstyle.api.SeverityLevel;
030import com.puppycrawl.tools.checkstyle.api.TextBlock;
031import com.puppycrawl.tools.checkstyle.api.TokenTypes;
032import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
033
034/**
035 * <p>
036 * Requires user defined Javadoc tag to be present in Javadoc comment with defined format.
037 * To define the format for a tag, set property tagFormat to a regular expression.
038 * Property tagSeverity is used for severity of events when the tag exists.
039 * </p>
040 * <ul>
041 * <li>
042 * Property {@code tag} - Specify the name of tag.
043 * Default value is {@code null}.
044 * </li>
045 * <li>
046 * Property {@code tagFormat} - Specify the regexp to match tag content.
047 * Default value is {@code null}.
048 * </li>
049 * <li>
050 * Property {@code tagSeverity} - Specify the severity level when tag is found and printed.
051 * Default value is {@code info}.
052 * </li>
053 * <li>
054 * Property {@code tokens} - tokens to check
055 * Default value is:
056 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#INTERFACE_DEF">
057 * INTERFACE_DEF</a>,
058 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#CLASS_DEF">
059 * CLASS_DEF</a>,
060 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ENUM_DEF">
061 * ENUM_DEF</a>,
062 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ANNOTATION_DEF">
063 * ANNOTATION_DEF</a>.
064 * </li>
065 * </ul>
066 * <p>
067 * To configure the check for printing author name:
068 * </p>
069 * <pre>
070 * &lt;module name="WriteTag"&gt;
071 *   &lt;property name="tag" value="@author"/&gt;
072 *   &lt;property name="tagFormat" value="\S"/&gt;
073 * &lt;/module&gt;
074 * </pre>
075 * <p>
076 * To configure the check to print warnings if an "@incomplete" tag is found,
077 * and not print anything if it is not found:
078 * </p>
079 * <pre>
080 * &lt;module name="WriteTag"&gt;
081 *   &lt;property name="tag" value="@incomplete"/&gt;
082 *   &lt;property name="tagFormat" value="\S"/&gt;
083 *   &lt;property name="severity" value="ignore"/&gt;
084 *   &lt;property name="tagSeverity" value="warning"/&gt;
085 * &lt;/module&gt;
086 * </pre>
087 *
088 * @since 4.2
089 */
090@StatelessCheck
091public class WriteTagCheck
092    extends AbstractCheck {
093
094    /**
095     * A key is pointing to the warning message text in "messages.properties"
096     * file.
097     */
098    public static final String MSG_MISSING_TAG = "type.missingTag";
099
100    /**
101     * A key is pointing to the warning message text in "messages.properties"
102     * file.
103     */
104    public static final String MSG_WRITE_TAG = "javadoc.writeTag";
105
106    /**
107     * A key is pointing to the warning message text in "messages.properties"
108     * file.
109     */
110    public static final String MSG_TAG_FORMAT = "type.tagFormat";
111
112    /** Compiled regexp to match tag. */
113    private Pattern tagRegExp;
114    /** Specify the regexp to match tag content. */
115    private Pattern tagFormat;
116
117    /** Specify the name of tag. */
118    private String tag;
119    /** Specify the severity level when tag is found and printed. */
120    private SeverityLevel tagSeverity = SeverityLevel.INFO;
121
122    /**
123     * Setter to specify the name of tag.
124     *
125     * @param tag tag to check
126     */
127    public void setTag(String tag) {
128        this.tag = tag;
129        tagRegExp = CommonUtil.createPattern(tag + "\\s*(.*$)");
130    }
131
132    /**
133     * Setter to specify the regexp to match tag content.
134     *
135     * @param pattern a {@code String} value
136     */
137    public void setTagFormat(Pattern pattern) {
138        tagFormat = pattern;
139    }
140
141    /**
142     * Setter to specify the severity level when tag is found and printed.
143     *
144     * @param severity  The new severity level
145     * @see SeverityLevel
146     */
147    public final void setTagSeverity(SeverityLevel severity) {
148        tagSeverity = severity;
149    }
150
151    @Override
152    public int[] getDefaultTokens() {
153        return new int[] {TokenTypes.INTERFACE_DEF,
154                          TokenTypes.CLASS_DEF,
155                          TokenTypes.ENUM_DEF,
156                          TokenTypes.ANNOTATION_DEF,
157        };
158    }
159
160    @Override
161    public int[] getAcceptableTokens() {
162        return new int[] {TokenTypes.INTERFACE_DEF,
163                          TokenTypes.CLASS_DEF,
164                          TokenTypes.ENUM_DEF,
165                          TokenTypes.ANNOTATION_DEF,
166                          TokenTypes.METHOD_DEF,
167                          TokenTypes.CTOR_DEF,
168                          TokenTypes.ENUM_CONSTANT_DEF,
169                          TokenTypes.ANNOTATION_FIELD_DEF,
170        };
171    }
172
173    @Override
174    public int[] getRequiredTokens() {
175        return CommonUtil.EMPTY_INT_ARRAY;
176    }
177
178    @Override
179    public void visitToken(DetailAST ast) {
180        final FileContents contents = getFileContents();
181        final int lineNo = ast.getLineNo();
182        final TextBlock cmt =
183            contents.getJavadocBefore(lineNo);
184        if (cmt == null) {
185            log(lineNo, MSG_MISSING_TAG, tag);
186        }
187        else {
188            checkTag(lineNo, cmt.getText());
189        }
190    }
191
192    /**
193     * Verifies that a type definition has a required tag.
194     * @param lineNo the line number for the type definition.
195     * @param comment the Javadoc comment for the type definition.
196     */
197    private void checkTag(int lineNo, String... comment) {
198        if (tagRegExp != null) {
199            boolean hasTag = false;
200            for (int i = 0; i < comment.length; i++) {
201                final String commentValue = comment[i];
202                final Matcher matcher = tagRegExp.matcher(commentValue);
203                if (matcher.find()) {
204                    hasTag = true;
205                    final int contentStart = matcher.start(1);
206                    final String content = commentValue.substring(contentStart);
207                    if (tagFormat == null || tagFormat.matcher(content).find()) {
208                        logTag(lineNo + i - comment.length, tag, content);
209                    }
210                    else {
211                        log(lineNo + i - comment.length, MSG_TAG_FORMAT, tag, tagFormat.pattern());
212                    }
213                }
214            }
215            if (!hasTag) {
216                log(lineNo, MSG_MISSING_TAG, tag);
217            }
218        }
219    }
220
221    /**
222     * Log a message.
223     *
224     * @param line the line number where the violation was found
225     * @param tagName the javadoc tag to be logged
226     * @param tagValue the contents of the tag
227     *
228     * @see java.text.MessageFormat
229     */
230    private void logTag(int line, String tagName, String tagValue) {
231        final String originalSeverity = getSeverity();
232        setSeverity(tagSeverity.getName());
233
234        log(line, MSG_WRITE_TAG, tagName, tagValue);
235
236        setSeverity(originalSeverity);
237    }
238
239}