This is an automated email from the ASF dual-hosted git repository.
He-Pin pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/pekko-http.git
The following commit(s) were added to refs/heads/main by this push:
new dd1e91458 refactor: migrate String.format to String.formatted (Java
15+) (#1091)
dd1e91458 is described below
commit dd1e91458bcfc1b05c0e4c685a3ef0b941da401d
Author: He-Pin(kerr) <[email protected]>
AuthorDate: Fri Jun 26 17:15:11 2026 +0800
refactor: migrate String.format to String.formatted (Java 15+) (#1091)
* refactor: migrate String.format to String.formatted (Java 15+)
Motivation:
Java 15 introduced String.formatted() as an instance method counterpart
to String.format(), providing cleaner and more readable string formatting.
Modification:
Replace String.format("literal", args) with "literal".formatted(args)
across 14 files including HTTP server examples, test files, and
the SimpleServerApp.
Result:
More concise and idiomatic Java code leveraging Java 15+ API.
* style: apply javafmt to fix Checks failure
Apply javafmt to 5 test files that were failing code style check.
---
.../http/javadsl/HttpRequestDetailedStringExampleTest.java | 2 +-
.../http/javadsl/HttpResponseDetailedStringExampleTest.java | 2 +-
.../javadsl/server/FormFieldRequestValsExampleTest.java | 4 ++--
.../http/javadsl/server/HeaderRequestValsExampleTest.java | 5 ++---
.../java/docs/http/javadsl/server/WebSocketCoreExample.java | 8 ++------
.../server/directives/CachingDirectivesExamplesTest.java | 13 +++++--------
.../server/directives/DebuggingDirectivesExamplesTest.java | 2 +-
.../server/directives/SchemeDirectivesExamplesTest.java | 3 +--
.../server/directives/TimeoutDirectivesExamplesTest.java | 2 +-
.../javadsl/server/examples/simple/SimpleServerApp.java | 6 +++---
.../docs/http/javadsl/server/HandlerExampleDocTest.java | 5 ++---
.../org/apache/pekko/http/javadsl/server/CompleteTest.java | 2 +-
.../apache/pekko/http/javadsl/server/JavaTestServer.java | 4 ++--
.../http/javadsl/server/directives/PathDirectivesTest.java | 2 +-
14 files changed, 25 insertions(+), 35 deletions(-)
diff --git
a/docs/src/test/java/docs/http/javadsl/HttpRequestDetailedStringExampleTest.java
b/docs/src/test/java/docs/http/javadsl/HttpRequestDetailedStringExampleTest.java
index bd1a5ee78..5bc0e1fb2 100644
---
a/docs/src/test/java/docs/http/javadsl/HttpRequestDetailedStringExampleTest.java
+++
b/docs/src/test/java/docs/http/javadsl/HttpRequestDetailedStringExampleTest.java
@@ -31,7 +31,7 @@ public class HttpRequestDetailedStringExampleTest {
RequestEntity entity = request.entity();
HttpProtocol protocol = request.protocol();
- return String.format("HttpRequest(%s, %s, %s, %s, %s)", method, uri,
headers, entity, protocol);
+ return "HttpRequest(%s, %s, %s, %s, %s)".formatted(method, uri, headers,
entity, protocol);
}
@Test
diff --git
a/docs/src/test/java/docs/http/javadsl/HttpResponseDetailedStringExampleTest.java
b/docs/src/test/java/docs/http/javadsl/HttpResponseDetailedStringExampleTest.java
index 5edb48927..1edb2d4ad 100644
---
a/docs/src/test/java/docs/http/javadsl/HttpResponseDetailedStringExampleTest.java
+++
b/docs/src/test/java/docs/http/javadsl/HttpResponseDetailedStringExampleTest.java
@@ -31,7 +31,7 @@ public class HttpResponseDetailedStringExampleTest {
HttpEntity entity = response.entity();
HttpProtocol protocol = response.protocol();
- return String.format("HttpResponse(%s, %s, %s, %s)", status, headers,
entity, protocol);
+ return "HttpResponse(%s, %s, %s, %s)".formatted(status, headers, entity,
protocol);
}
@Test
diff --git
a/docs/src/test/java/docs/http/javadsl/server/FormFieldRequestValsExampleTest.java
b/docs/src/test/java/docs/http/javadsl/server/FormFieldRequestValsExampleTest.java
index c3d8a9594..6caa7d7d9 100644
---
a/docs/src/test/java/docs/http/javadsl/server/FormFieldRequestValsExampleTest.java
+++
b/docs/src/test/java/docs/http/javadsl/server/FormFieldRequestValsExampleTest.java
@@ -48,7 +48,7 @@ public class FormFieldRequestValsExampleTest extends
JUnitJupiterRouteTest {
formField(
StringUnmarshallers.INTEGER,
"age",
- a -> complete(String.format("Name: %s, age: %d", n, a))));
+ a -> complete("Name: %s, age: %d".formatted(n, a))));
// tests:
final FormData formData =
@@ -66,7 +66,7 @@ public class FormFieldRequestValsExampleTest extends
JUnitJupiterRouteTest {
StringUnmarshaller.sync(s -> new SampleId(Integer.valueOf(s)));
final Route route =
- formField(SAMPLE_ID, "id", sid -> complete(String.format("SampleId:
%s", sid.id)));
+ formField(SAMPLE_ID, "id", sid -> complete("SampleId:
%s".formatted(sid.id)));
// tests:
final FormData formData = FormData.create(Pair.create("id", "1337"));
diff --git
a/docs/src/test/java/docs/http/javadsl/server/HeaderRequestValsExampleTest.java
b/docs/src/test/java/docs/http/javadsl/server/HeaderRequestValsExampleTest.java
index 6271091c7..5e2aadcd8 100644
---
a/docs/src/test/java/docs/http/javadsl/server/HeaderRequestValsExampleTest.java
+++
b/docs/src/test/java/docs/http/javadsl/server/HeaderRequestValsExampleTest.java
@@ -38,7 +38,7 @@ public class HeaderRequestValsExampleTest extends
JUnitJupiterRouteTest {
public void testHeaderVals() {
// #by-class
- final Route route = extractHost(host -> complete(String.format("Host
header was: %s", host)));
+ final Route route = extractHost(host -> complete("Host header was:
%s".formatted(host)));
// tests:
final HttpRequest request =
@@ -56,8 +56,7 @@ public class HeaderRequestValsExampleTest extends
JUnitJupiterRouteTest {
// extract the `value` of the header:
headerValueByName(
"X-Fish-Name",
- xFishName ->
- complete(String.format("The `X-Fish-Name` header's value was:
%s", xFishName)));
+ xFishName -> complete("The `X-Fish-Name` header's value was:
%s".formatted(xFishName)));
// tests:
final HttpRequest request =
diff --git
a/docs/src/test/java/docs/http/javadsl/server/WebSocketCoreExample.java
b/docs/src/test/java/docs/http/javadsl/server/WebSocketCoreExample.java
index e083940fa..e28988938 100644
--- a/docs/src/test/java/docs/http/javadsl/server/WebSocketCoreExample.java
+++ b/docs/src/test/java/docs/http/javadsl/server/WebSocketCoreExample.java
@@ -135,9 +135,7 @@ public class WebSocketCoreExample {
defaultSettings
.getWebsocketSettings()
.withPeriodicKeepAliveData(
- () ->
- ByteString.fromString(
- String.format("debug-%d",
pingCounter.incrementAndGet())));
+ () ->
ByteString.fromString("debug-%d".formatted(pingCounter.incrementAndGet())));
ServerSettings customServerSettings =
defaultSettings.withWebsocketSettings(customWebsocketSettings);
@@ -160,9 +158,7 @@ public class WebSocketCoreExample {
defaultSettings
.getWebsocketSettings()
.withPeriodicKeepAliveData(
- () ->
- ByteString.fromString(
- String.format("debug-%d",
pingCounter.incrementAndGet())));
+ () ->
ByteString.fromString("debug-%d".formatted(pingCounter.incrementAndGet())));
ClientConnectionSettings customSettings =
defaultSettings.withWebsocketSettings(customWebsocketSettings);
diff --git
a/docs/src/test/java/docs/http/javadsl/server/directives/CachingDirectivesExamplesTest.java
b/docs/src/test/java/docs/http/javadsl/server/directives/CachingDirectivesExamplesTest.java
index 40d1a593c..7dc26b625 100644
---
a/docs/src/test/java/docs/http/javadsl/server/directives/CachingDirectivesExamplesTest.java
+++
b/docs/src/test/java/docs/http/javadsl/server/directives/CachingDirectivesExamplesTest.java
@@ -82,9 +82,8 @@ public class CachingDirectivesExamplesTest extends
JUnitJupiterRouteTest {
extractUri(
uri ->
complete(
- String.format(
- "Request for %s @ count %d",
- uri, count.incrementAndGet())))));
+ "Request for %s @ count %d"
+ .formatted(uri,
count.incrementAndGet())))));
// tests:
testRoute(route)
@@ -137,9 +136,8 @@ public class CachingDirectivesExamplesTest extends
JUnitJupiterRouteTest {
extractUri(
uri ->
complete(
- String.format(
- "Request for %s @ count %d",
- uri, count.incrementAndGet())))));
+ "Request for %s @ count %d"
+ .formatted(uri,
count.incrementAndGet())))));
// tests:
testRoute(route)
@@ -187,8 +185,7 @@ public class CachingDirectivesExamplesTest extends
JUnitJupiterRouteTest {
final AtomicInteger count = new AtomicInteger(0);
final Route innerRoute =
extractUri(
- uri ->
- complete(String.format("Request for %s @ count %d", uri,
count.incrementAndGet())));
+ uri -> complete("Request for %s @ count %d".formatted(uri,
count.incrementAndGet())));
// #create-cache
final CachingSettings defaultCachingSettings =
CachingSettings.create(system());
diff --git
a/docs/src/test/java/docs/http/javadsl/server/directives/DebuggingDirectivesExamplesTest.java
b/docs/src/test/java/docs/http/javadsl/server/directives/DebuggingDirectivesExamplesTest.java
index 62d749141..7530ba498 100644
---
a/docs/src/test/java/docs/http/javadsl/server/directives/DebuggingDirectivesExamplesTest.java
+++
b/docs/src/test/java/docs/http/javadsl/server/directives/DebuggingDirectivesExamplesTest.java
@@ -122,7 +122,7 @@ public class DebuggingDirectivesExamplesTest extends
JUnitJupiterRouteTest {
Function<HttpResponse, LogEntry> showSuccessAsInfo =
(response) ->
LogEntry.create(
- String.format("Response code '%d'",
response.status().intValue()), InfoLevel());
+ "Response code '%d'".formatted(response.status().intValue()),
InfoLevel());
Function<List<Rejection>, LogEntry> showRejectionAsInfo =
(rejections) ->
diff --git
a/docs/src/test/java/docs/http/javadsl/server/directives/SchemeDirectivesExamplesTest.java
b/docs/src/test/java/docs/http/javadsl/server/directives/SchemeDirectivesExamplesTest.java
index 892eea9f8..40345feac 100644
---
a/docs/src/test/java/docs/http/javadsl/server/directives/SchemeDirectivesExamplesTest.java
+++
b/docs/src/test/java/docs/http/javadsl/server/directives/SchemeDirectivesExamplesTest.java
@@ -39,8 +39,7 @@ public class SchemeDirectivesExamplesTest extends
JUnitJupiterRouteTest {
@Test
public void testScheme() {
// #extractScheme
- final Route route =
- extractScheme((scheme) -> complete(String.format("The scheme is '%s'",
scheme)));
+ final Route route = extractScheme((scheme) -> complete("The scheme is
'%s'".formatted(scheme)));
testRoute(route)
.run(HttpRequest.GET("https://www.example.com/"))
.assertEntity("The scheme is 'https'");
diff --git
a/docs/src/test/java/docs/http/javadsl/server/directives/TimeoutDirectivesExamplesTest.java
b/docs/src/test/java/docs/http/javadsl/server/directives/TimeoutDirectivesExamplesTest.java
index c255411b4..b00ccbb83 100644
---
a/docs/src/test/java/docs/http/javadsl/server/directives/TimeoutDirectivesExamplesTest.java
+++
b/docs/src/test/java/docs/http/javadsl/server/directives/TimeoutDirectivesExamplesTest.java
@@ -53,7 +53,7 @@ public class TimeoutDirectivesExamplesTest extends
AllDirectives {
private final Http http = Http.get(system);
private void shutdown(ServerBinding b) throws Exception {
- System.out.println(String.format("Unbinding from %s", b.localAddress()));
+ System.out.println("Unbinding from %s".formatted(b.localAddress()));
b.unbind().toCompletableFuture().get(3, TimeUnit.SECONDS);
}
diff --git
a/http-tests/src/main/java/org/apache/pekko/http/javadsl/server/examples/simple/SimpleServerApp.java
b/http-tests/src/main/java/org/apache/pekko/http/javadsl/server/examples/simple/SimpleServerApp.java
index 6a354a84c..a3835e3cc 100644
---
a/http-tests/src/main/java/org/apache/pekko/http/javadsl/server/examples/simple/SimpleServerApp.java
+++
b/http-tests/src/main/java/org/apache/pekko/http/javadsl/server/examples/simple/SimpleServerApp.java
@@ -45,7 +45,7 @@ public class SimpleServerApp {
// #https-http-app
public Route multiply(int x, int y) {
int result = x * y;
- return complete(String.format("%d * %d = %d", x, y, result));
+ return complete("%d * %d = %d".formatted(x, y, result));
}
public CompletionStage<Route> multiplyAsync(Executor ctx, int x, int y) {
@@ -63,13 +63,13 @@ public class SimpleServerApp {
"y",
y -> {
int result = x + y;
- return complete(String.format("%d + %d = %d", x, y,
result));
+ return complete("%d + %d = %d".formatted(x, y, result));
}));
BiFunction<Integer, Integer, Route> subtractHandler =
(x, y) -> {
int result = x - y;
- return complete(String.format("%d - %d = %d", x, y, result));
+ return complete("%d - %d = %d".formatted(x, y, result));
};
return concat(
diff --git
a/http-tests/src/test/java/docs/http/javadsl/server/HandlerExampleDocTest.java
b/http-tests/src/test/java/docs/http/javadsl/server/HandlerExampleDocTest.java
index 4b27fe5f6..af962ccc7 100644
---
a/http-tests/src/test/java/docs/http/javadsl/server/HandlerExampleDocTest.java
+++
b/http-tests/src/test/java/docs/http/javadsl/server/HandlerExampleDocTest.java
@@ -69,8 +69,7 @@ public class HandlerExampleDocTest extends
JUnitJupiterRouteTest {
method ->
extractUri(
uri ->
- complete(
- String.format("This was a %s request to %s",
method.name(), uri))));
+ complete("This was a %s request to
%s".formatted(method.name(), uri))));
Route handlerResponse =
extractMethod(
@@ -81,7 +80,7 @@ public class HandlerExampleDocTest extends
JUnitJupiterRouteTest {
final HttpResponse response =
HttpResponse.create()
.withEntity(
- String.format("Accepted %s request to %s",
method.name(), uri))
+ "Accepted %s request to
%s".formatted(method.name(), uri))
.withStatus(StatusCodes.ACCEPTED);
return complete(response);
}));
diff --git
a/http-tests/src/test/java/org/apache/pekko/http/javadsl/server/CompleteTest.java
b/http-tests/src/test/java/org/apache/pekko/http/javadsl/server/CompleteTest.java
index 6d2c992dd..ff36c78c7 100644
---
a/http-tests/src/test/java/org/apache/pekko/http/javadsl/server/CompleteTest.java
+++
b/http-tests/src/test/java/org/apache/pekko/http/javadsl/server/CompleteTest.java
@@ -69,7 +69,7 @@ public class CompleteTest extends JUnitJupiterRouteTest {
return CompletableFuture.supplyAsync(
() -> {
int result = x + y;
- return String.format("%d + %d = %d", x, y, result);
+ return "%d + %d = %d".formatted(x, y, result);
});
}
diff --git
a/http-tests/src/test/java/org/apache/pekko/http/javadsl/server/JavaTestServer.java
b/http-tests/src/test/java/org/apache/pekko/http/javadsl/server/JavaTestServer.java
index ef1314501..42c22be96 100644
---
a/http-tests/src/test/java/org/apache/pekko/http/javadsl/server/JavaTestServer.java
+++
b/http-tests/src/test/java/org/apache/pekko/http/javadsl/server/JavaTestServer.java
@@ -79,7 +79,7 @@ public class JavaTestServer {
authenticateBasic(
"My basic secure site",
handleAuth,
- (login) -> complete(String.format("Hello, %s!", login))));
+ (login) -> complete("Hello, %s!".formatted(login))));
final Route ping = path("ping", () -> complete("PONG!"));
@@ -198,7 +198,7 @@ public class JavaTestServer {
private CompletionStage<Void> shutdown(CompletionStage<ServerBinding>
binding) {
return binding.thenAccept(
b -> {
- System.out.println(String.format("Unbinding from %s",
b.localAddress()));
+ System.out.println("Unbinding from %s".formatted(b.localAddress()));
final CompletionStage<?> unbound = b.unbind();
try {
diff --git
a/http-tests/src/test/java/org/apache/pekko/http/javadsl/server/directives/PathDirectivesTest.java
b/http-tests/src/test/java/org/apache/pekko/http/javadsl/server/directives/PathDirectivesTest.java
index 377239b18..d5ab4fdcc 100644
---
a/http-tests/src/test/java/org/apache/pekko/http/javadsl/server/directives/PathDirectivesTest.java
+++
b/http-tests/src/test/java/org/apache/pekko/http/javadsl/server/directives/PathDirectivesTest.java
@@ -204,7 +204,7 @@ public class PathDirectivesTest extends
JUnitJupiterRouteTest {
testRoute(
path(
segment("multiply").slash(integerSegment()).slash("with").slash(integerSegment()),
- (x, y) -> complete(String.format("%d * %d = %d", x, y, x *
y))));
+ (x, y) -> complete("%d * %d = %d".formatted(x, y, x * y))));
route.run(HttpRequest.GET("/multiply/3/with/6")).assertEntity("3 * 6 =
18");
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]