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 * <p> 29 * Filter {@code SuppressWarningsFilter} uses annotation 30 * {@code @SuppressWarnings} to suppress audit events. 31 * </p> 32 * <p> 33 * Rationale: Same as for {@code SuppressionCommentFilter}. In the contrary to it here, 34 * comments are not used comments but the builtin syntax of {@code @SuppressWarnings}. 35 * This can be perceived as a more elegant solution than using comments. 36 * Also, this approach maybe supported by various IDE. 37 * </p> 38 * <p> 39 * Usage: This filter only works in conjunction with a 40 * <a href="https://checkstyle.org/checks/annotation/suppresswarningsholder.html#SuppressWarningsHolder"> 41 * SuppressWarningsHolder</a>, 42 * since that check finds the annotations in the Java files and makes them available for the filter. 43 * Because of that, a configuration that includes this filter must also include 44 * {@code SuppressWarningsHolder} as a child module of the {@code TreeWalker}. 45 * Name of check in annotation is case-insensitive and should be written with 46 * any dotted prefix or "Check" suffix removed. 47 * </p> 48 * <p> 49 * SuppressWarningsFilter can suppress Checks that have Treewalker or 50 * Checker as parent module. 51 * </p> 52 * <p> 53 * Parent is {@code com.puppycrawl.tools.checkstyle.Checker} 54 * </p> 55 * 56 * @since 5.7 57 */ 58 public class SuppressWarningsFilter 59 extends AbstractAutomaticBean 60 implements Filter { 61 62 @Override 63 protected void finishLocalSetup() { 64 // No code by default 65 } 66 67 @Override 68 public boolean accept(AuditEvent event) { 69 return !SuppressWarningsHolder.isSuppressed(event); 70 } 71 72 }