View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 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#SuppressWarningsHolder">
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   * SuppressWarningsFilter can suppress Checks that have Treewalker or
53   * Checker as parent module.
54   * </p>
55   *
56   * <p>
57   * Parent is {@code com.puppycrawl.tools.checkstyle.Checker}
58   * </p>
59   *
60   * @since 5.7
61   */
62  public class SuppressWarningsFilter
63      extends AbstractAutomaticBean
64      implements Filter {
65  
66      @Override
67      protected void finishLocalSetup() {
68          // No code by default
69      }
70  
71      @Override
72      public boolean accept(AuditEvent event) {
73          return !SuppressWarningsHolder.isSuppressed(event);
74      }
75  
76  }