3636import java .util .HashSet ;
3737import java .util .List ;
3838import java .util .Objects ;
39+ import java .util .Optional ;
3940import java .util .Set ;
4041import java .util .function .Supplier ;
4142import java .util .regex .Pattern ;
@@ -77,6 +78,10 @@ public Executor setToolProvider(JavaTool v) {
7778 return setToolProvider (v .asToolProvider ());
7879 }
7980
81+ public Optional <Path > getExecutable () {
82+ return Optional .ofNullable (executable );
83+ }
84+
8085 public Executor setDirectory (Path v ) {
8186 directory = v ;
8287 return this ;
@@ -174,10 +179,10 @@ public Executor dumpOutput(boolean v) {
174179 return this ;
175180 }
176181
177- public class Result {
182+ public record Result ( int exitCode , List < String > output , Supplier < String > cmdline ) {
178183
179- Result ( int exitCode ) {
180- this . exitCode = exitCode ;
184+ public Result {
185+ Objects . requireNonNull ( cmdline ) ;
181186 }
182187
183188 public String getFirstLineOfOutput () {
@@ -188,14 +193,10 @@ public List<String> getOutput() {
188193 return output ;
189194 }
190195
191- public String getPrintableCommandLine () {
192- return Executor .this .getPrintableCommandLine ();
193- }
194-
195196 public Result assertExitCodeIs (int expectedExitCode ) {
196197 TKit .assertEquals (expectedExitCode , exitCode , String .format (
197198 "Check command %s exited with %d code" ,
198- getPrintableCommandLine (), expectedExitCode ));
199+ cmdline . get (), expectedExitCode ));
199200 return this ;
200201 }
201202
@@ -206,9 +207,6 @@ public Result assertExitCodeIsZero() {
206207 public int getExitCode () {
207208 return exitCode ;
208209 }
209-
210- final int exitCode ;
211- private List <String > output ;
212210 }
213211
214212 public Result executeWithoutExitCodeCheck () {
@@ -406,28 +404,34 @@ private Result runExecutable() throws IOException, InterruptedException {
406404 }
407405 }
408406
409- Result reply = new Result ( process .waitFor () );
410- trace ("Done. Exit code: " + reply . exitCode );
407+ final int exitCode = process .waitFor ();
408+ trace ("Done. Exit code: " + exitCode );
411409
410+ final List <String > output ;
412411 if (outputLines != null ) {
413- reply .output = Collections .unmodifiableList (outputLines );
412+ output = Collections .unmodifiableList (outputLines );
413+ } else {
414+ output = null ;
414415 }
415- return reply ;
416+ return createResult ( exitCode , output ) ;
416417 }
417418
418- private Result runToolProvider (PrintStream out , PrintStream err ) {
419+ private int runToolProvider (PrintStream out , PrintStream err ) {
419420 trace ("Execute " + getPrintableCommandLine () + "..." );
420- Result reply = new Result ( toolProvider .run (out , err , args .toArray (
421- String []::new ))) ;
422- trace ("Done. Exit code: " + reply . exitCode );
423- return reply ;
421+ final int exitCode = toolProvider .run (out , err , args .toArray (
422+ String []::new ));
423+ trace ("Done. Exit code: " + exitCode );
424+ return exitCode ;
424425 }
425426
427+ private Result createResult (int exitCode , List <String > output ) {
428+ return new Result (exitCode , output , this ::getPrintableCommandLine );
429+ }
426430
427431 private Result runToolProvider () throws IOException {
428432 if (!withSavedOutput ()) {
429433 if (saveOutputType .contains (SaveOutputType .DUMP )) {
430- return runToolProvider (System .out , System .err );
434+ return createResult ( runToolProvider (System .out , System .err ), null );
431435 }
432436
433437 PrintStream nullPrintStream = new PrintStream (new OutputStream () {
@@ -436,36 +440,40 @@ public void write(int b) {
436440 // Nop
437441 }
438442 });
439- return runToolProvider (nullPrintStream , nullPrintStream );
443+ return createResult ( runToolProvider (nullPrintStream , nullPrintStream ), null );
440444 }
441445
442446 try (ByteArrayOutputStream buf = new ByteArrayOutputStream ();
443447 PrintStream ps = new PrintStream (buf )) {
444- Result reply = runToolProvider (ps , ps );
448+ final var exitCode = runToolProvider (ps , ps );
445449 ps .flush ();
450+ final List <String > output ;
446451 try (BufferedReader bufReader = new BufferedReader (new StringReader (
447452 buf .toString ()))) {
448453 if (saveOutputType .contains (SaveOutputType .FIRST_LINE )) {
449454 String firstLine = bufReader .lines ().findFirst ().orElse (null );
450455 if (firstLine != null ) {
451- reply .output = List .of (firstLine );
456+ output = List .of (firstLine );
457+ } else {
458+ output = null ;
452459 }
453460 } else if (saveOutputType .contains (SaveOutputType .FULL )) {
454- reply .output = bufReader .lines ().collect (
455- Collectors .toUnmodifiableList ());
461+ output = bufReader .lines ().collect (Collectors .toUnmodifiableList ());
462+ } else {
463+ output = null ;
456464 }
457465
458466 if (saveOutputType .contains (SaveOutputType .DUMP )) {
459467 Stream <String > lines ;
460468 if (saveOutputType .contains (SaveOutputType .FULL )) {
461- lines = reply . output .stream ();
469+ lines = output .stream ();
462470 } else {
463471 lines = bufReader .lines ();
464472 }
465473 lines .forEach (System .out ::println );
466474 }
467475 }
468- return reply ;
476+ return createResult ( exitCode , output ) ;
469477 }
470478 }
471479
0 commit comments