/
Report.java
528 lines (469 loc) · 14.9 KB
/
Report.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
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import net.sourceforge.pmd.lang.dfa.report.ReportTree;
import net.sourceforge.pmd.renderers.AbstractAccumulatingRenderer;
import net.sourceforge.pmd.stat.Metric;
import net.sourceforge.pmd.util.DateTimeUtil;
import net.sourceforge.pmd.util.EmptyIterator;
import net.sourceforge.pmd.util.NumericConstants;
import net.sourceforge.pmd.util.StringUtil;
/**
* A {@link Report} collects all informations during a PMD execution. This
* includes violations, suppressed violations, metrics, error during processing
* and configuration errors.
*/
public class Report implements Iterable<RuleViolation> {
/*
* The idea is to store the violations in a tree instead of a list, to do
* better and faster sort and filter mechanism and to visualize the result
* as tree. (ide plugins).
*/
private final ReportTree violationTree = new ReportTree();
// Note that this and the above data structure are both being maintained for
// a bit
private final List<RuleViolation> violations = new ArrayList<>();
private final Set<Metric> metrics = new HashSet<>();
private final List<SynchronizedReportListener> listeners = new ArrayList<>();
private List<ProcessingError> errors;
private List<RuleConfigurationError> configErrors;
private Map<Integer, String> linesToSuppress = new HashMap<>();
private long start;
private long end;
private List<SuppressedViolation> suppressedRuleViolations = new ArrayList<>();
/**
* Creates a new, initialized, empty report for the given file name.
*
* @param ctx
* The context to use to connect to the report
* @param fileName
* the filename used to report any violations
* @return the new report
*/
public static Report createReport(RuleContext ctx, String fileName) {
Report report = new Report();
// overtake the listener
report.addSynchronizedListeners(ctx.getReport().getSynchronizedListeners());
ctx.setReport(report);
ctx.setSourceCodeFilename(fileName);
ctx.setSourceCodeFile(new File(fileName));
return report;
}
/**
* Represents a duration. Useful for reporting processing time.
*/
public static class ReadableDuration {
private final long duration;
/**
* Creates a new duration.
*
* @param duration
* the duration in milliseconds.
*/
public ReadableDuration(long duration) {
this.duration = duration;
}
/**
* Gets a human readable representation of the duration, such as "1h 3m
* 5s".
*
* @return human readable representation of the duration
*/
public String getTime() {
return DateTimeUtil.asHoursMinutesSeconds(duration);
}
}
/**
* Represents a configuration error.
*/
public static class RuleConfigurationError {
private final Rule rule;
private final String issue;
/**
* Creates a new configuration error.
*
* @param theRule
* the rule which is configured wrongly
* @param theIssue
* the reason, why the configuration is wrong
*/
public RuleConfigurationError(Rule theRule, String theIssue) {
rule = theRule;
issue = theIssue;
}
/**
* Gets the wrongly configured rule
*
* @return the wrongly configured rule
*/
public Rule rule() {
return rule;
}
/**
* Gets the reason for the configuration error.
*
* @return the issue
*/
public String issue() {
return issue;
}
}
/**
* Represents a processing error, such as a parse error.
*/
public static class ProcessingError {
private final String msg;
private final String file;
/**
* Creates a new processing error
*
* @param msg
* the error message
* @param file
* the file during which the error occurred
*/
public ProcessingError(String msg, String file) {
this.msg = msg;
this.file = file;
}
public String getMsg() {
return msg;
}
public String getFile() {
return file;
}
}
/**
* Represents a violation, that has been suppressed.
*/
public static class SuppressedViolation {
private final RuleViolation rv;
private final boolean isNOPMD;
private final String userMessage;
/**
* Creates a suppressed violation.
*
* @param rv
* the actual violation, that has been suppressed
* @param isNOPMD
* the suppression mode: <code>true</code> if it is
* suppressed via a NOPMD comment, <code>false</code> if
* suppressed via annotations.
* @param userMessage
* contains the suppressed code line or <code>null</code>
*/
public SuppressedViolation(RuleViolation rv, boolean isNOPMD, String userMessage) {
this.isNOPMD = isNOPMD;
this.rv = rv;
this.userMessage = userMessage;
}
/**
* Returns <code>true</code> if the violation has been suppressed via a
* NOPMD comment.
*
* @return <code>true</code> if the violation has been suppressed via a
* NOPMD comment.
*/
public boolean suppressedByNOPMD() {
return this.isNOPMD;
}
/**
* Returns <code>true</code> if the violation has been suppressed via a
* annotation.
*
* @return <code>true</code> if the violation has been suppressed via a
* annotation.
*/
public boolean suppressedByAnnotation() {
return !this.isNOPMD;
}
public RuleViolation getRuleViolation() {
return this.rv;
}
public String getUserMessage() {
return userMessage;
}
}
/**
* Configure the lines, that are suppressed via a NOPMD comment.
*
* @param lines
* the suppressed lines
*/
public void suppress(Map<Integer, String> lines) {
linesToSuppress = lines;
}
private static String keyFor(RuleViolation rv) {
return StringUtil.isNotEmpty(rv.getPackageName()) ? rv.getPackageName() + '.' + rv.getClassName() : "";
}
/**
* Calculate a summary of violation counts per fully classified class name.
*
* @return violations per class name
*/
public Map<String, Integer> getCountSummary() {
Map<String, Integer> summary = new HashMap<>();
for (RuleViolation rv : violationTree) {
String key = keyFor(rv);
Integer o = summary.get(key);
summary.put(key, o == null ? NumericConstants.ONE : o + 1);
}
return summary;
}
public ReportTree getViolationTree() {
return this.violationTree;
}
/**
* Calculate a summary of violations per rule.
*
* @return a Map summarizing the Report: String (rule name) -> Integer (count
* of violations)
*/
public Map<String, Integer> getSummary() {
Map<String, Integer> summary = new HashMap<>();
for (RuleViolation rv : violations) {
String name = rv.getRule().getName();
if (!summary.containsKey(name)) {
summary.put(name, NumericConstants.ZERO);
}
Integer count = summary.get(name);
summary.put(name, count + 1);
}
return summary;
}
/**
* Registers a report listener
*
* @param listener
* the listener
*/
public void addListener(ReportListener listener) {
listeners.add(new SynchronizedReportListener(listener));
}
public List<SuppressedViolation> getSuppressedRuleViolations() {
return suppressedRuleViolations;
}
/**
* Adds a new rule violation to the report and notify the listeners.
*
* @param violation
* the violation to add
*/
public void addRuleViolation(RuleViolation violation) {
// NOPMD suppress
int line = violation.getBeginLine();
if (linesToSuppress.containsKey(line)) {
suppressedRuleViolations.add(new SuppressedViolation(violation, true, linesToSuppress.get(line)));
return;
}
if (violation.isSuppressed()) {
suppressedRuleViolations.add(new SuppressedViolation(violation, false, null));
return;
}
int index = Collections.binarySearch(violations, violation, RuleViolationComparator.INSTANCE);
violations.add(index < 0 ? -index - 1 : index, violation);
violationTree.addRuleViolation(violation);
for (ReportListener listener : listeners) {
listener.ruleViolationAdded(violation);
}
}
/**
* Adds a new metric to the report and notify the listeners
*
* @param metric
* the metric to add
*/
public void addMetric(Metric metric) {
metrics.add(metric);
for (ReportListener listener : listeners) {
listener.metricAdded(metric);
}
}
/**
* Adds a new configuration error to the report.
*
* @param error
* the error to add
*/
public void addConfigError(RuleConfigurationError error) {
if (configErrors == null) {
configErrors = new ArrayList<>();
}
configErrors.add(error);
}
/**
* Adds a new processing error to the report.
*
* @param error
* the error to add
*/
public void addError(ProcessingError error) {
if (errors == null) {
errors = new ArrayList<>();
}
errors.add(error);
}
/**
* Merges the given report into this report. This might be necessary, if a
* summary over all violations is needed as PMD creates one report per file
* by default.
*
* @param r
* the report to be merged into this.
* @see AbstractAccumulatingRenderer
*/
public void merge(Report r) {
Iterator<ProcessingError> i = r.errors();
while (i.hasNext()) {
addError(i.next());
}
Iterator<Metric> m = r.metrics();
while (m.hasNext()) {
addMetric(m.next());
}
Iterator<RuleViolation> v = r.iterator();
while (v.hasNext()) {
RuleViolation violation = v.next();
int index = Collections.binarySearch(violations, violation, RuleViolationComparator.INSTANCE);
violations.add(index < 0 ? -index - 1 : index, violation);
violationTree.addRuleViolation(violation);
}
Iterator<SuppressedViolation> s = r.getSuppressedRuleViolations().iterator();
while (s.hasNext()) {
suppressedRuleViolations.add(s.next());
}
}
/**
* Check whether any metrics have been reported
*
* @return <code>true</code> if there are metrics, <code>false</code>
* otherwise
*/
public boolean hasMetrics() {
return !metrics.isEmpty();
}
/**
* Iterate over the metrics.
*
* @return an iterator over the metrics
*/
public Iterator<Metric> metrics() {
return metrics.iterator();
}
public boolean isEmpty() {
return !violations.iterator().hasNext() && !hasErrors();
}
/**
* Checks whether any processing errors have been reported.
*
* @return <code>true</code> if there were any processing errors,
* <code>false</code> otherwise
*/
public boolean hasErrors() {
return errors != null && !errors.isEmpty();
}
/**
* Checks whether any configuration errors have been reported.
*
* @return <code>true</code> if there were any configuration errors,
* <code>false</code> otherwise
*/
public boolean hasConfigErrors() {
return configErrors != null && !configErrors.isEmpty();
}
/**
* Checks whether no violations have been reported.
*
* @return <code>true</code> if no violations have been reported,
* <code>false</code> otherwise
*/
public boolean treeIsEmpty() {
return !violationTree.iterator().hasNext();
}
/**
* Returns an iteration over the reported violations.
*
* @return an iterator
*/
public Iterator<RuleViolation> treeIterator() {
return violationTree.iterator();
}
@Override
public Iterator<RuleViolation> iterator() {
return violations.iterator();
}
/**
* Returns an iterator of the reported processing errors.
*
* @return the iterator
*/
public Iterator<ProcessingError> errors() {
return errors == null ? EmptyIterator.<ProcessingError>instance() : errors.iterator();
}
/**
* Returns an iterator of the reported configuration errors.
*
* @return the iterator
*/
public Iterator<RuleConfigurationError> configErrors() {
return configErrors == null ? EmptyIterator.<RuleConfigurationError>instance() : configErrors.iterator();
}
/**
* The number of violations.
*
* @return number of violations.
*/
public int treeSize() {
return violationTree.size();
}
/**
* The number of violations.
*
* @return number of violations.
*/
public int size() {
return violations.size();
}
/**
* Mark the start time of the report. This is used to get the elapsed time
* in the end.
*
* @see #getElapsedTimeInMillis()
*/
public void start() {
start = System.currentTimeMillis();
}
/**
* Mark the end time of the report. This is ued to get the elapsed time.
*
* @see #getElapsedTimeInMillis()
*/
public void end() {
end = System.currentTimeMillis();
}
public long getElapsedTimeInMillis() {
return end - start;
}
public List<SynchronizedReportListener> getSynchronizedListeners() {
return listeners;
}
/**
* Adds all given listeners to this report
*
* @param synchronizedListeners
* the report listeners
*/
public void addSynchronizedListeners(List<SynchronizedReportListener> synchronizedListeners) {
listeners.addAll(synchronizedListeners);
}
}