Skip to content

Commit

Permalink
Convert ExternalProcess.Result class into a Java 17 record
Browse files Browse the repository at this point in the history
  • Loading branch information
dabico committed Jan 10, 2024
1 parent f249f4f commit 2a98a91
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/main/java/ch/usi/si/seart/cloc/CLOCConnector.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public JsonObject analyze(Path path) throws StaticCodeAnalysisException {
try {
ExternalProcess process = new ExternalProcess(path, "cloc", "--json", "--quiet", ".");
ExternalProcess.Result result = process.execute(analysisTimeout.toMillis());
result.ifFailedThrow(() -> new StaticCodeAnalysisException(result.getStdErr()));
JsonElement element = conversionService.convert(result.getStdOut(), JsonElement.class);
result.ifFailedThrow(() -> new StaticCodeAnalysisException(result.stdErr()));
JsonElement element = conversionService.convert(result.stdOut(), JsonElement.class);
return element.isJsonNull() ? new JsonObject() : element.getAsJsonObject();
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/ch/usi/si/seart/git/GitConnector.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public LocalRepositoryClone clone(URL url) throws GitException {
ExternalProcess process = new ExternalProcess(directory, command);
ExternalProcess.Result result = process.execute(cloneTimeout.toMillis());
result.ifFailedThrow(() -> {
GitException exception = conversionService.convert(result.getStdErr(), GitException.class);
GitException exception = conversionService.convert(result.stdErr(), GitException.class);
return (GitException) exception.fillInStackTrace();
});
return new LocalRepositoryClone(directory);
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/ch/usi/si/seart/http/ClientURLConnector.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ public boolean ping(URL url) throws ClientURLException {
ExternalProcess process = new ExternalProcess(command);
log.trace("Pinging: {}", url);
ExternalProcess.Result result = process.execute(connectTimeout.toMillis());
int code = result.getCode();
return switch (code) {
return switch (result.code()) {
case 0 -> true;
case 6 -> throw new UnknownHostException("Could not resolve host address!");
case 7 -> throw new ConnectException("Connection to host failed!");
Expand Down
13 changes: 1 addition & 12 deletions src/main/java/ch/usi/si/seart/io/ExternalProcess.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package ch.usi.si.seart.io;

import ch.usi.si.seart.exception.TerminalExecutionException;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.experimental.FieldDefaults;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -151,14 +147,7 @@ public Result execute(long timeout, TimeUnit unit) throws TimeoutException, Inte
*
* @author Ozren Dabić
*/
@Getter
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@FieldDefaults(level = AccessLevel.PRIVATE, makeFinal = true)
public static final class Result {

int code;
String stdOut;
String stdErr;
public record Result(int code, String stdOut, String stdErr) {

/**
* Checks if the command execution succeeded.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public Object postProcessBeforeInitialization(Object bean, @NotNull String beanN
String message = String.format(template, command);
return new BeanInitializationException(message);
});
result.getStdOut().trim().lines().forEach(log::debug);
result.stdOut().trim().lines().forEach(log::debug);
} catch (TimeoutException ex) {
String template = "Timed out checking '%s'";
String message = String.format(template, command);
Expand Down

0 comments on commit 2a98a91

Please sign in to comment.