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.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 * <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/checks/annotation/suppresswarningsholder.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 * Parent is {@code com.puppycrawl.tools.checkstyle.Checker}
054 * </p>
055 *
056 * @since 5.7
057 */
058public class SuppressWarningsFilter
059    extends AbstractAutomaticBean
060    implements Filter {
061
062    @Override
063    protected void finishLocalSetup() {
064        // No code by default
065    }
066
067    @Override
068    public boolean accept(AuditEvent event) {
069        return !SuppressWarningsHolder.isSuppressed(event);
070    }
071
072}