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 ;
@@ -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 () {
@@ -408,28 +406,34 @@ private Result runExecutable() throws IOException, InterruptedException {
408406 }
409407 }
410408
411- Result reply = new Result ( process .waitFor () );
412- trace ("Done. Exit code: " + reply . exitCode );
409+ final int exitCode = process .waitFor ();
410+ trace ("Done. Exit code: " + exitCode );
413411
412+ final List <String > output ;
414413 if (outputLines != null ) {
415- reply .output = Collections .unmodifiableList (outputLines );
414+ output = Collections .unmodifiableList (outputLines );
415+ } else {
416+ output = null ;
416417 }
417- return reply ;
418+ return createResult ( exitCode , output ) ;
418419 }
419420
420- private Result runToolProvider (PrintStream out , PrintStream err ) {
421+ private int runToolProvider (PrintStream out , PrintStream err ) {
421422 trace ("Execute " + getPrintableCommandLine () + "..." );
422- Result reply = new Result ( toolProvider .run (out , err , args .toArray (
423- String []::new ))) ;
424- trace ("Done. Exit code: " + reply . exitCode );
425- return reply ;
423+ final int exitCode = toolProvider .run (out , err , args .toArray (
424+ String []::new ));
425+ trace ("Done. Exit code: " + exitCode );
426+ return exitCode ;
426427 }
427428
429+ private Result createResult (int exitCode , List <String > output ) {
430+ return new Result (exitCode , output , this ::getPrintableCommandLine );
431+ }
428432
429433 private Result runToolProvider () throws IOException {
430434 if (!withSavedOutput ()) {
431435 if (saveOutputType .contains (SaveOutputType .DUMP )) {
432- return runToolProvider (System .out , System .err );
436+ return createResult ( runToolProvider (System .out , System .err ), null );
433437 }
434438
435439 PrintStream nullPrintStream = new PrintStream (new OutputStream () {
@@ -438,36 +442,40 @@ public void write(int b) {
438442 // Nop
439443 }
440444 });
441- return runToolProvider (nullPrintStream , nullPrintStream );
445+ return createResult ( runToolProvider (nullPrintStream , nullPrintStream ), null );
442446 }
443447
444448 try (ByteArrayOutputStream buf = new ByteArrayOutputStream ();
445449 PrintStream ps = new PrintStream (buf )) {
446- Result reply = runToolProvider (ps , ps );
450+ final var exitCode = runToolProvider (ps , ps );
447451 ps .flush ();
452+ final List <String > output ;
448453 try (BufferedReader bufReader = new BufferedReader (new StringReader (
449454 buf .toString ()))) {
450455 if (saveOutputType .contains (SaveOutputType .FIRST_LINE )) {
451456 String firstLine = bufReader .lines ().findFirst ().orElse (null );
452457 if (firstLine != null ) {
453- reply .output = List .of (firstLine );
458+ output = List .of (firstLine );
459+ } else {
460+ output = null ;
454461 }
455462 } else if (saveOutputType .contains (SaveOutputType .FULL )) {
456- reply .output = bufReader .lines ().collect (
457- Collectors .toUnmodifiableList ());
463+ output = bufReader .lines ().collect (Collectors .toUnmodifiableList ());
464+ } else {
465+ output = null ;
458466 }
459467
460468 if (saveOutputType .contains (SaveOutputType .DUMP )) {
461469 Stream <String > lines ;
462470 if (saveOutputType .contains (SaveOutputType .FULL )) {
463- lines = reply . output .stream ();
471+ lines = output .stream ();
464472 } else {
465473 lines = bufReader .lines ();
466474 }
467475 lines .forEach (System .out ::println );
468476 }
469477 }
470- return reply ;
478+ return createResult ( exitCode , output ) ;
471479 }
472480 }
473481
0 commit comments