From d7959edb3ea1a004d58e989c1a99a53f04d77243 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Fri, 19 Jan 2018 19:05:07 +0100 Subject: [PATCH] Improve performance of some string operations Issue: SPR-16293 --- .../beans/CachedIntrospectionResults.java | 8 ++++---- .../springframework/core/convert/Property.java | 6 ++---- .../PathMatchingResourcePatternResolver.java | 18 +++++++++--------- .../jdbc/support/JdbcUtils.java | 14 +++++++------- .../StandardServletMultipartResolver.java | 5 +++-- 5 files changed, 25 insertions(+), 26 deletions(-) diff --git a/spring-beans/src/main/java/org/springframework/beans/CachedIntrospectionResults.java b/spring-beans/src/main/java/org/springframework/beans/CachedIntrospectionResults.java index 2efe9dabeb..a9a01f2bf4 100644 --- a/spring-beans/src/main/java/org/springframework/beans/CachedIntrospectionResults.java +++ b/spring-beans/src/main/java/org/springframework/beans/CachedIntrospectionResults.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -339,10 +339,10 @@ public class CachedIntrospectionResults { PropertyDescriptor getPropertyDescriptor(String name) { PropertyDescriptor pd = this.propertyDescriptorCache.get(name); if (pd == null && StringUtils.hasLength(name)) { - // Same lenient fallback checking as in PropertyTypeDescriptor... - pd = this.propertyDescriptorCache.get(name.substring(0, 1).toLowerCase() + name.substring(1)); + // Same lenient fallback checking as in Property... + pd = this.propertyDescriptorCache.get(StringUtils.uncapitalize(name)); if (pd == null) { - pd = this.propertyDescriptorCache.get(name.substring(0, 1).toUpperCase() + name.substring(1)); + pd = this.propertyDescriptorCache.get(StringUtils.capitalize(name)); } } return (pd == null || pd instanceof GenericTypeAwarePropertyDescriptor ? pd : diff --git a/spring-core/src/main/java/org/springframework/core/convert/Property.java b/spring-core/src/main/java/org/springframework/core/convert/Property.java index e966e7366c..9c6db43c76 100644 --- a/spring-core/src/main/java/org/springframework/core/convert/Property.java +++ b/spring-core/src/main/java/org/springframework/core/convert/Property.java @@ -225,11 +225,9 @@ public final class Property { Field field = ReflectionUtils.findField(declaringClass, name); if (field == null) { // Same lenient fallback checking as in CachedIntrospectionResults... - field = ReflectionUtils.findField(declaringClass, - name.substring(0, 1).toLowerCase() + name.substring(1)); + field = ReflectionUtils.findField(declaringClass, StringUtils.uncapitalize(name)); if (field == null) { - field = ReflectionUtils.findField(declaringClass, - name.substring(0, 1).toUpperCase() + name.substring(1)); + field = ReflectionUtils.findField(declaringClass, StringUtils.capitalize(name)); } } return field; diff --git a/spring-core/src/main/java/org/springframework/core/io/support/PathMatchingResourcePatternResolver.java b/spring-core/src/main/java/org/springframework/core/io/support/PathMatchingResourcePatternResolver.java index 41d2da58b6..f9ae32cf31 100644 --- a/spring-core/src/main/java/org/springframework/core/io/support/PathMatchingResourcePatternResolver.java +++ b/spring-core/src/main/java/org/springframework/core/io/support/PathMatchingResourcePatternResolver.java @@ -424,7 +424,7 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol int prefixIndex = filePath.indexOf(':'); if (prefixIndex == 1) { // Possibly "c:" drive prefix on Windows, to be upper-cased for proper duplicate detection - filePath = filePath.substring(0, 1).toUpperCase() + filePath.substring(1); + filePath = StringUtils.capitalize(filePath); } UrlResource jarResource = new UrlResource(ResourceUtils.JAR_URL_PREFIX + ResourceUtils.FILE_URL_PREFIX + filePath + ResourceUtils.JAR_URL_SEPARATOR); @@ -489,18 +489,18 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol Set result = new LinkedHashSet(16); for (Resource rootDirResource : rootDirResources) { rootDirResource = resolveRootDirResource(rootDirResource); - URL rootDirURL = rootDirResource.getURL(); + URL rootDirUrl = rootDirResource.getURL(); if (equinoxResolveMethod != null) { - if (rootDirURL.getProtocol().startsWith("bundle")) { - rootDirURL = (URL) ReflectionUtils.invokeMethod(equinoxResolveMethod, null, rootDirURL); - rootDirResource = new UrlResource(rootDirURL); + if (rootDirUrl.getProtocol().startsWith("bundle")) { + rootDirUrl = (URL) ReflectionUtils.invokeMethod(equinoxResolveMethod, null, rootDirUrl); + rootDirResource = new UrlResource(rootDirUrl); } } - if (rootDirURL.getProtocol().startsWith(ResourceUtils.URL_PROTOCOL_VFS)) { - result.addAll(VfsResourceMatchingDelegate.findMatchingResources(rootDirURL, subPattern, getPathMatcher())); + if (rootDirUrl.getProtocol().startsWith(ResourceUtils.URL_PROTOCOL_VFS)) { + result.addAll(VfsResourceMatchingDelegate.findMatchingResources(rootDirUrl, subPattern, getPathMatcher())); } - else if (ResourceUtils.isJarURL(rootDirURL) || isJarResource(rootDirResource)) { - result.addAll(doFindPathMatchingJarResources(rootDirResource, rootDirURL, subPattern)); + else if (ResourceUtils.isJarURL(rootDirUrl) || isJarResource(rootDirResource)) { + result.addAll(doFindPathMatchingJarResources(rootDirResource, rootDirUrl, subPattern)); } else { result.addAll(doFindPathMatchingFileResources(rootDirResource, subPattern)); diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/support/JdbcUtils.java b/spring-jdbc/src/main/java/org/springframework/jdbc/support/JdbcUtils.java index 061d36c16d..c01972bd10 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/support/JdbcUtils.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/support/JdbcUtils.java @@ -481,24 +481,24 @@ public abstract class JdbcUtils { StringBuilder result = new StringBuilder(); boolean nextIsUpper = false; if (name != null && name.length() > 0) { - if (name.length() > 1 && name.substring(1, 2).equals("_")) { - result.append(name.substring(0, 1).toUpperCase()); + if (name.length() > 1 && name.charAt(1) == '_') { + result.append(Character.toUpperCase(name.charAt(0))); } else { - result.append(name.substring(0, 1).toLowerCase()); + result.append(Character.toLowerCase(name.charAt(0))); } for (int i = 1; i < name.length(); i++) { - String s = name.substring(i, i + 1); - if (s.equals("_")) { + char c = name.charAt(i); + if (c == '_') { nextIsUpper = true; } else { if (nextIsUpper) { - result.append(s.toUpperCase()); + result.append(Character.toUpperCase(c)); nextIsUpper = false; } else { - result.append(s.toLowerCase()); + result.append(Character.toLowerCase(c)); } } } diff --git a/spring-web/src/main/java/org/springframework/web/multipart/support/StandardServletMultipartResolver.java b/spring-web/src/main/java/org/springframework/web/multipart/support/StandardServletMultipartResolver.java index 370d1c81cf..15e9cc48eb 100644 --- a/spring-web/src/main/java/org/springframework/web/multipart/support/StandardServletMultipartResolver.java +++ b/spring-web/src/main/java/org/springframework/web/multipart/support/StandardServletMultipartResolver.java @@ -21,6 +21,7 @@ import javax.servlet.http.Part; import org.apache.commons.logging.LogFactory; +import org.springframework.util.StringUtils; import org.springframework.web.multipart.MultipartException; import org.springframework.web.multipart.MultipartHttpServletRequest; import org.springframework.web.multipart.MultipartResolver; @@ -67,11 +68,11 @@ public class StandardServletMultipartResolver implements MultipartResolver { @Override public boolean isMultipart(HttpServletRequest request) { // Same check as in Commons FileUpload... - if (!"post".equals(request.getMethod().toLowerCase())) { + if (!"post".equalsIgnoreCase(request.getMethod())) { return false; } String contentType = request.getContentType(); - return (contentType != null && contentType.toLowerCase().startsWith("multipart/")); + return StringUtils.startsWithIgnoreCase(contentType, "multipart/"); } @Override