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.api.SeverityLevel; 26 27 /** 28 * <p> 29 * Filter {@code SeverityMatchFilter} decides audit events according to the 30 * <a href="https://checkstyle.org/config.html#Severity">severity level</a> of the event. 31 * </p> 32 * <p> 33 * SeverityMatchFilter can suppress Checks that have Treewalker or Checker as parent module. 34 * </p> 35 * <ul> 36 * <li> 37 * Property {@code acceptOnMatch} - Control whether the filter accepts an audit 38 * event if and only if there is a match between the event's severity level and 39 * property severity. If acceptOnMatch is {@code false}, then the filter accepts 40 * an audit event if and only if there is not a match between the event's severity 41 * level and property severity. 42 * Type is {@code boolean}. 43 * Default value is {@code true}. 44 * </li> 45 * <li> 46 * Property {@code severity} - Specify the severity level of this filter. 47 * Type is {@code com.puppycrawl.tools.checkstyle.api.SeverityLevel}. 48 * Default value is {@code error}. 49 * </li> 50 * </ul> 51 * <p> 52 * Parent is {@code com.puppycrawl.tools.checkstyle.Checker} 53 * </p> 54 * 55 * @since 3.2 56 */ 57 public class SeverityMatchFilter 58 extends AbstractAutomaticBean 59 implements Filter { 60 61 /** Specify the severity level of this filter. */ 62 private SeverityLevel severity = SeverityLevel.ERROR; 63 64 /** 65 * Control whether the filter accepts an audit event if and only if there 66 * is a match between the event's severity level and property severity. 67 * If acceptOnMatch is {@code false}, then the filter accepts an audit event 68 * if and only if there is not a match between the event's severity level 69 * and property severity. 70 */ 71 private boolean acceptOnMatch = true; 72 73 /** 74 * Setter to specify the severity level of this filter. 75 * 76 * @param severity The new severity level 77 * @see SeverityLevel 78 * @since 3.2 79 */ 80 public final void setSeverity(SeverityLevel severity) { 81 this.severity = severity; 82 } 83 84 /** 85 * Setter to control whether the filter accepts an audit event if and only if there 86 * is a match between the event's severity level and property severity. 87 * If acceptOnMatch is {@code false}, then the filter accepts an audit event 88 * if and only if there is not a match between the event's severity level and property severity. 89 * 90 * @param acceptOnMatch if true, accept on matches; if 91 * false, reject on matches. 92 * @since 3.2 93 */ 94 public final void setAcceptOnMatch(boolean acceptOnMatch) { 95 this.acceptOnMatch = acceptOnMatch; 96 } 97 98 @Override 99 protected void finishLocalSetup() { 100 // No code by default 101 } 102 103 @Override 104 public boolean accept(AuditEvent event) { 105 final boolean severityMatches = severity == event.getSeverityLevel(); 106 return acceptOnMatch == severityMatches; 107 } 108 109 }