Skip to content

Commit c0bd19a

Browse files
committed
Refine use of substring operations
Closes gh-25445
1 parent 14b1d20 commit c0bd19a

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
@@ -656,7 +656,7 @@ private PointcutBody getPointcutBody(String[] tokens, int startIndex) {
656656
}
657657

658658
if (tokens[currentIndex].endsWith(")")) {
659-
sb.append(tokens[currentIndex].substring(0, tokens[currentIndex].length() - 1));
659+
sb.append(tokens[currentIndex], 0, tokens[currentIndex].length() - 1);
660660
return new PointcutBody(numTokensConsumed, sb.toString().trim());
661661
}
662662

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-2018 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.
@@ -199,7 +199,7 @@ public static MimeType parseMimeType(String mimeType) {
199199
throw new InvalidMimeTypeException(mimeType, "does not contain subtype after '/'");
200200
}
201201
String type = fullType.substring(0, subIndex);
202-
String subtype = fullType.substring(subIndex + 1, fullType.length());
202+
String subtype = fullType.substring(subIndex + 1);
203203
if (MimeType.WILDCARD_TYPE.equals(type) && !MimeType.WILDCARD_TYPE.equals(subtype)) {
204204
throw new InvalidMimeTypeException(mimeType, "wildcard type is legal only in '*/*' (all mime types)");
205205
}

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.
@@ -309,7 +309,7 @@ public void setConcurrency(String concurrency) {
309309
int separatorIndex = concurrency.indexOf('-');
310310
if (separatorIndex != -1) {
311311
setConcurrentConsumers(Integer.parseInt(concurrency.substring(0, separatorIndex)));
312-
setMaxConcurrentConsumers(Integer.parseInt(concurrency.substring(separatorIndex + 1, concurrency.length())));
312+
setMaxConcurrentConsumers(Integer.parseInt(concurrency.substring(separatorIndex + 1)));
313313
}
314314
else {
315315
setConcurrentConsumers(1);
@@ -383,8 +383,7 @@ public final int getConcurrentConsumers() {
383383
public void setMaxConcurrentConsumers(int maxConcurrentConsumers) {
384384
Assert.isTrue(maxConcurrentConsumers > 0, "'maxConcurrentConsumers' value must be at least 1 (one)");
385385
synchronized (this.lifecycleMonitor) {
386-
this.maxConcurrentConsumers =
387-
(maxConcurrentConsumers > this.concurrentConsumers ? maxConcurrentConsumers : this.concurrentConsumers);
386+
this.maxConcurrentConsumers = Math.max(maxConcurrentConsumers, this.concurrentConsumers);
388387
}
389388
}
390389

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
@@ -108,7 +108,7 @@ public void setConcurrency(String concurrency) {
108108
try {
109109
int separatorIndex = concurrency.indexOf('-');
110110
if (separatorIndex != -1) {
111-
setConcurrentConsumers(Integer.parseInt(concurrency.substring(separatorIndex + 1, concurrency.length())));
111+
setConcurrentConsumers(Integer.parseInt(concurrency.substring(separatorIndex + 1)));
112112
}
113113
else {
114114
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-2017 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.
@@ -223,7 +223,7 @@ else if (sb != null){
223223
private StringBuilder getStringBuilder(@Nullable StringBuilder sb, String inString, int i) {
224224
if (sb == null) {
225225
sb = new StringBuilder(inString.length());
226-
sb.append(inString.substring(0, i));
226+
sb.append(inString, 0, i);
227227
}
228228
return sb;
229229
}

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.
@@ -72,7 +72,7 @@ public PatternMappingFilterProxy(Filter delegate, String... urlPatterns) {
7272
private void addUrlPattern(String urlPattern) {
7373
Assert.notNull(urlPattern, "Found null URL Pattern");
7474
if (urlPattern.startsWith(EXTENSION_MAPPING_PATTERN)) {
75-
this.endsWithMatches.add(urlPattern.substring(1, urlPattern.length()));
75+
this.endsWithMatches.add(urlPattern.substring(1));
7676
}
7777
else if (urlPattern.equals(PATH_MAPPING_PATTERN)) {
7878
this.startsWithMatches.add("");
@@ -82,7 +82,7 @@ else if (urlPattern.endsWith(PATH_MAPPING_PATTERN)) {
8282
this.exactMatches.add(urlPattern.substring(0, urlPattern.length() - 2));
8383
}
8484
else {
85-
if ("".equals(urlPattern)) {
85+
if (urlPattern.isEmpty()) {
8686
urlPattern = "/";
8787
}
8888
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-2017 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
@@ -277,7 +277,7 @@ String createUrl() throws JspException {
277277
}
278278
else {
279279
if (this.context.endsWith("/")) {
280-
url.append(this.context.substring(0, this.context.length() - 1));
280+
url.append(this.context, 0, this.context.length() - 1);
281281
}
282282
else {
283283
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
@@ -56,7 +56,7 @@
5656
* but this behavior can be changed by overriding the
5757
* {@link #isEligibleProperty(String, Object)} method.
5858
*
59-
* <p>A URL for this view is supposed to be a HTTP redirect URL, i.e.
59+
* <p>A URL for this view is supposed to be an HTTP redirect URL, i.e.
6060
* suitable for HttpServletResponse's {@code sendRedirect} method, which
6161
* is what actually does the redirect if the HTTP 1.0 flag is on, or via sending
6262
* back an HTTP 303 code - if the HTTP 1.0 compatibility flag is off.
@@ -386,7 +386,7 @@ protected StringBuilder replaceUriTemplateVariables(
386386
if (value == null) {
387387
throw new IllegalArgumentException("Model has no value for key '" + name + "'");
388388
}
389-
result.append(targetUrl.substring(endLastMatch, matcher.start()));
389+
result.append(targetUrl, endLastMatch, matcher.start());
390390
result.append(UriUtils.encodePathSegment(value.toString(), encodingScheme));
391391
endLastMatch = matcher.end();
392392
}

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)