001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2026 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.AbstractAutomaticBean; 023import com.puppycrawl.tools.checkstyle.api.AuditEvent; 024import com.puppycrawl.tools.checkstyle.api.Filter; 025import com.puppycrawl.tools.checkstyle.checks.SuppressWarningsHolder; 026 027/** 028 * <div> 029 * Filter {@code SuppressWarningsFilter} uses annotation 030 * {@code @SuppressWarnings} to suppress audit events. 031 * </div> 032 * 033 * <p> 034 * Rationale: Same as for {@code SuppressionCommentFilter}. In the contrary to it here, 035 * comments are not used comments but the builtin syntax of {@code @SuppressWarnings}. 036 * This can be perceived as a more elegant solution than using comments. 037 * Also, this approach maybe supported by various IDE. 038 * </p> 039 * 040 * <p> 041 * Usage: This filter only works in conjunction with a 042 * <a href="https://checkstyle.org/checks/annotation/suppresswarningsholder.html"> 043 * SuppressWarningsHolder</a>, 044 * since that check finds the annotations in the Java files and makes them available for the filter. 045 * Because of that, a configuration that includes this filter must also include 046 * {@code SuppressWarningsHolder} as a child module of the {@code TreeWalker}. 047 * Name of check in annotation is case-insensitive and should be written with 048 * any dotted prefix or "Check" suffix removed. 049 * </p> 050 * 051 * <p> 052 * Notes: 053 * SuppressWarningsFilter can suppress Checks that have Treewalker or Checker as parent module. 054 * </p> 055 * 056 * @since 5.7 057 */ 058public class SuppressWarningsFilter 059 extends AbstractAutomaticBean 060 implements Filter { 061 062 /** 063 * Creates a new {@code SuppressWarningsFilter} instance. 064 */ 065 public SuppressWarningsFilter() { 066 // no code by default 067 } 068 069 @Override 070 protected void finishLocalSetup() { 071 // No code by default 072 } 073 074 @Override 075 public boolean accept(AuditEvent event) { 076 return !SuppressWarningsHolder.isSuppressed(event); 077 } 078 079}