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.filters;
021
022import com.puppycrawl.tools.checkstyle.api.AuditEvent;
023import com.puppycrawl.tools.checkstyle.api.AutomaticBean;
024import com.puppycrawl.tools.checkstyle.api.Filter;
025import com.puppycrawl.tools.checkstyle.checks.SuppressWarningsHolder;
026
027/**
028 * <p>
029 * Filter {@code SuppressWarningsFilter} uses annotation
030 * {@code SuppressWarnings} to suppress audit events.
031 * </p>
032 * <p>
033 * Rationale: Same as for {@code SuppressionCommentFilter}. In the contrary to it here,
034 * comments are not used comments but the builtin syntax of {@code @SuppressWarnings}.
035 * This can be perceived as a more elegant solution than using comments.
036 * Also this approach maybe supported by various IDE.
037 * </p>
038 * <p>
039 * Usage: This filter only works in conjunction with a
040 * <a href="https://checkstyle.org/config_annotation.html#SuppressWarningsHolder">
041 * SuppressWarningsHolder</a>,
042 * since that check finds the annotations in the Java files and makes them available for the filter.
043 * Because of that, a configuration that includes this filter must also include
044 * {@code SuppressWarningsHolder} as a child module of the {@code TreeWalker}.
045 * Name of check in annotation is case-insensitive and should be written with
046 * any dotted prefix or "Check" suffix removed.
047 * </p>
048 * <p>
049 * SuppressWarningsFilter can suppress Checks that have Treewalker or
050 * Checker as parent module.
051 * </p>
052 * <p>
053 * To configure the check that makes tha annotations available to the filter.
054 * </p>
055 * <pre>
056 * &lt;module name=&quot;TreeWalker&quot;&gt;
057 *               ...
058 * &lt;module name=&quot;SuppressWarningsHolder&quot; /&gt;
059 *               ...
060 * &lt;/module&gt;
061 * </pre>
062 * <p>
063 * To configure filter to suppress audit events for annotations add:
064 * </p>
065 * <pre>
066 * &lt;module name=&quot;SuppressWarningsFilter&quot; /&gt;
067 * </pre>
068 * <pre>
069 * &#64;SuppressWarnings({"memberName"})
070 * private int J; // should NOT fail MemberNameCheck
071 *
072 * &#64;SuppressWarnings({"MemberName"})
073 * &#64;SuppressWarnings({"NoWhitespaceAfter"})
074 * private int [] ARRAY; // should NOT fail MemberNameCheck and NoWhitespaceAfterCheck
075 * </pre>
076 * <p>
077 * It is possible to specify an ID of checks, so that it can be leveraged by
078 * the SuppressWarningsFilter to skip validations. The following examples show how to
079 * skip validations near code that has {@code @SuppressWarnings("checkstyle:&lt;ID&gt;")}
080 * or just {@code @SuppressWarnings("&lt;ID&gt;")} annotation, where ID is the ID
081 * of checks you want to suppress.
082 * </p>
083 * <p>
084 * Example of Checkstyle check configuration:
085 * </p>
086 * <pre>
087 * &lt;module name=&quot;RegexpSinglelineJava&quot;&gt;
088 *   &lt;property name=&quot;id&quot; value=&quot;systemout&quot;/&gt;
089 *   &lt;property name=&quot;format&quot; value=&quot;^.*System\.(out|err).*$&quot;/&gt;
090 *   &lt;property name=&quot;message&quot;
091 *     value=&quot;Don't use System.out/err, use SLF4J instead.&quot;/&gt;
092 * &lt;/module&gt;
093 * </pre>
094 * <p>
095 * To make the annotations available to the filter.
096 * </p>
097 * <pre>
098 * &lt;module name=&quot;TreeWalker&quot;&gt;
099 *   ...
100 *   &lt;module name=&quot;SuppressWarningsHolder&quot; /&gt;
101 *   ...
102 * &lt;/module&gt;
103 * </pre>
104 * <p>
105 * To configure filter to suppress audit events for annotations add:
106 * </p>
107 * <pre>
108 * &lt;module name=&quot;SuppressWarningsFilter&quot; /&gt;
109 * </pre>
110 * <pre>
111 * &#64;SuppressWarnings("checkstyle:systemout")
112 * public static void foo() {
113 *   System.out.println("Debug info."); // should NOT fail RegexpSinglelineJava
114 * }
115 * </pre>
116 * <p>
117 * Parent is {@code com.puppycrawl.tools.checkstyle.Checker}
118 * </p>
119 *
120 * @since 5.7
121 */
122public class SuppressWarningsFilter
123    extends AutomaticBean
124    implements Filter {
125
126    @Override
127    protected void finishLocalSetup() {
128        // No code by default
129    }
130
131    @Override
132    public boolean accept(AuditEvent event) {
133        return !SuppressWarningsHolder.isSuppressed(event);
134    }
135
136}