40
40
import java .util .Map ;
41
41
import java .util .Set ;
42
42
import java .util .StringTokenizer ;
43
- import java .util .function .BiConsumer ;
44
- import java .util .function .Supplier ;
45
43
import java .util .zip .GZIPOutputStream ;
46
44
47
45
import javax .annotation .CheckForNull ;
@@ -117,8 +115,6 @@ private interface Chooser {
117
115
118
116
private final Set <String > disabledBugReporterDecorators = new LinkedHashSet <>();
119
117
120
- private final Set <Path > usedReporterPaths = new LinkedHashSet <>();
121
-
122
118
private boolean setExitCode = false ;
123
119
124
120
private boolean noClassOk = false ;
@@ -270,40 +266,30 @@ public boolean justPrintVersion() {
270
266
private List <TextUIBugReporter > reporters = new ArrayList <>();
271
267
272
268
/**
273
- * Parse {@code optionExtraPart} and create a path-associated {@Link TextUIBugReporter} if it contains the
269
+ * Parse {@code optionExtraPart} and configure {@Link TextUIBugReporter} if it contains the
274
270
* output file path such as {@code ":withMessages=path/to/file.extension"} and {@code "=/absolute/path/to/file.extension"}.
275
- * Finally configure the created BugReporter with the {@code optionExtraPart}'s configuration information.
276
271
*
272
+ * @param reporter the reporter to set a {@link PrintStream} based on the given file path
277
273
* @param optionExtraPart extra part of the specified commandline option
278
- * @param ctor A supplier for an unconfigured {@Link TextUIBugReporter}.
279
- * @param handleOptions A function that can configure the created {@code BugReporter} instance.
280
- * @param <T> The implementation type of the {@Link TextUIBugReporter} to propagate type
281
- * information between {@link ctor} and {@link handleOptions}.
282
- * @return The fully configured reporter, or {@code null}, if the reporter would output to a file that is already used as a reporter output file.
274
+ * @return Remaining part of {@code optionExtraPart}
283
275
*/
284
- /* visible for testing */ <T extends TextUIBugReporter > TextUIBugReporter initializeReporter (String optionExtraPart ,
285
- Supplier <T > ctor , BiConsumer <T , String > handleOptions ) {
276
+ /* visible for testing */ String handleOutputFilePath (TextUIBugReporter reporter , String optionExtraPart ) {
286
277
int index = optionExtraPart .indexOf ('=' );
287
- T reporter = ctor .get ();
288
278
if (index >= 0 ) {
289
279
Path path = Paths .get (optionExtraPart .substring (index + 1 ));
290
- if (this .usedReporterPaths .contains (path )) {
291
- return null ;
292
- }
293
280
try {
294
281
OutputStream oStream = Files .newOutputStream (path , StandardOpenOption .CREATE , StandardOpenOption .WRITE ,
295
282
StandardOpenOption .TRUNCATE_EXISTING );
296
283
if ("gz" .equals (Util .getFileExtension (path .toFile ()))) {
297
284
oStream = new GZIPOutputStream (oStream );
298
285
}
299
286
reporter .setOutputStream (UTF8 .printStream (oStream ));
300
- usedReporterPaths .add (path );
301
287
} catch (IOException e ) {
302
288
throw new UncheckedIOException (e );
303
289
}
304
- handleOptions . accept ( reporter , optionExtraPart .substring (0 , index ) );
290
+ optionExtraPart = optionExtraPart .substring (0 , index );
305
291
}
306
- return reporter ;
292
+ return optionExtraPart ;
307
293
}
308
294
309
295
@ SuppressFBWarnings ("DM_EXIT" )
@@ -357,44 +343,35 @@ protected void handleOption(String option, String optionExtraPart) {
357
343
mergeSimilarWarnings = false ;
358
344
} else if ("-sortByClass" .equals (option )) {
359
345
bugReporterType = SORTING_REPORTER ;
360
- TextUIBugReporter reporter = initializeReporter (optionExtraPart ,
361
- () -> new SortingBugReporter (),
362
- (r , rOpts ) -> {
363
- });
364
- if (reporter != null ) {
365
- reporters .add (reporter );
366
- }
346
+ SortingBugReporter sortingBugReporter = new SortingBugReporter ();
347
+ handleOutputFilePath (sortingBugReporter , optionExtraPart );
348
+ reporters .add (sortingBugReporter );
367
349
} else if ("-xml" .equals (option )) {
368
350
bugReporterType = XML_REPORTER ;
369
- TextUIBugReporter reporter = initializeReporter (
370
- optionExtraPart ,
371
- () -> new XMLBugReporter (project ),
372
- (r , arg ) -> {
373
- if (!"" .equals (arg )) {
374
- if ("withMessages" .equals (arg )) {
375
- r .setAddMessages (true );
376
- } else if ("withAbridgedMessages" .equals (arg )) {
377
- r .setAddMessages (true );
378
- xmlWithAbridgedMessages = true ;
379
- } else if ("minimal" .equals (arg )) {
380
- r .setMinimalXML (true );
381
- } else {
382
- throw new IllegalArgumentException ("Unknown option: -xml:" + arg );
383
- }
384
- }
385
- });
386
- if (reporter != null ) {
387
- reporters .add (reporter );
351
+ XMLBugReporter xmlBugReporter = new XMLBugReporter (project );
352
+ optionExtraPart = handleOutputFilePath (xmlBugReporter , optionExtraPart );
353
+ boolean xmlWithMessages = false ;
354
+ boolean xmlMinimal = false ;
355
+ if (!"" .equals (optionExtraPart )) {
356
+ if ("withMessages" .equals (optionExtraPart )) {
357
+ xmlWithMessages = true ;
358
+ } else if ("withAbridgedMessages" .equals (optionExtraPart )) {
359
+ xmlWithMessages = true ;
360
+ xmlWithAbridgedMessages = true ;
361
+ } else if ("minimal" .equals (optionExtraPart )) {
362
+ xmlMinimal = true ;
363
+ } else {
364
+ throw new IllegalArgumentException ("Unknown option: -xml:" + optionExtraPart );
365
+ }
388
366
}
367
+ xmlBugReporter .setAddMessages (xmlWithMessages );
368
+ xmlBugReporter .setMinimalXML (xmlMinimal );
369
+ reporters .add (xmlBugReporter );
389
370
} else if ("-emacs" .equals (option )) {
371
+ EmacsBugReporter emacsBugReporter = new EmacsBugReporter ();
372
+ handleOutputFilePath (emacsBugReporter , optionExtraPart );
373
+ reporters .add (emacsBugReporter );
390
374
bugReporterType = EMACS_REPORTER ;
391
- TextUIBugReporter reporter = initializeReporter (optionExtraPart ,
392
- () -> new EmacsBugReporter (),
393
- (r , rOpts ) -> {
394
- });
395
- if (reporter != null ) {
396
- reporters .add (reporter );
397
- }
398
375
} else if ("-relaxed" .equals (option )) {
399
376
relaxedReportingMode = true ;
400
377
} else if ("-train" .equals (option )) {
@@ -403,34 +380,22 @@ protected void handleOption(String option, String optionExtraPart) {
403
380
trainingInputDir = !"" .equals (optionExtraPart ) ? optionExtraPart : "." ;
404
381
} else if ("-html" .equals (option )) {
405
382
bugReporterType = HTML_REPORTER ;
406
- TextUIBugReporter reporter = initializeReporter (optionExtraPart ,
407
- () -> new HTMLBugReporter (project , "default.xsl" ),
408
- (r , rOpt ) -> {
409
- if (!"" .equals (rOpt )) {
410
- r .setStylesheet (rOpt );
411
- }
412
- });
413
- if (reporter != null ) {
414
- reporters .add (reporter );
383
+ HTMLBugReporter htmlBugReporter = new HTMLBugReporter (project , "default.xsl" );
384
+ optionExtraPart = handleOutputFilePath (htmlBugReporter , optionExtraPart );
385
+ if (!"" .equals (optionExtraPart )) {
386
+ htmlBugReporter .setStylesheet (optionExtraPart );
415
387
}
388
+ reporters .add (htmlBugReporter );
416
389
} else if ("-xdocs" .equals (option )) {
417
390
bugReporterType = XDOCS_REPORTER ;
418
- TextUIBugReporter reporter = initializeReporter (optionExtraPart ,
419
- () -> new XDocsBugReporter (project ),
420
- (r , rOpts ) -> {
421
- });
422
- if (reporter != null ) {
423
- reporters .add (reporter );
424
- }
391
+ XDocsBugReporter xDocsBugReporter = new XDocsBugReporter (project );
392
+ handleOutputFilePath (xDocsBugReporter , optionExtraPart );
393
+ reporters .add (xDocsBugReporter );
425
394
} else if ("-sarif" .equals (option )) {
426
395
bugReporterType = SARIF_REPORTER ;
427
- TextUIBugReporter reporter = initializeReporter (optionExtraPart ,
428
- () -> new SarifBugReporter (project ),
429
- (r , rOpts ) -> {
430
- });
431
- if (reporter != null ) {
432
- reporters .add (reporter );
433
- }
396
+ SarifBugReporter sarifBugReporter = new SarifBugReporter (project );
397
+ handleOutputFilePath (sarifBugReporter , optionExtraPart );
398
+ reporters .add (sarifBugReporter );
434
399
} else if ("-applySuppression" .equals (option )) {
435
400
applySuppression = true ;
436
401
} else if ("-quiet" .equals (option )) {
0 commit comments