001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2020 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.api.AuditEvent; 023import com.puppycrawl.tools.checkstyle.api.AutomaticBean; 024import com.puppycrawl.tools.checkstyle.api.Filter; 025import com.puppycrawl.tools.checkstyle.api.SeverityLevel; 026 027/** 028 * <p> 029 * Filter {@code SeverityMatchFilter} decides audit events according to the 030 * <a href="https://checkstyle.org/config.html#Severity">severity level</a> of the event. 031 * </p> 032 * <p> 033 * SeverityMatchFilter can suppress Checks that have Treewalker or Checker as parent module. 034 * </p> 035 * <ul> 036 * <li> 037 * Property {@code severity} - Specify the severity level of this filter. 038 * Type is {@code com.puppycrawl.tools.checkstyle.api.SeverityLevel}. 039 * Default value is {@code error}. 040 * </li> 041 * <li> 042 * Property {@code acceptOnMatch} - Control whether the filter accepts an audit 043 * event if and only if there is a match between the event's severity level and 044 * property severity. If acceptOnMatch is {@code false}, then the filter accepts 045 * an audit event if and only if there is not a match between the event's severity 046 * level and property severity. 047 * Type is {@code boolean}. 048 * Default value is {@code true}. 049 * </li> 050 * </ul> 051 * <p> 052 * For example, the following configuration fragment directs the Checker to not 053 * report audit events with severity level {@code info}: 054 * </p> 055 * <pre> 056 * <module name="SeverityMatchFilter"> 057 * <property name="severity" value="info"/> 058 * <property name="acceptOnMatch" value="false"/> 059 * </module> 060 * </pre> 061 * <p> 062 * Parent is {@code com.puppycrawl.tools.checkstyle.Checker} 063 * </p> 064 * 065 * @since 3.2 066 */ 067public class SeverityMatchFilter 068 extends AutomaticBean 069 implements Filter { 070 071 /** Specify the severity level of this filter. */ 072 private SeverityLevel severity = SeverityLevel.ERROR; 073 074 /** 075 * Control whether the filter accepts an audit event if and only if there 076 * is a match between the event's severity level and property severity. 077 * If acceptOnMatch is {@code false}, then the filter accepts an audit event 078 * if and only if there is not a match between the event's severity level 079 * and property severity. 080 */ 081 private boolean acceptOnMatch = true; 082 083 /** 084 * Setter to specify the severity level of this filter. 085 * 086 * @param severity The new severity level 087 * @see SeverityLevel 088 */ 089 public final void setSeverity(SeverityLevel severity) { 090 this.severity = severity; 091 } 092 093 /** 094 * Setter to control whether the filter accepts an audit event if and only if there 095 * is a match between the event's severity level and property severity. 096 * If acceptOnMatch is {@code false}, then the filter accepts an audit event 097 * if and only if there is not a match between the event's severity level and property severity. 098 * 099 * @param acceptOnMatch if true, accept on matches; if 100 * false, reject on matches. 101 */ 102 public final void setAcceptOnMatch(boolean acceptOnMatch) { 103 this.acceptOnMatch = acceptOnMatch; 104 } 105 106 @Override 107 protected void finishLocalSetup() { 108 // No code by default 109 } 110 111 @Override 112 public boolean accept(AuditEvent event) { 113 final boolean severityMatches = severity == event.getSeverityLevel(); 114 return acceptOnMatch == severityMatches; 115 } 116 117}