-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathwritingfilters.html
More file actions
407 lines (346 loc) · 17 KB
/
writingfilters.html
File metadata and controls
407 lines (346 loc) · 17 KB
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
<!DOCTYPE html>
<!--
| Generated by Apache Maven Doxia Site Renderer 2.0.0 from src/site/xdoc/writingfilters.xml at 2026-05-02
| Rendered using Apache Maven Fluido Skin 2.0.1
-->
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="generator" content="Apache Maven Doxia Site Renderer 2.0.0" />
<title>Writing Filters – checkstyle</title>
<link rel="stylesheet" href="./css/apache-maven-fluido-2.0.1.min.css" />
<link rel="stylesheet" href="./css/site.css" />
<link rel="stylesheet" href="./css/print.css" media="print" />
<script src="./js/apache-maven-fluido-2.0.1.min.js"></script>
<script type="text/javascript" src="./js/checkstyle.js" defer async></script>
<script type="text/javascript" src="./js/anchors.js" defer async></script>
<script type="text/javascript"
src="./js/google-analytics.js" defer async></script>
<script type="text/javascript"
src="./js/copy-clipboard.js" defer async></script>
<link rel="icon" href="./images/favicon.png" type="image/x-icon" />
<link rel="shortcut icon" href="./images/favicon.ico" type="image/ico" />
</head>
<body>
<div class="container-fluid container-fluid-top">
<header>
<div id="banner">
<div class="pull-left"><div id="bannerLeft"><h1><a href="./"><img class="class java.lang.Object" src="images/header-checkstyle-logo.png" alt="Checkstyle" /></a></h1></div></div>
<div class="pull-right"><div id="bannerRight"><h1><img class="class java.lang.Object" src="images/header-right-ruler.png" /></h1></div></div>
<div class="clear"><hr/></div>
</div>
<div id="breadcrumbs">
<ul class="breadcrumb">
<li id="publishDate" class="pull-right"><span class="divider">|</span> Last Published: 2026-05-02</li>
<li id="projectVersion" class="pull-right"><span class="divider">|</span>Version: 13.4.2</li>
<li class="pull-right"><a>toTop</a></li>
</ul>
</div>
</header>
<div class="row-fluid">
<header id="leftColumn" class="span4">
<nav class="well sidebar-nav">
<ul class="nav nav-list">
<li class="nav-header">About</li>
<li><a href="index.html">Checkstyle</a></li>
<li><a href="releasenotes.html">Release Notes</a></li>
<li><a href="consulting.html">Consulting</a></li>
<li><a href="sponsoring.html">Sponsoring</a></li>
<li class="nav-header">Documentation</li>
<li><a href="config.html"><span class="icon-chevron-down"></span>Configuration</a>
<ul class="nav nav-list">
<li><a href="property_types.html">Property Types</a></li>
<li><a href="config_system_properties.html">System Properties</a></li>
<li><a href="xpath.html">XPath</a></li>
</ul></li>
<li><a href="running.html"><span class="icon-chevron-down"></span>Running</a>
<ul class="nav nav-list">
<li><a href="anttask.html">Ant Task</a></li>
<li><a href="cmdline.html">Command Line</a></li>
<li><a href="result_reports.html">Result Reports</a></li>
</ul></li>
<li><a href="checks.html"><span class="icon-chevron-down"></span>Checks</a>
<ul class="nav nav-list">
<li><a href="checks/annotation/index.html"><span class="icon-chevron-right"></span>Annotations</a></li>
<li><a href="checks/blocks/index.html"><span class="icon-chevron-right"></span>Block Checks</a></li>
<li><a href="checks/design/index.html"><span class="icon-chevron-right"></span>Class Design</a></li>
<li><a href="checks/coding/index.html"><span class="icon-chevron-right"></span>Coding</a></li>
<li><a href="checks/header/index.html"><span class="icon-chevron-right"></span>Headers</a></li>
<li><a href="checks/imports/index.html"><span class="icon-chevron-right"></span>Imports</a></li>
<li><a href="checks/javadoc/index.html"><span class="icon-chevron-right"></span>Javadoc Comments</a></li>
<li><a href="checks/metrics/index.html"><span class="icon-chevron-right"></span>Metrics</a></li>
<li><a href="checks/misc/index.html"><span class="icon-chevron-right"></span>Miscellaneous</a></li>
<li><a href="checks/modifier/index.html"><span class="icon-chevron-right"></span>Modifiers</a></li>
<li><a href="checks/naming/index.html"><span class="icon-chevron-right"></span>Naming Conventions</a></li>
<li><a href="checks/regexp/index.html"><span class="icon-chevron-right"></span>Regexp</a></li>
<li><a href="checks/sizes/index.html"><span class="icon-chevron-right"></span>Size Violations</a></li>
<li><a href="checks/whitespace/index.html"><span class="icon-chevron-right"></span>Whitespace</a></li>
</ul></li>
<li><a href="filters/index.html"><span class="icon-chevron-right"></span>Filters</a></li>
<li><a href="filefilters/index.html"><span class="icon-chevron-right"></span>File Filters</a></li>
<li><a href="style_configs.html"><span class="icon-chevron-down"></span>Style Configurations</a>
<ul class="nav nav-list">
<li><a href="google_style.html">Google's Style</a></li>
<li><a href="openjdk_style.html">OpenJDK's Style</a></li>
<li><a href="sun_style.html">Sun's Style</a></li>
</ul></li>
<li class="nav-header">Developers</li>
<li><a href="extending.html"><span class="icon-chevron-down"></span>Extending Checkstyle</a>
<ul class="nav nav-list">
<li><a href="writingchecks.html">Writing Checks</a></li>
<li><a href="writingjavadocchecks.html">Writing Javadoc Checks</a></li>
<li class="active"><a>Writing Filters</a></li>
<li><a href="writingfilefilters.html">Writing File Filters</a></li>
<li><a href="writinglisteners.html">Writing Listeners</a></li>
</ul></li>
<li><a href="contributing.html">Contributing</a></li>
<li><a href="beginning_development.html"><span class="icon-chevron-down"></span>Beginning Development</a>
<ul class="nav nav-list">
<li><a href="eclipse.html">Eclipse IDE</a></li>
<li><a href="netbeans.html">NetBeans IDE</a></li>
<li><a href="idea.html">IntelliJ IDE</a></li>
</ul></li>
<li><a href="apidocs/index.html">Javadoc</a></li>
<li class="nav-header">Project Documentation</li>
<li><a href="project-info.html"><span class="icon-chevron-right"></span>Project Information</a></li>
<li><a href="project-reports.html"><span class="icon-chevron-right"></span>Project Reports</a></li>
</ul>
</nav>
<div class="well sidebar-nav">
<div id="poweredBy">
<div class="clear"></div>
<div class="clear"></div>
<a href="https://github.com/checkstyle/checkstyle" class="builtBy"><img class="builtBy" src="images/github_logo_social_coding_outlined.png" alt="GitHub" /></a>
<a href="https://twitter.com/checkstyle_java/" class="builtBy"><img class="builtBy" src="images/twitter_button.png" alt="Twitter" /></a>
<a href="https://stackoverflow.com/questions/tagged/checkstyle" class="builtBy"><img class="builtBy" src="images/stackoverflow.jpeg" alt="Stackoverflow" /></a>
<a href="https://groups.google.com/forum/#!forum/checkstyle" class="builtBy"><img class="builtBy" src="images/groups.png" alt="GoogleGroups" /></a>
<a href="https://www.ej-technologies.com/products/jprofiler/overview.html" class="builtBy"><img class="builtBy" src="https://www.ej-technologies.com/images/product_banners/jprofiler_medium.png" alt="JProfiler" /></a>
</div>
</div>
</header>
<main id="bodyColumn" class="span8">
<section><a id="Content"></a>
<h1>Content</h1>
<ul>
<li><a href="#Content">Content</a></li>
<li><a href="#Overview">Overview</a></li>
<li><a href="#Writing_Filters">Writing Filters</a></li>
<li><a href="#Writing_TreeWalkerFilters">Writing TreeWalkerFilters</a></li>
<li><a href="#Using_TreeWalkerFilters">Using TreeWalkerFilters</a></li>
<li><a href="#Declare_check.27s_external_resource_locations">Declare check's external resource locations</a></li>
<li><a href="#Using_Filters">Using Filters</a></li>
<li><a href="#Huh.3F_I_can.27t_figure_it_out.21">Huh? I can't figure it out!</a></li>
<li><a href="#Contributing">Contributing</a></li></ul>
</section>
<section><a id="Overview"></a>
<h1>Overview</h1>
<p>
A <code>Checker</code> has a set of <code>Filter</code>s that decide which audit events
the <code>Checker</code> reports through its listeners. Interface <code>Filter</code>
and class <code>FilterSet</code> are intended to support general <code>AuditEvent</code>
filtering using a set of <code>Filter</code>s.
</p>
<p>
A <code>Filter</code> has <code>boolean</code> method <code>accept(AuditEvent)</code> that
returns <code>true</code> if the <code>Filter</code> accepts the <code>AuditEvent</code>
parameter and returns <code>false</code> if the <code>Filter</code> rejects it.
</p>
<p>
A <code>FilterSet</code> is a particular <code>Filter</code> that contains a set of
<code>Filter</code>s. A <code>FilterSet</code> accepts an <code>AuditEvent</code> if and
only if all <code>Filter</code>s in the set accept the <code>AuditEvent</code>.
</p>
<p>
Here is a UML diagram for interface <code>Filter</code>
and class <code>FilterSet</code>.
</p>
<div class="wrapper">
<img src="images/Filter.png" alt="Filter UML diagram" />
</div>
<p>
Checkstyle also provides the <code>TreeWalkerFilter</code> interface for filters
that must live inside the <code>TreeWalker</code> module. See
<a href="#Writing_TreeWalkerFilters">Writing TreeWalkerFilters</a> below.
</p>
</section>
<section><a id="Writing_Filters"></a>
<h1>Writing Filters</h1>
<p>
The <code>Filter</code> that we demonstrate here rejects
audit events for files whose name matches a <a href="property_types.html#Pattern">Pattern</a>. In order to
enable the specification of the file name pattern as a property in a
configuration file, the <code>Filter</code> is an <a href="apidocs/com/puppycrawl/tools/checkstyle/api/AutomaticBean.html">AutomaticBean</a>
with mutator method <code>setFiles(String)</code> that
receives the file name pattern. An <code>AutomaticBean</code> uses JavaBean
introspection to set JavaBean properties such as <code>files</code>.
</p>
<div class="wrapper">
<pre class="prettyprint"><code class="language-java">
package com.mycompany.filters;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import com.puppycrawl.tools.checkstyle.api.AuditEvent;
import com.puppycrawl.tools.checkstyle.api.AutomaticBean;
import com.puppycrawl.tools.checkstyle.api.Filter;
import com.puppycrawl.tools.checkstyle.api.Utils;
public class FilesFilter
extends AutomaticBean
implements Filter
{
private Pattern mFileRegexp;
public FilesFilter()
throws PatternSyntaxException
{
setFiles("^$");
}
public boolean accept(AuditEvent aEvent)
{
final String fileName = aEvent.getFileName();
return ((fileName == null) || !mFileRegexp.matcher(fileName).find());
}
public void setFiles(String aFilesPattern)
throws PatternSyntaxException
{
mFileRegexp = Utils.getPattern(aFilesPattern);
}
}
</code></pre></div>
</section>
<section><a id="Writing_TreeWalkerFilters"></a>
<h1>Writing TreeWalkerFilters</h1>
<p>
A <code>TreeWalkerFilter</code> is like a <code>Filter</code> but must be declared
as a child of the <code>TreeWalker</code> module instead of <code>Checker</code>.
It receives a <code>TreeWalkerAuditEvent</code> instead of a plain
<code>AuditEvent</code>, which additionally provides access to the file's
<code>FileContents</code> (including comments) and the root <code>DetailAST</code>
of the parsed syntax tree.
</p>
<p>
The <code>TreeWalkerFilter</code> that we demonstrate here suppresses violations
on any line that contains a single-line comment matching a configurable pattern.
Like the built-in <code>SuppressionCommentFilter</code>, it extends
<code>AbstractAutomaticBean</code> so its property can be set from the
configuration file.
</p>
<div class="wrapper">
<pre class="prettyprint"><code class="language-java">
package com.mycompany.filters;
import java.util.regex.Pattern;
import com.puppycrawl.tools.checkstyle.AbstractAutomaticBean;
import com.puppycrawl.tools.checkstyle.TreeWalkerAuditEvent;
import com.puppycrawl.tools.checkstyle.TreeWalkerFilter;
import com.puppycrawl.tools.checkstyle.api.FileContents;
import com.puppycrawl.tools.checkstyle.api.TextBlock;
public class CommentAnnotationFilter
extends AbstractAutomaticBean
implements TreeWalkerFilter
{
private Pattern commentPattern = Pattern.compile("@SuppressMyCheck");
public void setCommentPattern(String pattern)
{
commentPattern = Pattern.compile(pattern);
}
@Override
protected void finishLocalSetup() {}
@Override
public boolean accept(TreeWalkerAuditEvent event)
{
if (event.violation() == null) {
return true;
}
final FileContents contents = event.fileContents();
final TextBlock comment =
contents.getSingleLineComments().get(event.getLine());
return comment == null
|| !commentPattern.matcher(comment.getText()[0]).find();
}
}
</code></pre></div>
</section>
<section><a id="Using_TreeWalkerFilters"></a>
<h1>Using TreeWalkerFilters</h1>
<p>
To incorporate a <code>TreeWalkerFilter</code>, declare it as a child of the
<code>TreeWalker</code> module in the configuration file. For example:
</p>
<div class="wrapper">
<pre class="prettyprint"><code class="language-xml">
<module name="Checker">
<module name="TreeWalker">
<module name="com.mycompany.filters.CommentAnnotationFilter">
<property name="commentPattern" value="@SuppressMyCheck"/>
</module>
<module name="ConstantName"/>
</module>
</module>
</code></pre></div>
<p>
Note that a <code>TreeWalkerFilter</code> only applies to checks that are also
children of the <code>TreeWalker</code> module. To filter non-TreeWalker checks,
use a regular <code>Filter</code> under <code>Checker</code> instead.
</p>
</section>
<section><a id="Declare_check.27s_external_resource_locations"></a>
<h1>Declare check's external resource locations</h1>
<p>
See <a href="writingchecks.html#Declare_check.27s_external_resource_locations">
Declare check's external resource locations</a>.
</p>
</section>
<section><a id="Using_Filters"></a>
<h1>Using Filters</h1>
<p>
To incorporate a <code>Filter</code> in the filter set
for a <code>Checker</code>, include a module element for
the <code>Filter</code> in the <a href="config.html#Filters">configuration file</a>. For example, to
configure a <code>Checker</code> so that it uses custom
filter <code>FilesFilter</code> to prevent reporting of
audit events for files whose name contains "Generated",
include the following module in the configuration file:
</p>
<div class="wrapper">
<pre class="prettyprint"><code class="language-xml">
<module name="com.mycompany.filters.FilesFilter">
<property name="files" value="Generated"/>
</module>
</code></pre></div>
</section>
<section><a id="Huh.3F_I_can.27t_figure_it_out.21"></a>
<h1>Huh? I can't figure it out!</h1>
<p>
That's probably our fault, and it means that we have to provide
better documentation. Please do not hesitate to ask questions on the
user mailing list, this will help us to improve this document. Please
ask your questions as precisely as possible. We will not be able to
answer questions like "I want to write a filter but I don't
know how, can you help me?". Tell us what you are trying to do
(the purpose of the filter), what you have understood so far, and what
exactly you are getting stuck on.
</p>
</section>
<section><a id="Contributing"></a>
<h1>Contributing</h1>
<p>
We need <em>your</em> help to keep improving Checkstyle. Whenever you
write a filter that you think is generally useful, please consider <a href="contributing.html">contributing</a> it to the Checkstyle
community and submit it for inclusion in the next release of
Checkstyle.
</p>
</section>
</main>
</div>
</div>
<hr/>
<footer>
<div class="container-fluid">
<div class="row-fluid">
<p>© 2001–2026
</p>
</div>
</div>
</footer>
</body>
</html>