Skip to content

Commit c6228df

Browse files
committed
Replace use of String.format(..) with formatted Strings.
Closes spring-projects#2751
1 parent 9fb6b55 commit c6228df

File tree

83 files changed

+316
-351
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+316
-351
lines changed

src/main/java/org/springframework/data/redis/ClusterRedirectException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public class ClusterRedirectException extends DataRetrievalFailureException {
4343
*/
4444
public ClusterRedirectException(int slot, String targetHost, int targetPort, Throwable e) {
4545

46-
super(String.format("Redirect: slot %s to %s:%s.", slot, targetHost, targetPort), e);
46+
super("Redirect: slot %s to %s:%s.".formatted(slot, targetHost, targetPort), e);
4747

4848
this.slot = slot;
4949
this.host = targetHost;

src/main/java/org/springframework/data/redis/cache/DefaultRedisCacheWriter.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -382,9 +382,8 @@ private void checkAndPotentiallyWaitUntilUnlocked(String name, RedisConnection c
382382
// Re-interrupt current Thread to allow other participants to react.
383383
Thread.currentThread().interrupt();
384384

385-
String message = String.format("Interrupted while waiting to unlock cache %s", name);
386-
387-
throw new PessimisticLockingFailureException(message, ex);
385+
throw new PessimisticLockingFailureException("Interrupted while waiting to unlock cache %s"
386+
.formatted(name), ex);
388387
} finally {
389388
this.statistics.incLockTime(name, System.nanoTime() - lockWaitTimeNs);
390389
}

src/main/java/org/springframework/data/redis/cache/RedisCache.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -324,12 +324,9 @@ private Object processAndCheckValue(@Nullable Object value) {
324324
Object cacheValue = preProcessCacheValue(value);
325325

326326
if (nullCacheValueIsNotAllowed(cacheValue)) {
327-
328-
String message = String.format("Cache '%s' does not allow 'null' values; Avoid storing null"
327+
throw new IllegalArgumentException(("Cache '%s' does not allow 'null' values; Avoid storing null"
329328
+ " via '@Cacheable(unless=\"#result == null\")' or configure RedisCache to allow 'null'"
330-
+ " via RedisCacheConfiguration", getName());
331-
332-
throw new IllegalArgumentException(message);
329+
+ " via RedisCacheConfiguration").formatted(getName()));
333330
}
334331

335332
return cacheValue;
@@ -440,11 +437,9 @@ protected String convertKey(Object key) {
440437
return key.toString();
441438
}
442439

443-
String message = String.format("Cannot convert cache key %s to String; Please register a suitable Converter"
444-
+ " via 'RedisCacheConfiguration.configureKeyConverters(...)' or override '%s.toString()'",
445-
source, key.getClass().getName());
446-
447-
throw new IllegalStateException(message);
440+
throw new IllegalStateException(("Cannot convert cache key %s to String; Please register a suitable Converter"
441+
+ " via 'RedisCacheConfiguration.configureKeyConverters(...)' or override '%s.toString()'")
442+
.formatted(source, key.getClass().getName()));
448443
}
449444

450445
private CompletableFuture<byte[]> retrieveValue(Object key) {
@@ -502,7 +497,7 @@ private String convertCollectionLikeOrMapKey(Object key, TypeDescriptor source)
502497
return "[" + stringJoiner + "]";
503498
}
504499

505-
throw new IllegalArgumentException(String.format("Cannot convert cache key [%s] to String", key));
500+
throw new IllegalArgumentException("Cannot convert cache key [%s] to String".formatted(key));
506501
}
507502

508503
private byte[] createAndConvertCacheKey(Object key) {

src/main/java/org/springframework/data/redis/cache/RedisCacheConfiguration.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -424,11 +424,9 @@ public void addCacheKeyConverter(Converter<?, String> cacheKeyConverter) {
424424
public void configureKeyConverters(Consumer<ConverterRegistry> registryConsumer) {
425425

426426
if (!(getConversionService() instanceof ConverterRegistry)) {
427-
428-
String message = "'%s' returned by getConversionService() does not allow Converter registration;"
429-
+ " Please make sure to provide a ConversionService that implements ConverterRegistry";
430-
431-
throw new IllegalStateException(String.format(message, getConversionService().getClass().getName()));
427+
throw new IllegalStateException(("'%s' returned by getConversionService() does not allow Converter registration;"
428+
+ " Please make sure to provide a ConversionService that implements ConverterRegistry")
429+
.formatted(getConversionService().getClass().getName()));
432430
}
433431

434432
registryConsumer.accept((ConverterRegistry) getConversionService());

src/main/java/org/springframework/data/redis/connection/ClusterCommandExecutor.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,9 @@ private <S, T> NodeResult<T> executeCommandOnSingleNode(ClusterCommandCallback<S
130130
Assert.notNull(node, "RedisClusterNode must not be null");
131131

132132
if (redirectCount > this.maxRedirects) {
133-
134-
String message = String.format("Cannot follow Cluster Redirects over more than %s legs; "
135-
+ "Consider increasing the number of redirects to follow; Current value is: %s.",
136-
redirectCount, this.maxRedirects);
137-
138-
throw new TooManyClusterRedirectionsException(message);
133+
throw new TooManyClusterRedirectionsException(("Cannot follow Cluster Redirects over more than %s legs;"
134+
+ " Consider increasing the number of redirects to follow; Current value is: %s")
135+
.formatted(redirectCount, this.maxRedirects));
139136
}
140137

141138
RedisClusterNode nodeToUse = lookupNode(node);
@@ -178,7 +175,7 @@ private RedisClusterNode lookupNode(RedisClusterNode node) {
178175
try {
179176
return topologyProvider.getTopology().lookup(node);
180177
} catch (ClusterStateFailureException ex) {
181-
throw new IllegalArgumentException(String.format("Node %s is unknown to cluster", node), ex);
178+
throw new IllegalArgumentException("Node %s is unknown to cluster".formatted(node), ex);
182179
}
183180
}
184181

@@ -215,7 +212,7 @@ public <S, T> MultiNodeResult<T> executeCommandAsyncOnNodes(ClusterCommandCallba
215212
try {
216213
resolvedRedisClusterNodes.add(topology.lookup(node));
217214
} catch (ClusterStateFailureException ex) {
218-
throw new IllegalArgumentException(String.format("Node %s is unknown to cluster", node), ex);
215+
throw new IllegalArgumentException("Node %s is unknown to cluster".formatted(node), ex);
219216
}
220217
}
221218

src/main/java/org/springframework/data/redis/connection/ClusterTopology.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ public RedisClusterNode getKeyServingMasterNode(byte[] key) {
140140
}
141141
}
142142

143-
throw new ClusterStateFailureException(
144-
String.format("Could not find master node serving slot %s for key '%s',", slot, Arrays.toString(key)));
143+
throw new ClusterStateFailureException("Could not find master node serving slot %s for key '%s',"
144+
.formatted(slot, Arrays.toString(key)));
145145
}
146146

147147
/**
@@ -160,8 +160,8 @@ public RedisClusterNode lookup(String host, int port) {
160160
}
161161
}
162162

163-
throw new ClusterStateFailureException(
164-
String.format("Could not find node at %s:%s; Is your cluster info up to date", host, port));
163+
throw new ClusterStateFailureException("Could not find node at %s:%d; Is your cluster info up to date"
164+
.formatted(host, port));
165165
}
166166

167167
/**
@@ -181,8 +181,8 @@ public RedisClusterNode lookup(String nodeId) {
181181
}
182182
}
183183

184-
throw new ClusterStateFailureException(
185-
String.format("Could not find node at %s; Is your cluster info up to date", nodeId));
184+
throw new ClusterStateFailureException("Could not find node at %s; Is your cluster info up to date"
185+
.formatted(nodeId));
186186
}
187187

188188
/**
@@ -209,8 +209,8 @@ public RedisClusterNode lookup(RedisClusterNode node) {
209209
return lookup(node.getId());
210210
}
211211

212-
throw new ClusterStateFailureException(
213-
String.format("Could not find node at %s; Have you provided either host and port or the nodeId", node));
212+
throw new ClusterStateFailureException(("Could not find node at %s;"
213+
+ " Have you provided either host and port or the nodeId").formatted(node));
214214
}
215215

216216
/**

src/main/java/org/springframework/data/redis/connection/RedisNode.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,11 @@ public static RedisNode fromString(String hostPortString) {
102102
try {
103103
port = Integer.parseInt(portString);
104104
} catch (RuntimeException ignore) {
105-
throw new IllegalArgumentException(String.format("Unparseable port number: %s", hostPortString));
105+
throw new IllegalArgumentException("Unparseable port number: %s".formatted(hostPortString));
106106
}
107107

108108
if (!isValidPort(port)) {
109-
throw new IllegalArgumentException(String.format("Port number out of range: %s", hostPortString));
109+
throw new IllegalArgumentException("Port number out of range: %s".formatted(hostPortString));
110110
}
111111

112112
return new RedisNode(host, port);
@@ -122,28 +122,28 @@ public static RedisNode fromString(String hostPortString) {
122122
private static String[] getHostAndPortFromBracketedHost(String hostPortString) {
123123

124124
if (hostPortString.charAt(0) != '[') {
125-
throw new IllegalArgumentException(
126-
String.format("Bracketed host-port string must start with a bracket: %s", hostPortString));
125+
throw new IllegalArgumentException("Bracketed host-port string must start with a bracket: %s"
126+
.formatted(hostPortString));
127127
}
128128

129129
int colonIndex = hostPortString.indexOf(':');
130130
int closeBracketIndex = hostPortString.lastIndexOf(']');
131131

132132
if (!(colonIndex > -1 && closeBracketIndex > colonIndex)) {
133-
throw new IllegalArgumentException(String.format("Invalid bracketed host/port: %s", hostPortString));
133+
throw new IllegalArgumentException("Invalid bracketed host/port: %s".formatted(hostPortString));
134134
}
135135

136136
String host = hostPortString.substring(1, closeBracketIndex);
137137
if (closeBracketIndex + 1 == hostPortString.length()) {
138138
return new String[] { host, "" };
139139
} else {
140140
if (!(hostPortString.charAt(closeBracketIndex + 1) == ':')) {
141-
throw new IllegalArgumentException(
142-
String.format("Only a colon may follow a close bracket: %s", hostPortString));
141+
throw new IllegalArgumentException("Only a colon may follow a close bracket: %s"
142+
.formatted(hostPortString));
143143
}
144144
for (int i = closeBracketIndex + 2; i < hostPortString.length(); ++i) {
145145
if (!Character.isDigit(hostPortString.charAt(i))) {
146-
throw new IllegalArgumentException(String.format("Port must be numeric: %s", hostPortString));
146+
throw new IllegalArgumentException("Port must be numeric: %s".formatted(hostPortString));
147147
}
148148
}
149149
return new String[] { host, hostPortString.substring(closeBracketIndex + 2) };

src/main/java/org/springframework/data/redis/connection/RedisPassword.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public Optional<char[]> toOptional() {
140140

141141
@Override
142142
public String toString() {
143-
return String.format("%s[%s]", getClass().getSimpleName(), isPresent() ? "*****" : "<none>");
143+
return "%s[%s]".formatted(getClass().getSimpleName(), isPresent() ? "*****" : "<none>");
144144
}
145145

146146
@Override

src/main/java/org/springframework/data/redis/connection/RedisSentinelConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ public int getDatabase() {
223223
@Override
224224
public void setDatabase(int index) {
225225

226-
Assert.isTrue(index >= 0, () -> String.format("Invalid DB index '%d'; non-negative index required", index));
226+
Assert.isTrue(index >= 0, () -> "Invalid DB index '%d'; non-negative index required".formatted(index));
227227

228228
this.database = index;
229229
}

src/main/java/org/springframework/data/redis/connection/RedisSocketConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public int getDatabase() {
7474
@Override
7575
public void setDatabase(int index) {
7676

77-
Assert.isTrue(index >= 0, () -> String.format("Invalid DB index '%s' (a positive index required)", index));
77+
Assert.isTrue(index >= 0, () -> "Invalid DB index '%s'; non-negative index required".formatted(index));
7878

7979
this.database = index;
8080
}

0 commit comments

Comments
 (0)