1 ///////////////////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3 // Copyright (C) 2001-2025 the original author or authors.
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ///////////////////////////////////////////////////////////////////////////////////////////////
19
20 package com.puppycrawl.tools.checkstyle.filters;
21
22 import com.puppycrawl.tools.checkstyle.AbstractAutomaticBean;
23 import com.puppycrawl.tools.checkstyle.api.AuditEvent;
24 import com.puppycrawl.tools.checkstyle.api.Filter;
25 import com.puppycrawl.tools.checkstyle.checks.SuppressWarningsHolder;
26
27 /**
28 * <div>
29 * Filter {@code SuppressWarningsFilter} uses annotation
30 * {@code @SuppressWarnings} to suppress audit events.
31 * </div>
32 *
33 * <p>
34 * Rationale: Same as for {@code SuppressionCommentFilter}. In the contrary to it here,
35 * comments are not used comments but the builtin syntax of {@code @SuppressWarnings}.
36 * This can be perceived as a more elegant solution than using comments.
37 * Also, this approach maybe supported by various IDE.
38 * </p>
39 *
40 * <p>
41 * Usage: This filter only works in conjunction with a
42 * <a href="https://checkstyle.org/checks/annotation/suppresswarningsholder.html">
43 * SuppressWarningsHolder</a>,
44 * since that check finds the annotations in the Java files and makes them available for the filter.
45 * Because of that, a configuration that includes this filter must also include
46 * {@code SuppressWarningsHolder} as a child module of the {@code TreeWalker}.
47 * Name of check in annotation is case-insensitive and should be written with
48 * any dotted prefix or "Check" suffix removed.
49 * </p>
50 *
51 * <p>
52 * Notes:
53 * SuppressWarningsFilter can suppress Checks that have Treewalker or
54 * Checker as parent module.
55 * </p>
56 *
57 * @since 5.7
58 */
59 public class SuppressWarningsFilter
60 extends AbstractAutomaticBean
61 implements Filter {
62
63 @Override
64 protected void finishLocalSetup() {
65 // No code by default
66 }
67
68 @Override
69 public boolean accept(AuditEvent event) {
70 return !SuppressWarningsHolder.isSuppressed(event);
71 }
72
73 }