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