Skip to content

Commit 3711389

Browse files
committed
Use Java 17 language features
instanceof pattern matching Formatted strings Add @serial annotation
1 parent 8a46834 commit 3711389

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

src/main/java/io/jenkins/plugins/httpclient/RobustHTTPClient.java

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.io.File;
3434
import java.io.IOException;
3535
import java.io.InputStream;
36+
import java.io.Serial;
3637
import java.io.Serializable;
3738
import java.net.URI;
3839
import java.net.URISyntaxException;
@@ -64,6 +65,7 @@
6465
*/
6566
public final class RobustHTTPClient implements Serializable {
6667

68+
@Serial
6769
private static final long serialVersionUID = 1;
6870

6971
private static final ExecutorService executors =
@@ -191,12 +193,12 @@ public void connect(
191193
} else {
192194
diag = null;
193195
}
194-
throw new AbortException(String.format(
195-
"Failed to %s, response: %d %s, body: %s",
196-
whatVerbose,
197-
responseCode.get(),
198-
statusLine != null ? statusLine.getReasonPhrase() : "?",
199-
diag));
196+
throw new AbortException("Failed to %s, response: %d %s, body: %s"
197+
.formatted(
198+
whatVerbose,
199+
responseCode.get(),
200+
statusLine != null ? statusLine.getReasonPhrase() : "?",
201+
diag));
200202
}
201203
connectionUser.use(response);
202204
}
@@ -212,13 +214,13 @@ public void connect(
212214
return; // success
213215
} catch (ExecutionException wrapped) {
214216
Throwable x = wrapped.getCause();
215-
if (x instanceof IOException) {
217+
if (x instanceof IOException exception) {
216218
if (attempt == stopAfterAttemptNumber) {
217-
throw (IOException) x; // last chance
219+
throw exception; // last chance
218220
}
219221
if (responseCode.get() > 0 && responseCode.get() < 200
220222
|| responseCode.get() >= 300 && responseCode.get() < 500) {
221-
throw (IOException) x; // 4xx errors should not be retried
223+
throw exception; // 4xx errors should not be retried
222224
}
223225
// TODO exponent base could be made into a configurable parameter
224226
Thread.sleep(Math.min(((long) Math.pow(2d, attempt)) * waitMultiplier, waitMaximum));
@@ -227,10 +229,10 @@ public void connect(
227229
"Retrying %s after: %s%n",
228230
whatConcise, x instanceof AbortException ? x.getMessage() : x.toString());
229231
attempt++; // and continue
230-
} else if (x instanceof InterruptedException) { // all other exceptions considered fatal
231-
throw (InterruptedException) x;
232-
} else if (x instanceof RuntimeException) {
233-
throw (RuntimeException) x;
232+
} else if (x instanceof InterruptedException exception) { // all other exceptions considered fatal
233+
throw exception;
234+
} else if (x instanceof RuntimeException exception) {
235+
throw exception;
234236
} else if (x != null) {
235237
throw new RuntimeException(x);
236238
} else {
@@ -318,7 +320,9 @@ public void copyFromRemotely(FilePath f, URL url, TaskListener listener) throws
318320
}
319321

320322
private static final class CopyFromRemotely extends MasterToSlaveFileCallable<Void> {
323+
@Serial
321324
private static final long serialVersionUID = 1;
325+
322326
private final RobustHTTPClient client;
323327
private final URL u;
324328
private final TaskListener listener;

0 commit comments

Comments
 (0)