1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.puppycrawl.tools.checkstyle.filters;
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.nio.charset.StandardCharsets;
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.Objects;
28 import java.util.Optional;
29 import java.util.regex.Matcher;
30 import java.util.regex.Pattern;
31 import java.util.regex.PatternSyntaxException;
32
33 import com.puppycrawl.tools.checkstyle.AbstractAutomaticBean;
34 import com.puppycrawl.tools.checkstyle.PropertyType;
35 import com.puppycrawl.tools.checkstyle.XdocsPropertyType;
36 import com.puppycrawl.tools.checkstyle.api.AuditEvent;
37 import com.puppycrawl.tools.checkstyle.api.FileText;
38 import com.puppycrawl.tools.checkstyle.api.Filter;
39 import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
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 public class SuppressWithPlainTextCommentFilter extends AbstractAutomaticBean implements Filter {
119
120
121 private static final String DEFAULT_OFF_FORMAT = "// CHECKSTYLE:OFF";
122
123
124 private static final String DEFAULT_ON_FORMAT = "// CHECKSTYLE:ON";
125
126
127 private static final String DEFAULT_CHECK_FORMAT = ".*";
128
129
130 private final Collection<Suppression> currentFileSuppressionCache = new ArrayList<>();
131
132
133 private String currentFileName = "";
134
135
136 private Pattern offCommentFormat = CommonUtil.createPattern(DEFAULT_OFF_FORMAT);
137
138
139 private Pattern onCommentFormat = CommonUtil.createPattern(DEFAULT_ON_FORMAT);
140
141
142 @XdocsPropertyType(PropertyType.PATTERN)
143 private String checkFormat = DEFAULT_CHECK_FORMAT;
144
145
146 @XdocsPropertyType(PropertyType.PATTERN)
147 private String messageFormat;
148
149
150 @XdocsPropertyType(PropertyType.PATTERN)
151 private String idFormat;
152
153
154
155
156
157
158
159 public final void setOffCommentFormat(Pattern pattern) {
160 offCommentFormat = pattern;
161 }
162
163
164
165
166
167
168
169 public final void setOnCommentFormat(Pattern pattern) {
170 onCommentFormat = pattern;
171 }
172
173
174
175
176
177
178
179 public final void setCheckFormat(String format) {
180 checkFormat = format;
181 }
182
183
184
185
186
187
188
189 public final void setMessageFormat(String format) {
190 messageFormat = format;
191 }
192
193
194
195
196
197
198
199 public final void setIdFormat(String format) {
200 idFormat = format;
201 }
202
203 @Override
204 public boolean accept(AuditEvent event) {
205 boolean accepted = true;
206 if (event.getViolation() != null) {
207 final String eventFileName = event.getFileName();
208
209 if (!currentFileName.equals(eventFileName)) {
210 currentFileName = eventFileName;
211 final FileText fileText = getFileText(eventFileName);
212 currentFileSuppressionCache.clear();
213 if (fileText != null) {
214 cacheSuppressions(fileText);
215 }
216 }
217
218 accepted = getNearestSuppression(currentFileSuppressionCache, event) == null;
219 }
220 return accepted;
221 }
222
223 @Override
224 protected void finishLocalSetup() {
225
226 }
227
228
229
230
231
232
233
234
235 private static FileText getFileText(String fileName) {
236 final File file = new File(fileName);
237 FileText result = null;
238
239
240 if (!file.isDirectory()) {
241 try {
242 result = new FileText(file, StandardCharsets.UTF_8.name());
243 }
244 catch (IOException exc) {
245 throw new IllegalStateException("Cannot read source file: " + fileName, exc);
246 }
247 }
248
249 return result;
250 }
251
252
253
254
255
256
257 private void cacheSuppressions(FileText fileText) {
258 for (int lineNo = 0; lineNo < fileText.size(); lineNo++) {
259 final Optional<Suppression> suppression = getSuppression(fileText, lineNo);
260 suppression.ifPresent(currentFileSuppressionCache::add);
261 }
262 }
263
264
265
266
267
268
269
270
271 private Optional<Suppression> getSuppression(FileText fileText, int lineNo) {
272 final String line = fileText.get(lineNo);
273 final Matcher onCommentMatcher = onCommentFormat.matcher(line);
274 final Matcher offCommentMatcher = offCommentFormat.matcher(line);
275
276 Suppression suppression = null;
277 if (onCommentMatcher.find()) {
278 suppression = new Suppression(onCommentMatcher.group(0),
279 lineNo + 1, SuppressionType.ON, this);
280 }
281 if (offCommentMatcher.find()) {
282 suppression = new Suppression(offCommentMatcher.group(0),
283 lineNo + 1, SuppressionType.OFF, this);
284 }
285
286 return Optional.ofNullable(suppression);
287 }
288
289
290
291
292
293
294
295
296
297
298 private static Suppression getNearestSuppression(Collection<Suppression> suppressions,
299 AuditEvent event) {
300 return suppressions
301 .stream()
302 .filter(suppression -> suppression.isMatch(event))
303 .reduce((first, second) -> second)
304 .filter(suppression -> suppression.suppressionType != SuppressionType.ON)
305 .orElse(null);
306 }
307
308
309 private enum SuppressionType {
310
311
312 ON,
313
314 OFF,
315
316 }
317
318
319 private static final class Suppression {
320
321
322 private final Pattern eventSourceRegexp;
323
324 private final Pattern eventMessageRegexp;
325
326 private final Pattern eventIdRegexp;
327
328
329 private final int lineNo;
330
331
332 private final SuppressionType suppressionType;
333
334
335
336
337
338
339
340
341
342
343 private Suppression(
344 String text,
345 int lineNo,
346 SuppressionType suppressionType,
347 SuppressWithPlainTextCommentFilter filter
348 ) {
349 this.lineNo = lineNo;
350 this.suppressionType = suppressionType;
351
352 final Pattern commentFormat;
353 if (this.suppressionType == SuppressionType.ON) {
354 commentFormat = filter.onCommentFormat;
355 }
356 else {
357 commentFormat = filter.offCommentFormat;
358 }
359
360
361
362 String format = "";
363 try {
364 format = CommonUtil.fillTemplateWithStringsByRegexp(
365 filter.checkFormat, text, commentFormat);
366 eventSourceRegexp = Pattern.compile(format);
367 if (filter.messageFormat == null) {
368 eventMessageRegexp = null;
369 }
370 else {
371 format = CommonUtil.fillTemplateWithStringsByRegexp(
372 filter.messageFormat, text, commentFormat);
373 eventMessageRegexp = Pattern.compile(format);
374 }
375 if (filter.idFormat == null) {
376 eventIdRegexp = null;
377 }
378 else {
379 format = CommonUtil.fillTemplateWithStringsByRegexp(
380 filter.idFormat, text, commentFormat);
381 eventIdRegexp = Pattern.compile(format);
382 }
383 }
384 catch (final PatternSyntaxException exc) {
385 throw new IllegalArgumentException(
386 "unable to parse expanded comment " + format, exc);
387 }
388 }
389
390
391
392
393
394
395
396
397 @Override
398 public boolean equals(Object other) {
399 if (this == other) {
400 return true;
401 }
402 if (other == null || getClass() != other.getClass()) {
403 return false;
404 }
405 final Suppression suppression = (Suppression) other;
406 return Objects.equals(lineNo, suppression.lineNo)
407 && Objects.equals(suppressionType, suppression.suppressionType)
408 && Objects.equals(eventSourceRegexp, suppression.eventSourceRegexp)
409 && Objects.equals(eventMessageRegexp, suppression.eventMessageRegexp)
410 && Objects.equals(eventIdRegexp, suppression.eventIdRegexp);
411 }
412
413 @Override
414 public int hashCode() {
415 return Objects.hash(
416 lineNo, suppressionType, eventSourceRegexp, eventMessageRegexp,
417 eventIdRegexp);
418 }
419
420
421
422
423
424
425
426 private boolean isMatch(AuditEvent event) {
427 return isInScopeOfSuppression(event)
428 && isCheckMatch(event)
429 && isIdMatch(event)
430 && isMessageMatch(event);
431 }
432
433
434
435
436
437
438
439 private boolean isInScopeOfSuppression(AuditEvent event) {
440 return lineNo <= event.getLine();
441 }
442
443
444
445
446
447
448
449 private boolean isCheckMatch(AuditEvent event) {
450 final Matcher checkMatcher = eventSourceRegexp.matcher(event.getSourceName());
451 return checkMatcher.find();
452 }
453
454
455
456
457
458
459
460 private boolean isIdMatch(AuditEvent event) {
461 boolean match = true;
462 if (eventIdRegexp != null) {
463 if (event.getModuleId() == null) {
464 match = false;
465 }
466 else {
467 final Matcher idMatcher = eventIdRegexp.matcher(event.getModuleId());
468 match = idMatcher.find();
469 }
470 }
471 return match;
472 }
473
474
475
476
477
478
479
480 private boolean isMessageMatch(AuditEvent event) {
481 boolean match = true;
482 if (eventMessageRegexp != null) {
483 final Matcher messageMatcher = eventMessageRegexp.matcher(event.getMessage());
484 match = messageMatcher.find();
485 }
486 return match;
487 }
488 }
489
490 }