/
SuppressionFilter.java
273 lines (262 loc) · 10 KB
/
SuppressionFilter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code for adherence to a set of rules.
// Copyright (C) 2001-2019 the original author or authors.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
////////////////////////////////////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle.filters;
import java.util.Collections;
import java.util.Set;
import com.puppycrawl.tools.checkstyle.api.AuditEvent;
import com.puppycrawl.tools.checkstyle.api.AutomaticBean;
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
import com.puppycrawl.tools.checkstyle.api.ExternalResourceHolder;
import com.puppycrawl.tools.checkstyle.api.Filter;
import com.puppycrawl.tools.checkstyle.api.FilterSet;
import com.puppycrawl.tools.checkstyle.utils.FilterUtil;
/**
* <p>
* Filter {@code SuppressionFilter} rejects audit events for Check errors according to a
* <a href="https://checkstyle.org/dtds/suppressions_1_2.dtd">suppressions XML document</a>
* in a file. If there is no configured suppressions file or the optional is set to true and
* suppressions file was not found the Filter accepts all audit events.
* </p>
* <p>
* A <a href="https://checkstyle.org/dtds/suppressions_1_2.dtd">suppressions XML document</a>
* contains a set of {@code suppress} elements, where each {@code suppress}
* element can have the following attributes:
* </p>
* <ul>
* <li>
* {@code files} - a <a href="https://checkstyle.org/property_types.html#regexp">
* Regular Expression</a> matched against the file name associated with an audit event.
* It is optional.
* </li>
* <li>
* {@code checks} - a <a href="https://checkstyle.org/property_types.html#regexp">
* Regular Expression</a> matched against the name of the check associated with an audit event.
* Optional as long as {@code id} or {@code message} is specified.
* </li>
* <li>
* {@code message} - a <a href="https://checkstyle.org/property_types.html#regexp">
* Regular Expression</a> matched against the message of the check associated with an audit event.
* Optional as long as {@code checks} or {@code id} is specified.
* </li>
* <li>
* {@code id} - a <a href="https://checkstyle.org/property_types.html#string">string</a>
* matched against the <a href="config.html#Id">check id</a> associated with an audit event.
* Optional as long as {@code checks} or {@code message} is specified.
* </li>
* <li>
* {@code lines} - a comma-separated list of values, where each value is an
* <a href="https://checkstyle.org/property_types.html#integer">integer</a>
* or a range of integers denoted by integer-integer.
* It is optional.
* </li>
* <li>
* {@code columns} - a comma-separated list of values, where each value is an
* <a href="https://checkstyle.org/property_types.html#integer">integer</a>
* or a range of integers denoted by integer-integer.
* It is optional.
* </li>
* </ul>
* <p>
* Each audit event is checked against each {@code suppress} element.
* It is suppressed if all specified attributes match against the audit event.
* </p>
* <p>
* ATTENTION: filtering by message is dependant on runtime locale.
* If project is running in different languages it is better to avoid filtering by message.
* </p>
* <p>
* You can download template of empty suppression filter
* <a href="https://checkstyle.org/files/suppressions_none.xml">here</a>.
* </p>
* <p>
* Location of the file defined in {@code file} property is checked in the following order:
* </p>
* <ol>
* <li>
* as a filesystem location
* </li>
* <li>
* if no file found, and the location starts with either {@code http://} or {@code https://},
* then it is interpreted as a URL
* </li>
* <li>
* if no file found, then passed to the {@code ClassLoader.getResource()} method.
* </li>
* </ol>
* <ul>
* <li>
* Property {@code file} - Specify the location of the <em>suppressions XML document</em> file.
* Default value is {@code null}.
* </li>
* <li>
* Property {@code optional} - Control what to do when the file is not existing.
* If {@code optional} is set to {@code false} the file must exist, or else it
* ends with error. On the other hand if optional is {@code true} and file is
* not found, the filter accept all audit events.
* Default value is {@code false}.
* </li>
* </ul>
* <p>
* For example, the following configuration fragment directs the Checker to use
* a {@code SuppressionFilter} with suppressions file {@code config/suppressions.xml}:
* </p>
* <pre>
* <module name="SuppressionFilter">
* <property name="file" value="config/suppressions.xml"/>
* <property name="optional" value="false"/>
* </module>
* </pre>
* <p>
* The following suppressions XML document directs a {@code SuppressionFilter} to
* reject {@code JavadocStyleCheck} errors for lines 82 and 108 to 122 of file
* {@code AbstractComplexityCheck.java}, and {@code MagicNumberCheck} errors for
* line 221 of file {@code JavadocStyleCheck.java}, and
* {@code 'Missing a Javadoc comment'} errors for all lines and files:
* </p>
* <pre>
* <?xml version="1.0"?>
*
* <!DOCTYPE suppressions PUBLIC
* "-//Checkstyle//DTD SuppressionFilter Configuration 1.2//EN"
* "https://checkstyle.org/dtds/suppressions_1_2.dtd">
*
* <suppressions>
* <suppress checks="JavadocStyleCheck"
* files="AbstractComplexityCheck.java"
* lines="82,108-122"/>
* <suppress checks="MagicNumberCheck"
* files="JavadocStyleCheck.java"
* lines="221"/>
* <suppress message="Missing a Javadoc comment"/>
* </suppressions>
* </pre>
* <p>
* Suppress check by <a href="https://checkstyle.org/config.html#Id">module id</a>
* when config have two instances on the same check:
* </p>
* <pre>
* <suppress id="stringEqual" files="SomeTestCode.java"/>
* </pre>
* <p>
* Suppress all checks for hidden files and folders:
* </p>
* <pre>
* <suppress files="[/\\]\..+" checks=".*"/>
* </pre>
* <p>
* Suppress all checks for Maven-generated code:
* </p>
* <pre>
* <suppress files="[/\\]target[/\\]" checks=".*"/>
* </pre>
* <p>
* Suppress all checks for archives, classes and other binary files:
* </p>
* <pre>
* <suppress files=".+\.(?:jar|zip|war|class|tar|bin)$" checks=".*"/>
* </pre>
* <p>
* Suppress all checks for image files:
* </p>
* <pre>
* <suppress files=".+\.(?:png|gif|jpg|jpeg)$" checks=".*"/>
* </pre>
* <p>
* Suppress all checks for non-java files:
* </p>
* <pre>
* <suppress files=".+\.(?:txt|xml|csv|sh|thrift|html|sql|eot|ttf|woff|css|png)$"
* checks=".*"/>
* </pre>
* <p>
* Suppress all checks in generated sources:
* </p>
* <pre>
* <suppress checks=".*" files="com[\\/]mycompany[\\/]app[\\/]gen[\\/]"/>
* </pre>
* <p>
* Suppress FileLength check on integration tests in certain folder:
* </p>
* <pre>
* <suppress checks="FileLength"
* files="com[\\/]mycompany[\\/]app[\\/].*IT.java"/>
* </pre>
* <p>
* Suppress naming errors on variable named 'log' in all files:
* </p>
* <pre>
* <suppress message="Name 'log' must match pattern"/>
* </pre>
*
* @since 3.2
*/
public class SuppressionFilter extends AutomaticBean implements Filter, ExternalResourceHolder {
/** Specify the location of the <em>suppressions XML document</em> file. */
private String file;
/**
* Control what to do when the file is not existing. If {@code optional} is
* set to {@code false} the file must exist, or else it ends with error.
* On the other hand if optional is {@code true} and file is not found,
* the filter accept all audit events.
*/
private boolean optional;
/** Set of individual suppresses. */
private FilterSet filters = new FilterSet();
/**
* Setter to specify the location of the <em>suppressions XML document</em> file.
* @param fileName name of the suppressions file.
*/
public void setFile(String fileName) {
file = fileName;
}
/**
* Setter to control what to do when the file is not existing.
* If {@code optional} is set to {@code false} the file must exist, or else
* it ends with error. On the other hand if optional is {@code true}
* and file is not found, the filter accept all audit events.
* @param optional tells if config file existence is optional.
*/
public void setOptional(boolean optional) {
this.optional = optional;
}
@Override
public boolean accept(AuditEvent event) {
return filters.accept(event);
}
@Override
protected void finishLocalSetup() throws CheckstyleException {
if (file != null) {
if (optional) {
if (FilterUtil.isFileExists(file)) {
filters = SuppressionsLoader.loadSuppressions(file);
}
else {
filters = new FilterSet();
}
}
else {
filters = SuppressionsLoader.loadSuppressions(file);
}
}
}
@Override
public Set<String> getExternalResourceLocations() {
return Collections.singleton(file);
}
}