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