Skip to content

Commit

Permalink
Use Java 17 language features
Browse files Browse the repository at this point in the history
instanceof pattern matching

Formatted strings

Add @serial annotation
  • Loading branch information
MarkEWaite committed Jan 3, 2025
1 parent dd4fb3b commit 54b139b
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions src/main/java/io/jenkins/plugins/httpclient/RobustHTTPClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serial;
import java.io.Serializable;
import java.net.URI;
import java.net.URISyntaxException;
Expand Down Expand Up @@ -64,6 +65,7 @@
*/
public final class RobustHTTPClient implements Serializable {

@Serial
private static final long serialVersionUID = 1;

private static final ExecutorService executors =
Expand Down Expand Up @@ -191,12 +193,12 @@ public void connect(
} else {
diag = null;
}
throw new AbortException(String.format(
"Failed to %s, response: %d %s, body: %s",
whatVerbose,
responseCode.get(),
statusLine != null ? statusLine.getReasonPhrase() : "?",
diag));
throw new AbortException("Failed to %s, response: %d %s, body: %s"
.formatted(
whatVerbose,
responseCode.get(),
statusLine != null ? statusLine.getReasonPhrase() : "?",
diag));
}
connectionUser.use(response);
}
Expand All @@ -212,13 +214,13 @@ public void connect(
return; // success
} catch (ExecutionException wrapped) {
Throwable x = wrapped.getCause();
if (x instanceof IOException) {
if (x instanceof IOException exception) {

Check warning on line 217 in src/main/java/io/jenkins/plugins/httpclient/RobustHTTPClient.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 217 is only partially covered, one branch is missing
if (attempt == stopAfterAttemptNumber) {
throw (IOException) x; // last chance
throw exception; // last chance
}
if (responseCode.get() > 0 && responseCode.get() < 200
|| responseCode.get() >= 300 && responseCode.get() < 500) {
throw (IOException) x; // 4xx errors should not be retried
throw exception; // 4xx errors should not be retried
}
// TODO exponent base could be made into a configurable parameter

Check warning on line 225 in src/main/java/io/jenkins/plugins/httpclient/RobustHTTPClient.java

View check run for this annotation

ci.jenkins.io / Open Tasks Scanner

TODO

NORMAL: exponent base could be made into a configurable parameter
Thread.sleep(Math.min(((long) Math.pow(2d, attempt)) * waitMultiplier, waitMaximum));
Expand All @@ -227,10 +229,10 @@ public void connect(
"Retrying %s after: %s%n",
whatConcise, x instanceof AbortException ? x.getMessage() : x.toString());
attempt++; // and continue
} else if (x instanceof InterruptedException) { // all other exceptions considered fatal
throw (InterruptedException) x;
} else if (x instanceof RuntimeException) {
throw (RuntimeException) x;
} else if (x instanceof InterruptedException exception) { // all other exceptions considered fatal
throw exception;
} else if (x instanceof RuntimeException exception) {
throw exception;

Check warning on line 235 in src/main/java/io/jenkins/plugins/httpclient/RobustHTTPClient.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 232-235 are not covered by tests
} else if (x != null) {
throw new RuntimeException(x);
} else {
Expand Down Expand Up @@ -318,7 +320,9 @@ public void copyFromRemotely(FilePath f, URL url, TaskListener listener) throws
}

private static final class CopyFromRemotely extends MasterToSlaveFileCallable<Void> {
@Serial
private static final long serialVersionUID = 1;

private final RobustHTTPClient client;
private final URL u;
private final TaskListener listener;
Expand Down

0 comments on commit 54b139b

Please sign in to comment.