3636import java .util .HashSet ;
3737import java .util .List ;
3838import java .util .Objects ;
39+ import java .util .Optional ;
3940import java .util .Set ;
4041import java .util .regex .Pattern ;
4142import java .util .spi .ToolProvider ;
@@ -76,6 +77,10 @@ public Executor setToolProvider(JavaTool v) {
7677 return setToolProvider (v .asToolProvider ());
7778 }
7879
80+ public Optional <Path > getExecutable () {
81+ return Optional .ofNullable (executable );
82+ }
83+
7984 public Executor setDirectory (Path v ) {
8085 directory = v ;
8186 return this ;
@@ -173,10 +178,10 @@ public Executor dumpOutput(boolean v) {
173178 return this ;
174179 }
175180
176- public class Result {
181+ public record Result ( int exitCode , List < String > output , Supplier < String > cmdline ) {
177182
178- Result ( int exitCode ) {
179- this . exitCode = exitCode ;
183+ public Result {
184+ Objects . requireNonNull ( cmdline ) ;
180185 }
181186
182187 public String getFirstLineOfOutput () {
@@ -187,14 +192,10 @@ public List<String> getOutput() {
187192 return output ;
188193 }
189194
190- public String getPrintableCommandLine () {
191- return Executor .this .getPrintableCommandLine ();
192- }
193-
194195 public Result assertExitCodeIs (int expectedExitCode ) {
195196 TKit .assertEquals (expectedExitCode , exitCode , String .format (
196197 "Check command %s exited with %d code" ,
197- getPrintableCommandLine (), expectedExitCode ));
198+ cmdline . get (), expectedExitCode ));
198199 return this ;
199200 }
200201
@@ -205,9 +206,6 @@ public Result assertExitCodeIsZero() {
205206 public int getExitCode () {
206207 return exitCode ;
207208 }
208-
209- final int exitCode ;
210- private List <String > output ;
211209 }
212210
213211 public Result executeWithoutExitCodeCheck () {
@@ -367,28 +365,34 @@ private Result runExecutable() throws IOException, InterruptedException {
367365 }
368366 }
369367
370- Result reply = new Result ( process .waitFor () );
371- trace ("Done. Exit code: " + reply . exitCode );
368+ final int exitCode = process .waitFor ();
369+ trace ("Done. Exit code: " + exitCode );
372370
371+ final List <String > output ;
373372 if (outputLines != null ) {
374- reply .output = Collections .unmodifiableList (outputLines );
373+ output = Collections .unmodifiableList (outputLines );
374+ } else {
375+ output = null ;
375376 }
376- return reply ;
377+ return createResult ( exitCode , output ) ;
377378 }
378379
379- private Result runToolProvider (PrintStream out , PrintStream err ) {
380+ private int runToolProvider (PrintStream out , PrintStream err ) {
380381 trace ("Execute " + getPrintableCommandLine () + "..." );
381- Result reply = new Result ( toolProvider .run (out , err , args .toArray (
382- String []::new ))) ;
383- trace ("Done. Exit code: " + reply . exitCode );
384- return reply ;
382+ final int exitCode = toolProvider .run (out , err , args .toArray (
383+ String []::new ));
384+ trace ("Done. Exit code: " + exitCode );
385+ return exitCode ;
385386 }
386387
388+ private Result createResult (int exitCode , List <String > output ) {
389+ return new Result (exitCode , output , this ::getPrintableCommandLine );
390+ }
387391
388392 private Result runToolProvider () throws IOException {
389393 if (!withSavedOutput ()) {
390394 if (saveOutputType .contains (SaveOutputType .DUMP )) {
391- return runToolProvider (System .out , System .err );
395+ return createResult ( runToolProvider (System .out , System .err ), null );
392396 }
393397
394398 PrintStream nullPrintStream = new PrintStream (new OutputStream () {
@@ -397,36 +401,40 @@ public void write(int b) {
397401 // Nop
398402 }
399403 });
400- return runToolProvider (nullPrintStream , nullPrintStream );
404+ return createResult ( runToolProvider (nullPrintStream , nullPrintStream ), null );
401405 }
402406
403407 try (ByteArrayOutputStream buf = new ByteArrayOutputStream ();
404408 PrintStream ps = new PrintStream (buf )) {
405- Result reply = runToolProvider (ps , ps );
409+ final var exitCode = runToolProvider (ps , ps );
406410 ps .flush ();
411+ final List <String > output ;
407412 try (BufferedReader bufReader = new BufferedReader (new StringReader (
408413 buf .toString ()))) {
409414 if (saveOutputType .contains (SaveOutputType .FIRST_LINE )) {
410415 String firstLine = bufReader .lines ().findFirst ().orElse (null );
411416 if (firstLine != null ) {
412- reply .output = List .of (firstLine );
417+ output = List .of (firstLine );
418+ } else {
419+ output = null ;
413420 }
414421 } else if (saveOutputType .contains (SaveOutputType .FULL )) {
415- reply .output = bufReader .lines ().collect (
416- Collectors .toUnmodifiableList ());
422+ output = bufReader .lines ().collect (Collectors .toUnmodifiableList ());
423+ } else {
424+ output = null ;
417425 }
418426
419427 if (saveOutputType .contains (SaveOutputType .DUMP )) {
420428 Stream <String > lines ;
421429 if (saveOutputType .contains (SaveOutputType .FULL )) {
422- lines = reply . output .stream ();
430+ lines = output .stream ();
423431 } else {
424432 lines = bufReader .lines ();
425433 }
426434 lines .forEach (System .out ::println );
427435 }
428436 }
429- return reply ;
437+ return createResult ( exitCode , output ) ;
430438 }
431439 }
432440
0 commit comments