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 * <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#SuppressWarningsHolder">
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 * SuppressWarningsFilter can suppress Checks that have Treewalker or
053 * Checker as parent module.
054 * </p>
055 *
056 * <p>
057 * Parent is {@code com.puppycrawl.tools.checkstyle.Checker}
058 * </p>
059 *
060 * @since 5.7
061 */
062public class SuppressWarningsFilter
063    extends AbstractAutomaticBean
064    implements Filter {
065
066    @Override
067    protected void finishLocalSetup() {
068        // No code by default
069    }
070
071    @Override
072    public boolean accept(AuditEvent event) {
073        return !SuppressWarningsHolder.isSuppressed(event);
074    }
075
076}