Skip to content

Commit ce4001d

Browse files
committed
Refine use of substring operations
Closes gh-25445
1 parent b254777 commit ce4001d

File tree

15 files changed

+39
-35
lines changed

15 files changed

+39
-35
lines changed

spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJAdviceParameterNameDiscoverer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,7 @@ private PointcutBody getPointcutBody(String[] tokens, int startIndex) {
648648
}
649649

650650
if (tokens[currentIndex].endsWith(")")) {
651-
sb.append(tokens[currentIndex].substring(0, tokens[currentIndex].length() - 1));
651+
sb.append(tokens[currentIndex], 0, tokens[currentIndex].length() - 1);
652652
return new PointcutBody(numTokensConsumed, sb.toString().trim());
653653
}
654654

spring-beans/src/main/java/org/springframework/beans/PropertyEditorRegistrySupport.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ private void addStrippedPropertyPaths(List<String> strippedPaths, String nestedP
501501
if (endIndex != -1) {
502502
String prefix = propertyPath.substring(0, startIndex);
503503
String key = propertyPath.substring(startIndex, endIndex + 1);
504-
String suffix = propertyPath.substring(endIndex + 1, propertyPath.length());
504+
String suffix = propertyPath.substring(endIndex + 1);
505505
// Strip the first key.
506506
strippedPaths.add(nestedPath + prefix + suffix);
507507
// Search for further keys to strip, with the first key stripped.

spring-context/src/main/java/org/springframework/scheduling/config/TaskExecutorFactoryBean.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ private void determinePoolSizeRange(ThreadPoolTaskExecutor executor) {
106106
int maxPoolSize;
107107
int separatorIndex = this.poolSize.indexOf('-');
108108
if (separatorIndex != -1) {
109-
corePoolSize = Integer.valueOf(this.poolSize.substring(0, separatorIndex));
110-
maxPoolSize = Integer.valueOf(this.poolSize.substring(separatorIndex + 1, this.poolSize.length()));
109+
corePoolSize = Integer.parseInt(this.poolSize.substring(0, separatorIndex));
110+
maxPoolSize = Integer.parseInt(this.poolSize.substring(separatorIndex + 1));
111111
if (corePoolSize > maxPoolSize) {
112112
throw new IllegalArgumentException(
113113
"Lower bound of pool-size range must not exceed the upper bound");

spring-core/src/main/java/org/springframework/util/MimeTypeUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -201,7 +201,7 @@ public static MimeType parseMimeType(String mimeType) {
201201
throw new InvalidMimeTypeException(mimeType, "does not contain subtype after '/'");
202202
}
203203
String type = fullType.substring(0, subIndex);
204-
String subtype = fullType.substring(subIndex + 1, fullType.length());
204+
String subtype = fullType.substring(subIndex + 1);
205205
if (MimeType.WILDCARD_TYPE.equals(type) && !MimeType.WILDCARD_TYPE.equals(subtype)) {
206206
throw new InvalidMimeTypeException(mimeType, "wildcard type is legal only in '*/*' (all mime types)");
207207
}

spring-jms/src/main/java/org/springframework/jms/listener/DefaultMessageListenerContainer.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -310,7 +310,7 @@ public void setConcurrency(String concurrency) {
310310
int separatorIndex = concurrency.indexOf('-');
311311
if (separatorIndex != -1) {
312312
setConcurrentConsumers(Integer.parseInt(concurrency.substring(0, separatorIndex)));
313-
setMaxConcurrentConsumers(Integer.parseInt(concurrency.substring(separatorIndex + 1, concurrency.length())));
313+
setMaxConcurrentConsumers(Integer.parseInt(concurrency.substring(separatorIndex + 1)));
314314
}
315315
else {
316316
setConcurrentConsumers(1);
@@ -384,8 +384,7 @@ public final int getConcurrentConsumers() {
384384
public void setMaxConcurrentConsumers(int maxConcurrentConsumers) {
385385
Assert.isTrue(maxConcurrentConsumers > 0, "'maxConcurrentConsumers' value must be at least 1 (one)");
386386
synchronized (this.lifecycleMonitor) {
387-
this.maxConcurrentConsumers =
388-
(maxConcurrentConsumers > this.concurrentConsumers ? maxConcurrentConsumers : this.concurrentConsumers);
387+
this.maxConcurrentConsumers = Math.max(maxConcurrentConsumers, this.concurrentConsumers);
389388
}
390389
}
391390

spring-jms/src/main/java/org/springframework/jms/listener/SimpleMessageListenerContainer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public void setConcurrency(String concurrency) {
126126
try {
127127
int separatorIndex = concurrency.indexOf('-');
128128
if (separatorIndex != -1) {
129-
setConcurrentConsumers(Integer.parseInt(concurrency.substring(separatorIndex + 1, concurrency.length())));
129+
setConcurrentConsumers(Integer.parseInt(concurrency.substring(separatorIndex + 1)));
130130
}
131131
else {
132132
setConcurrentConsumers(Integer.parseInt(concurrency));

spring-jms/src/main/java/org/springframework/jms/listener/endpoint/JmsActivationSpecConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ public void setConcurrency(String concurrency) {
226226
try {
227227
int separatorIndex = concurrency.indexOf('-');
228228
if (separatorIndex != -1) {
229-
setMaxConcurrency(Integer.parseInt(concurrency.substring(separatorIndex + 1, concurrency.length())));
229+
setMaxConcurrency(Integer.parseInt(concurrency.substring(separatorIndex + 1)));
230230
}
231231
else {
232232
setMaxConcurrency(Integer.parseInt(concurrency));

spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompDecoder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -263,7 +263,7 @@ private String unescape(String inString) {
263263
int index = inString.indexOf('\\');
264264

265265
while (index >= 0) {
266-
sb.append(inString.substring(pos, index));
266+
sb.append(inString, pos, index);
267267
if (index + 1 >= inString.length()) {
268268
throw new StompConversionException("Illegal escape sequence at index " + index + ": " + inString);
269269
}

spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompEncoder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -224,7 +224,7 @@ else if (sb != null){
224224
private StringBuilder getStringBuilder(@Nullable StringBuilder sb, String inString, int i) {
225225
if (sb == null) {
226226
sb = new StringBuilder(inString.length());
227-
sb.append(inString.substring(0, i));
227+
sb.append(inString, 0, i);
228228
}
229229
return sb;
230230
}

spring-test/src/main/java/org/springframework/test/web/servlet/setup/PatternMappingFilterProxy.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -73,7 +73,7 @@ public PatternMappingFilterProxy(Filter delegate, String... urlPatterns) {
7373
private void addUrlPattern(String urlPattern) {
7474
Assert.notNull(urlPattern, "Found null URL Pattern");
7575
if (urlPattern.startsWith(EXTENSION_MAPPING_PATTERN)) {
76-
this.endsWithMatches.add(urlPattern.substring(1, urlPattern.length()));
76+
this.endsWithMatches.add(urlPattern.substring(1));
7777
}
7878
else if (urlPattern.equals(PATH_MAPPING_PATTERN)) {
7979
this.startsWithMatches.add("");
@@ -83,7 +83,7 @@ else if (urlPattern.endsWith(PATH_MAPPING_PATTERN)) {
8383
this.exactMatches.add(urlPattern.substring(0, urlPattern.length() - 2));
8484
}
8585
else {
86-
if ("".equals(urlPattern)) {
86+
if (urlPattern.isEmpty()) {
8787
urlPattern = "/";
8888
}
8989
this.exactMatches.add(urlPattern);

spring-web/src/main/java/org/springframework/web/util/UriTemplate.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -30,14 +30,19 @@
3030
import org.springframework.util.Assert;
3131

3232
/**
33-
* Represents a URI template. A URI template is a URI-like String that contains variables
34-
* enclosed by braces ({@code {}}) which can be expanded to produce an actual URI.
33+
* Representation of a URI template that can be expanded with URI variables via
34+
* {@link #expand(Map)}, {@link #expand(Object[])}, or matched to a URL via
35+
* {@link #match(String)}. This class is designed to be thread-safe and
36+
* reusable, and allows any number of expand or match calls.
3537
*
36-
* <p>See {@link #expand(Map)}, {@link #expand(Object[])}, and {@link #match(String)}
37-
* for example usages.
38-
*
39-
* <p>This class is designed to be thread-safe and reusable, allowing for any number
40-
* of expand or match calls.
38+
* <p><strong>Note:</strong> this class uses {@link UriComponentsBuilder}
39+
* internally to expand URI templates, and is merely a shortcut for already
40+
* prepared URI templates. For more dynamic preparation and extra flexibility,
41+
* e.g. around URI encoding, consider using {@code UriComponentsBuilder} or the
42+
* higher level {@link DefaultUriBuilderFactory} which adds several encoding
43+
* modes on top of {@code UriComponentsBuilder}. See the
44+
* <a href="https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-uri-building">reference docs</a>
45+
* for further details.
4146
*
4247
* @author Arjen Poutsma
4348
* @author Juergen Hoeller
@@ -220,7 +225,7 @@ else if (c == '}') {
220225
throw new IllegalArgumentException(
221226
"No custom regular expression specified after ':' in \"" + variable + "\"");
222227
}
223-
String regex = variable.substring(idx + 1, variable.length());
228+
String regex = variable.substring(idx + 1);
224229
pattern.append('(');
225230
pattern.append(regex);
226231
pattern.append(')');

spring-webflux/src/main/java/org/springframework/web/reactive/result/view/RedirectView.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -245,12 +245,12 @@ protected StringBuilder expandTargetUrlTemplate(String targetUrl,
245245
String name = matcher.group(1);
246246
Object value = (model.containsKey(name) ? model.get(name) : uriVariables.get(name));
247247
Assert.notNull(value, () -> "No value for URI variable '" + name + "'");
248-
result.append(targetUrl.substring(endLastMatch, matcher.start()));
248+
result.append(targetUrl, endLastMatch, matcher.start());
249249
result.append(encodeUriVariable(value.toString()));
250250
endLastMatch = matcher.end();
251251
found = matcher.find();
252252
}
253-
result.append(targetUrl.substring(endLastMatch, targetUrl.length()));
253+
result.append(targetUrl.substring(endLastMatch));
254254
return result;
255255
}
256256

spring-webmvc/src/main/java/org/springframework/web/servlet/tags/UrlTag.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ String createUrl() throws JspException {
278278
}
279279
else {
280280
if (this.context.endsWith("/")) {
281-
url.append(this.context.substring(0, this.context.length() - 1));
281+
url.append(this.context, 0, this.context.length() - 1);
282282
}
283283
else {
284284
url.append(this.context);

spring-webmvc/src/main/java/org/springframework/web/servlet/view/RedirectView.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
* but this behavior can be changed by overriding the
5858
* {@link #isEligibleProperty(String, Object)} method.
5959
*
60-
* <p>A URL for this view is supposed to be a HTTP redirect URL, i.e.
60+
* <p>A URL for this view is supposed to be an HTTP redirect URL, i.e.
6161
* suitable for HttpServletResponse's {@code sendRedirect} method, which
6262
* is what actually does the redirect if the HTTP 1.0 flag is on, or via sending
6363
* back an HTTP 303 code - if the HTTP 1.0 compatibility flag is off.
@@ -387,7 +387,7 @@ protected StringBuilder replaceUriTemplateVariables(
387387
if (value == null) {
388388
throw new IllegalArgumentException("Model has no value for key '" + name + "'");
389389
}
390-
result.append(targetUrl.substring(endLastMatch, matcher.start()));
390+
result.append(targetUrl, endLastMatch, matcher.start());
391391
result.append(UriUtils.encodePathSegment(value.toString(), encodingScheme));
392392
endLastMatch = matcher.end();
393393
}

spring-websocket/src/main/java/org/springframework/web/socket/WebSocketExtension.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ private static WebSocketExtension parseExtension(String extension) {
160160
int eqIndex = parameter.indexOf('=');
161161
if (eqIndex != -1) {
162162
String attribute = parameter.substring(0, eqIndex);
163-
String value = parameter.substring(eqIndex + 1, parameter.length());
163+
String value = parameter.substring(eqIndex + 1);
164164
parameters.put(attribute, value);
165165
}
166166
}

0 commit comments

Comments
 (0)