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 java.util.regex.Pattern;
23  
24  import com.puppycrawl.tools.checkstyle.AbstractAutomaticBean;
25  import com.puppycrawl.tools.checkstyle.api.AuditEvent;
26  import com.puppycrawl.tools.checkstyle.api.Filter;
27  
28  /**
29   * <p>
30   * Filter {@code SuppressionSingleFilter} suppresses audit events for Checks violations in the
31   * specified file, class, checks, message, module id, lines, and columns.
32   * </p>
33   * <p>
34   * Rationale: To allow users use suppressions configured in the same config with other modules.
35   * SuppressionFilter and SuppressionXpathFilter are require separate file.
36   * </p>
37   * <p>
38   * Advice: If checkstyle configuration is used for several projects, single suppressions on common
39   * files/folders is better to put in checkstyle configuration as common rule. All suppression that
40   * are for specific file names is better to keep in project specific config file.
41   * </p>
42   * <p>
43   * Attention: This filter only supports single suppression, and will need multiple instances if
44   * users wants to suppress multiple violations.
45   * </p>
46   * <p>
47   * SuppressionSingleFilter can suppress Checks that have Treewalker or
48   * Checker as parent module.
49   * </p>
50   * <ul>
51   * <li>
52   * Property {@code checks} - Define the RegExp for matching against the name of the check
53   * associated with an audit event.
54   * Type is {@code java.util.regex.Pattern}.
55   * Default value is {@code null}.
56   * </li>
57   * <li>
58   * Property {@code columns} - Specify a comma-separated list of values, where each value is an
59   * integer or a range of integers denoted by integer-integer.
60   * Type is {@code java.lang.String}.
61   * Default value is {@code null}.
62   * </li>
63   * <li>
64   * Property {@code files} - Define the RegExp for matching against the file name associated with
65   * an audit event.
66   * Type is {@code java.util.regex.Pattern}.
67   * Default value is {@code null}.
68   * </li>
69   * <li>
70   * Property {@code id} - Specify a string matched against the ID of the check associated with
71   * an audit event.
72   * Type is {@code java.lang.String}.
73   * Default value is {@code null}.
74   * </li>
75   * <li>
76   * Property {@code lines} - Specify a comma-separated list of values, where each value is an
77   * integer or a range of integers denoted by integer-integer.
78   * Type is {@code java.lang.String}.
79   * Default value is {@code null}.
80   * </li>
81   * <li>
82   * Property {@code message} - Define the RegExp for matching against the message of the check
83   * associated with an audit event.
84   * Type is {@code java.util.regex.Pattern}.
85   * Default value is {@code null}.
86   * </li>
87   * </ul>
88   * <p>
89   * Parent is {@code com.puppycrawl.tools.checkstyle.Checker}
90   * </p>
91   *
92   * @since 8.23
93   */
94  public class SuppressionSingleFilter extends AbstractAutomaticBean implements Filter {
95  
96      /**
97       * SuppressFilterElement instance.
98       */
99      private SuppressFilterElement filter;
100     /**
101      * Define the RegExp for matching against the file name associated with an audit event.
102      */
103     private Pattern files;
104     /**
105      * Define the RegExp for matching against the name of the check associated with an audit event.
106      */
107     private Pattern checks;
108     /**
109      * Define the RegExp for matching against the message of the check associated with an audit
110      * event.
111      */
112     private Pattern message;
113     /**
114      * Specify a string matched against the ID of the check associated with an audit event.
115      */
116     private String id;
117     /**
118      * Specify a comma-separated list of values, where each value is an integer or a range of
119      * integers denoted by integer-integer.
120      */
121     private String lines;
122     /**
123      * Specify a comma-separated list of values, where each value is an integer or a range of
124      * integers denoted by integer-integer.
125      */
126     private String columns;
127 
128     /**
129      * Setter to define the RegExp for matching against the file name associated with an audit
130      * event.
131      *
132      * @param files regular expression for filtered file names
133      * @since 8.23
134      */
135     public void setFiles(Pattern files) {
136         this.files = files;
137     }
138 
139     /**
140      * Setter to define the RegExp for matching against the name of the check associated with an
141      * audit event.
142      *
143      * @param checks the name of the check
144      * @since 8.23
145      */
146     public void setChecks(String checks) {
147         this.checks = Pattern.compile(checks);
148     }
149 
150     /**
151      * Setter to define the RegExp for matching against the message of the check associated with
152      * an audit event.
153      *
154      * @param message the message of the check
155      * @since 8.23
156      */
157     public void setMessage(Pattern message) {
158         this.message = message;
159     }
160 
161     /**
162      * Setter to specify a string matched against the ID of the check associated with an audit
163      * event.
164      *
165      * @param id the ID of the check
166      * @since 8.23
167      */
168     public void setId(String id) {
169         this.id = id;
170     }
171 
172     /**
173      * Setter to specify a comma-separated list of values, where each value is an integer or a
174      * range of integers denoted by integer-integer.
175      *
176      * @param lines the lines of the check
177      * @since 8.23
178      */
179     public void setLines(String lines) {
180         this.lines = lines;
181     }
182 
183     /**
184      * Setter to specify a comma-separated list of values, where each value is an integer or a
185      * range of integers denoted by integer-integer.
186      *
187      * @param columns the columns of the check
188      * @since 8.23
189      */
190     public void setColumns(String columns) {
191         this.columns = columns;
192     }
193 
194     @Override
195     protected void finishLocalSetup() {
196         filter = new SuppressFilterElement(files, checks, message, id, lines, columns);
197     }
198 
199     @Override
200     public boolean accept(AuditEvent event) {
201         return filter.accept(event);
202     }
203 
204 }