Skip to content

Commit d7959ed

Browse files
committed
Improve performance of some string operations
Issue: SPR-16293
1 parent 0b77c88 commit d7959ed

File tree

5 files changed

+25
-26
lines changed

5 files changed

+25
-26
lines changed

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

Lines changed: 4 additions & 4 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-2017 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.
@@ -339,10 +339,10 @@ Class<?> getBeanClass() {
339339
PropertyDescriptor getPropertyDescriptor(String name) {
340340
PropertyDescriptor pd = this.propertyDescriptorCache.get(name);
341341
if (pd == null && StringUtils.hasLength(name)) {
342-
// Same lenient fallback checking as in PropertyTypeDescriptor...
343-
pd = this.propertyDescriptorCache.get(name.substring(0, 1).toLowerCase() + name.substring(1));
342+
// Same lenient fallback checking as in Property...
343+
pd = this.propertyDescriptorCache.get(StringUtils.uncapitalize(name));
344344
if (pd == null) {
345-
pd = this.propertyDescriptorCache.get(name.substring(0, 1).toUpperCase() + name.substring(1));
345+
pd = this.propertyDescriptorCache.get(StringUtils.capitalize(name));
346346
}
347347
}
348348
return (pd == null || pd instanceof GenericTypeAwarePropertyDescriptor ? pd :

spring-core/src/main/java/org/springframework/core/convert/Property.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,11 +225,9 @@ private Field getField() {
225225
Field field = ReflectionUtils.findField(declaringClass, name);
226226
if (field == null) {
227227
// Same lenient fallback checking as in CachedIntrospectionResults...
228-
field = ReflectionUtils.findField(declaringClass,
229-
name.substring(0, 1).toLowerCase() + name.substring(1));
228+
field = ReflectionUtils.findField(declaringClass, StringUtils.uncapitalize(name));
230229
if (field == null) {
231-
field = ReflectionUtils.findField(declaringClass,
232-
name.substring(0, 1).toUpperCase() + name.substring(1));
230+
field = ReflectionUtils.findField(declaringClass, StringUtils.capitalize(name));
233231
}
234232
}
235233
return field;

spring-core/src/main/java/org/springframework/core/io/support/PathMatchingResourcePatternResolver.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ protected void addClassPathManifestEntries(Set<Resource> result) {
424424
int prefixIndex = filePath.indexOf(':');
425425
if (prefixIndex == 1) {
426426
// Possibly "c:" drive prefix on Windows, to be upper-cased for proper duplicate detection
427-
filePath = filePath.substring(0, 1).toUpperCase() + filePath.substring(1);
427+
filePath = StringUtils.capitalize(filePath);
428428
}
429429
UrlResource jarResource = new UrlResource(ResourceUtils.JAR_URL_PREFIX +
430430
ResourceUtils.FILE_URL_PREFIX + filePath + ResourceUtils.JAR_URL_SEPARATOR);
@@ -489,18 +489,18 @@ protected Resource[] findPathMatchingResources(String locationPattern) throws IO
489489
Set<Resource> result = new LinkedHashSet<Resource>(16);
490490
for (Resource rootDirResource : rootDirResources) {
491491
rootDirResource = resolveRootDirResource(rootDirResource);
492-
URL rootDirURL = rootDirResource.getURL();
492+
URL rootDirUrl = rootDirResource.getURL();
493493
if (equinoxResolveMethod != null) {
494-
if (rootDirURL.getProtocol().startsWith("bundle")) {
495-
rootDirURL = (URL) ReflectionUtils.invokeMethod(equinoxResolveMethod, null, rootDirURL);
496-
rootDirResource = new UrlResource(rootDirURL);
494+
if (rootDirUrl.getProtocol().startsWith("bundle")) {
495+
rootDirUrl = (URL) ReflectionUtils.invokeMethod(equinoxResolveMethod, null, rootDirUrl);
496+
rootDirResource = new UrlResource(rootDirUrl);
497497
}
498498
}
499-
if (rootDirURL.getProtocol().startsWith(ResourceUtils.URL_PROTOCOL_VFS)) {
500-
result.addAll(VfsResourceMatchingDelegate.findMatchingResources(rootDirURL, subPattern, getPathMatcher()));
499+
if (rootDirUrl.getProtocol().startsWith(ResourceUtils.URL_PROTOCOL_VFS)) {
500+
result.addAll(VfsResourceMatchingDelegate.findMatchingResources(rootDirUrl, subPattern, getPathMatcher()));
501501
}
502-
else if (ResourceUtils.isJarURL(rootDirURL) || isJarResource(rootDirResource)) {
503-
result.addAll(doFindPathMatchingJarResources(rootDirResource, rootDirURL, subPattern));
502+
else if (ResourceUtils.isJarURL(rootDirUrl) || isJarResource(rootDirResource)) {
503+
result.addAll(doFindPathMatchingJarResources(rootDirResource, rootDirUrl, subPattern));
504504
}
505505
else {
506506
result.addAll(doFindPathMatchingFileResources(rootDirResource, subPattern));

spring-jdbc/src/main/java/org/springframework/jdbc/support/JdbcUtils.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -481,24 +481,24 @@ public static String convertUnderscoreNameToPropertyName(String name) {
481481
StringBuilder result = new StringBuilder();
482482
boolean nextIsUpper = false;
483483
if (name != null && name.length() > 0) {
484-
if (name.length() > 1 && name.substring(1, 2).equals("_")) {
485-
result.append(name.substring(0, 1).toUpperCase());
484+
if (name.length() > 1 && name.charAt(1) == '_') {
485+
result.append(Character.toUpperCase(name.charAt(0)));
486486
}
487487
else {
488-
result.append(name.substring(0, 1).toLowerCase());
488+
result.append(Character.toLowerCase(name.charAt(0)));
489489
}
490490
for (int i = 1; i < name.length(); i++) {
491-
String s = name.substring(i, i + 1);
492-
if (s.equals("_")) {
491+
char c = name.charAt(i);
492+
if (c == '_') {
493493
nextIsUpper = true;
494494
}
495495
else {
496496
if (nextIsUpper) {
497-
result.append(s.toUpperCase());
497+
result.append(Character.toUpperCase(c));
498498
nextIsUpper = false;
499499
}
500500
else {
501-
result.append(s.toLowerCase());
501+
result.append(Character.toLowerCase(c));
502502
}
503503
}
504504
}

spring-web/src/main/java/org/springframework/web/multipart/support/StandardServletMultipartResolver.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import org.apache.commons.logging.LogFactory;
2323

24+
import org.springframework.util.StringUtils;
2425
import org.springframework.web.multipart.MultipartException;
2526
import org.springframework.web.multipart.MultipartHttpServletRequest;
2627
import org.springframework.web.multipart.MultipartResolver;
@@ -67,11 +68,11 @@ public void setResolveLazily(boolean resolveLazily) {
6768
@Override
6869
public boolean isMultipart(HttpServletRequest request) {
6970
// Same check as in Commons FileUpload...
70-
if (!"post".equals(request.getMethod().toLowerCase())) {
71+
if (!"post".equalsIgnoreCase(request.getMethod())) {
7172
return false;
7273
}
7374
String contentType = request.getContentType();
74-
return (contentType != null && contentType.toLowerCase().startsWith("multipart/"));
75+
return StringUtils.startsWithIgnoreCase(contentType, "multipart/");
7576
}
7677

7778
@Override

0 commit comments

Comments
 (0)