001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2024 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.annotation;
021
022import java.util.ArrayDeque;
023import java.util.Deque;
024import java.util.Objects;
025import java.util.regex.Matcher;
026import java.util.regex.Pattern;
027
028import com.puppycrawl.tools.checkstyle.StatelessCheck;
029import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
030import com.puppycrawl.tools.checkstyle.api.DetailAST;
031import com.puppycrawl.tools.checkstyle.api.TokenTypes;
032import com.puppycrawl.tools.checkstyle.utils.AnnotationUtil;
033import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
034
035/**
036 * <div>
037 * Allows to specify what warnings that
038 * {@code @SuppressWarnings} is not allowed to suppress.
039 * You can also specify a list of TokenTypes that
040 * the configured warning(s) cannot be suppressed on.
041 * </div>
042 *
043 * <p>
044 * Limitations:  This check does not consider conditionals
045 * inside the &#64;SuppressWarnings annotation.
046 * </p>
047 *
048 * <p>
049 * For example:
050 * {@code @SuppressWarnings((false) ? (true) ? "unchecked" : "foo" : "unused")}.
051 * According to the above example, the "unused" warning is being suppressed
052 * not the "unchecked" or "foo" warnings.  All of these warnings will be
053 * considered and matched against regardless of what the conditional
054 * evaluates to.
055 * The check also does not support code like {@code @SuppressWarnings("un" + "used")},
056 * {@code @SuppressWarnings((String) "unused")} or
057 * {@code @SuppressWarnings({('u' + (char)'n') + (""+("used" + (String)"")),})}.
058 * </p>
059 *
060 * <p>
061 * By default, any warning specified will be disallowed on
062 * all legal TokenTypes unless otherwise specified via
063 * the tokens property.
064 * </p>
065 *
066 * <p>
067 * Also, by default warnings that are empty strings or all
068 * whitespace (regex: ^$|^\s+$) are flagged.  By specifying,
069 * the format property these defaults no longer apply.
070 * </p>
071 *
072 * <p>This check can be configured so that the "unchecked"
073 * and "unused" warnings cannot be suppressed on
074 * anything but variable and parameter declarations.
075 * See below of an example.
076 * </p>
077 * <ul>
078 * <li>
079 * Property {@code format} - Specify the RegExp to match against warnings. Any warning
080 * being suppressed matching this pattern will be flagged.
081 * Type is {@code java.util.regex.Pattern}.
082 * Default value is {@code "^\s*+$"}.
083 * </li>
084 * <li>
085 * Property {@code tokens} - tokens to check
086 * Type is {@code java.lang.String[]}.
087 * Validation type is {@code tokenSet}.
088 * Default value is:
089 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#CLASS_DEF">
090 * CLASS_DEF</a>,
091 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#INTERFACE_DEF">
092 * INTERFACE_DEF</a>,
093 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ENUM_DEF">
094 * ENUM_DEF</a>,
095 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ANNOTATION_DEF">
096 * ANNOTATION_DEF</a>,
097 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ANNOTATION_FIELD_DEF">
098 * ANNOTATION_FIELD_DEF</a>,
099 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ENUM_CONSTANT_DEF">
100 * ENUM_CONSTANT_DEF</a>,
101 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#PARAMETER_DEF">
102 * PARAMETER_DEF</a>,
103 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#VARIABLE_DEF">
104 * VARIABLE_DEF</a>,
105 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#METHOD_DEF">
106 * METHOD_DEF</a>,
107 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#CTOR_DEF">
108 * CTOR_DEF</a>,
109 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#COMPACT_CTOR_DEF">
110 * COMPACT_CTOR_DEF</a>,
111 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#RECORD_DEF">
112 * RECORD_DEF</a>,
113 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#PATTERN_VARIABLE_DEF">
114 * PATTERN_VARIABLE_DEF</a>.
115 * </li>
116 * </ul>
117 *
118 * <p>
119 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
120 * </p>
121 *
122 * <p>
123 * Violation Message Keys:
124 * </p>
125 * <ul>
126 * <li>
127 * {@code suppressed.warning.not.allowed}
128 * </li>
129 * </ul>
130 *
131 * @since 5.0
132 */
133@StatelessCheck
134public class SuppressWarningsCheck extends AbstractCheck {
135
136    /**
137     * A key is pointing to the warning message text in "messages.properties"
138     * file.
139     */
140    public static final String MSG_KEY_SUPPRESSED_WARNING_NOT_ALLOWED =
141        "suppressed.warning.not.allowed";
142
143    /** {@link SuppressWarnings SuppressWarnings} annotation name. */
144    private static final String SUPPRESS_WARNINGS = "SuppressWarnings";
145
146    /**
147     * Fully-qualified {@link SuppressWarnings SuppressWarnings}
148     * annotation name.
149     */
150    private static final String FQ_SUPPRESS_WARNINGS =
151        "java.lang." + SUPPRESS_WARNINGS;
152
153    /**
154     * Specify the RegExp to match against warnings. Any warning
155     * being suppressed matching this pattern will be flagged.
156     */
157    private Pattern format = Pattern.compile("^\\s*+$");
158
159    /**
160     * Setter to specify the RegExp to match against warnings. Any warning
161     * being suppressed matching this pattern will be flagged.
162     *
163     * @param pattern the new pattern
164     * @since 5.0
165     */
166    public final void setFormat(Pattern pattern) {
167        format = pattern;
168    }
169
170    @Override
171    public final int[] getDefaultTokens() {
172        return getAcceptableTokens();
173    }
174
175    @Override
176    public final int[] getAcceptableTokens() {
177        return new int[] {
178            TokenTypes.CLASS_DEF,
179            TokenTypes.INTERFACE_DEF,
180            TokenTypes.ENUM_DEF,
181            TokenTypes.ANNOTATION_DEF,
182            TokenTypes.ANNOTATION_FIELD_DEF,
183            TokenTypes.ENUM_CONSTANT_DEF,
184            TokenTypes.PARAMETER_DEF,
185            TokenTypes.VARIABLE_DEF,
186            TokenTypes.METHOD_DEF,
187            TokenTypes.CTOR_DEF,
188            TokenTypes.COMPACT_CTOR_DEF,
189            TokenTypes.RECORD_DEF,
190            TokenTypes.PATTERN_VARIABLE_DEF,
191        };
192    }
193
194    @Override
195    public int[] getRequiredTokens() {
196        return CommonUtil.EMPTY_INT_ARRAY;
197    }
198
199    @Override
200    public void visitToken(final DetailAST ast) {
201        final DetailAST annotation = getSuppressWarnings(ast);
202
203        if (annotation != null) {
204            final DetailAST warningHolder =
205                findWarningsHolder(annotation);
206
207            final DetailAST token =
208                    warningHolder.findFirstToken(TokenTypes.ANNOTATION_MEMBER_VALUE_PAIR);
209
210            // case like '@SuppressWarnings(value = UNUSED)'
211            final DetailAST parent = Objects.requireNonNullElse(token, warningHolder);
212            DetailAST warning = parent.findFirstToken(TokenTypes.EXPR);
213
214            // rare case with empty array ex: @SuppressWarnings({})
215            if (warning == null) {
216                // check to see if empty warnings are forbidden -- are by default
217                logMatch(warningHolder, "");
218            }
219            else {
220                while (warning != null) {
221                    if (warning.getType() == TokenTypes.EXPR) {
222                        final DetailAST fChild = warning.getFirstChild();
223                        switch (fChild.getType()) {
224                            // typical case
225                            case TokenTypes.STRING_LITERAL:
226                                final String warningText =
227                                    removeQuotes(warning.getFirstChild().getText());
228                                logMatch(warning, warningText);
229                                break;
230                            // conditional case
231                            // ex:
232                            // @SuppressWarnings((false) ? (true) ? "unchecked" : "foo" : "unused")
233                            case TokenTypes.QUESTION:
234                                walkConditional(fChild);
235                                break;
236                            default:
237                                // Known limitation: cases like @SuppressWarnings("un" + "used") or
238                                // @SuppressWarnings((String) "unused") are not properly supported,
239                                // but they should not cause exceptions.
240                                // Also constant as param
241                                // ex: public static final String UNCHECKED = "unchecked";
242                                // @SuppressWarnings(UNCHECKED)
243                                // or
244                                // @SuppressWarnings(SomeClass.UNCHECKED)
245                        }
246                    }
247                    warning = warning.getNextSibling();
248                }
249            }
250        }
251    }
252
253    /**
254     * Gets the {@link SuppressWarnings SuppressWarnings} annotation
255     * that is annotating the AST.  If the annotation does not exist
256     * this method will return {@code null}.
257     *
258     * @param ast the AST
259     * @return the {@link SuppressWarnings SuppressWarnings} annotation
260     */
261    private static DetailAST getSuppressWarnings(DetailAST ast) {
262        DetailAST annotation = AnnotationUtil.getAnnotation(ast, SUPPRESS_WARNINGS);
263
264        if (annotation == null) {
265            annotation = AnnotationUtil.getAnnotation(ast, FQ_SUPPRESS_WARNINGS);
266        }
267        return annotation;
268    }
269
270    /**
271     * This method looks for a warning that matches a configured expression.
272     * If found it logs a violation at the given AST.
273     *
274     * @param ast the location to place the violation
275     * @param warningText the warning.
276     */
277    private void logMatch(DetailAST ast, final String warningText) {
278        final Matcher matcher = format.matcher(warningText);
279        if (matcher.matches()) {
280            log(ast,
281                    MSG_KEY_SUPPRESSED_WARNING_NOT_ALLOWED, warningText);
282        }
283    }
284
285    /**
286     * Find the parent (holder) of the of the warnings (Expr).
287     *
288     * @param annotation the annotation
289     * @return a Token representing the expr.
290     */
291    private static DetailAST findWarningsHolder(final DetailAST annotation) {
292        final DetailAST annValuePair =
293            annotation.findFirstToken(TokenTypes.ANNOTATION_MEMBER_VALUE_PAIR);
294
295        final DetailAST annArrayInitParent = Objects.requireNonNullElse(annValuePair, annotation);
296        final DetailAST annArrayInit = annArrayInitParent
297                .findFirstToken(TokenTypes.ANNOTATION_ARRAY_INIT);
298        return Objects.requireNonNullElse(annArrayInit, annotation);
299    }
300
301    /**
302     * Strips a single double quote from the front and back of a string.
303     *
304     * <p>For example:</p>
305     * <pre>
306     * Input String = "unchecked"
307     * </pre>
308     * Output String = unchecked
309     *
310     * @param warning the warning string
311     * @return the string without two quotes
312     */
313    private static String removeQuotes(final String warning) {
314        return warning.substring(1, warning.length() - 1);
315    }
316
317    /**
318     * Walks a conditional expression checking the left
319     * and right sides, checking for matches and
320     * logging violations.
321     *
322     * @param cond a Conditional type
323     *     {@link TokenTypes#QUESTION QUESTION}
324     */
325    private void walkConditional(final DetailAST cond) {
326        final Deque<DetailAST> condStack = new ArrayDeque<>();
327        condStack.push(cond);
328
329        while (!condStack.isEmpty()) {
330            final DetailAST currentCond = condStack.pop();
331            if (currentCond.getType() == TokenTypes.QUESTION) {
332                condStack.push(getCondRight(currentCond));
333                condStack.push(getCondLeft(currentCond));
334            }
335            else {
336                final String warningText = removeQuotes(currentCond.getText());
337                logMatch(currentCond, warningText);
338            }
339        }
340    }
341
342    /**
343     * Retrieves the left side of a conditional.
344     *
345     * @param cond cond a conditional type
346     *     {@link TokenTypes#QUESTION QUESTION}
347     * @return either the value
348     *     or another conditional
349     */
350    private static DetailAST getCondLeft(final DetailAST cond) {
351        final DetailAST colon = cond.findFirstToken(TokenTypes.COLON);
352        return colon.getPreviousSibling();
353    }
354
355    /**
356     * Retrieves the right side of a conditional.
357     *
358     * @param cond a conditional type
359     *     {@link TokenTypes#QUESTION QUESTION}
360     * @return either the value
361     *     or another conditional
362     */
363    private static DetailAST getCondRight(final DetailAST cond) {
364        final DetailAST colon = cond.findFirstToken(TokenTypes.COLON);
365        return colon.getNextSibling();
366    }
367
368}