From 04df9b8f49ecb246e99404f217804316c3ec9336 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 25 Aug 2020 16:17:12 +0200 Subject: [PATCH] Efficient checks for empty strings and single character matches Closes gh-25552 Closes gh-25553 --- .../config/ConstructorArgumentValues.java | 22 ++++++++++--------- .../PropertiesBeanDefinitionReader.java | 4 ++-- .../xml/BeanDefinitionParserDelegate.java | 4 ++-- .../validation/AbstractBindingResult.java | 4 ++-- .../SpringValidatorAdapter.java | 4 ++-- .../PathMatchingResourcePatternResolver.java | 6 ++--- .../org/springframework/util/StringUtils.java | 12 +++++++++- .../AbstractListenerContainerParser.java | 4 ++-- .../config/JmsListenerContainerParser.java | 12 +++++----- .../test/context/jdbc/MergedSqlConfig.java | 4 ++-- .../http/server/DefaultRequestPath.java | 4 ++-- .../web/util/UrlPathHelper.java | 4 ++-- .../handler/AbstractUrlHandlerMapping.java | 3 ++- .../resource/ResourceUrlEncodingFilter.java | 3 ++- .../web/servlet/tags/form/ErrorsTag.java | 4 ++-- 15 files changed, 54 insertions(+), 40 deletions(-) diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/config/ConstructorArgumentValues.java b/spring-beans/src/main/java/org/springframework/beans/factory/config/ConstructorArgumentValues.java index 45da25fa84d..4fbfb435875 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/config/ConstructorArgumentValues.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/config/ConstructorArgumentValues.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 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. @@ -164,10 +164,10 @@ public class ConstructorArgumentValues { Assert.isTrue(index >= 0, "Index must not be negative"); ValueHolder valueHolder = this.indexedArgumentValues.get(index); if (valueHolder != null && - (valueHolder.getType() == null || - (requiredType != null && ClassUtils.matchesTypeName(requiredType, valueHolder.getType()))) && - (valueHolder.getName() == null || "".equals(requiredName) || - (requiredName != null && requiredName.equals(valueHolder.getName())))) { + (valueHolder.getType() == null || (requiredType != null && + ClassUtils.matchesTypeName(requiredType, valueHolder.getType()))) && + (valueHolder.getName() == null || (requiredName != null && + (requiredName.isEmpty() || requiredName.equals(valueHolder.getName()))))) { return valueHolder; } return null; @@ -277,17 +277,19 @@ public class ConstructorArgumentValues { * @return the ValueHolder for the argument, or {@code null} if none found */ @Nullable - public ValueHolder getGenericArgumentValue(@Nullable Class requiredType, @Nullable String requiredName, @Nullable Set usedValueHolders) { + public ValueHolder getGenericArgumentValue(@Nullable Class requiredType, @Nullable String requiredName, + @Nullable Set usedValueHolders) { + for (ValueHolder valueHolder : this.genericArgumentValues) { if (usedValueHolders != null && usedValueHolders.contains(valueHolder)) { continue; } - if (valueHolder.getName() != null && !"".equals(requiredName) && - (requiredName == null || !valueHolder.getName().equals(requiredName))) { + if (valueHolder.getName() != null && (requiredName == null || + (!requiredName.isEmpty() && !requiredName.equals(valueHolder.getName())))) { continue; } - if (valueHolder.getType() != null && - (requiredType == null || !ClassUtils.matchesTypeName(requiredType, valueHolder.getType()))) { + if (valueHolder.getType() != null && (requiredType == null || + !ClassUtils.matchesTypeName(requiredType, valueHolder.getType()))) { continue; } if (requiredType != null && valueHolder.getType() == null && valueHolder.getName() == null && diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/PropertiesBeanDefinitionReader.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/PropertiesBeanDefinitionReader.java index 4d24c57e862..362f7ac2df4 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/PropertiesBeanDefinitionReader.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/PropertiesBeanDefinitionReader.java @@ -446,8 +446,8 @@ public class PropertiesBeanDefinitionReader extends AbstractBeanDefinitionReader else if (SINGLETON_KEY.equals(property)) { // Spring 1.2 style String val = StringUtils.trimWhitespace((String) entry.getValue()); - scope = ("".equals(val) || TRUE_VALUE.equals(val) ? BeanDefinition.SCOPE_SINGLETON : - BeanDefinition.SCOPE_PROTOTYPE); + scope = (!StringUtils.hasLength(val) || TRUE_VALUE.equals(val) ? + BeanDefinition.SCOPE_SINGLETON : BeanDefinition.SCOPE_PROTOTYPE); } else if (LAZY_INIT_KEY.equals(property)) { String val = StringUtils.trimWhitespace((String) entry.getValue()); diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/xml/BeanDefinitionParserDelegate.java b/spring-beans/src/main/java/org/springframework/beans/factory/xml/BeanDefinitionParserDelegate.java index 90d728303f8..6be311eb1ef 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/xml/BeanDefinitionParserDelegate.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/xml/BeanDefinitionParserDelegate.java @@ -1523,7 +1523,7 @@ public class BeanDefinitionParserDelegate { * Determine whether the given URI indicates the default namespace. */ public boolean isDefaultNamespace(@Nullable String namespaceUri) { - return (!StringUtils.hasLength(namespaceUri) || BEANS_NAMESPACE_URI.equals(namespaceUri)); + return !StringUtils.hasLength(namespaceUri) || BEANS_NAMESPACE_URI.equals(namespaceUri); } /** @@ -1534,7 +1534,7 @@ public class BeanDefinitionParserDelegate { } private boolean isDefaultValue(String value) { - return (DEFAULT_VALUE.equals(value) || "".equals(value)); + return !StringUtils.hasLength(value) || DEFAULT_VALUE.equals(value); } private boolean isCandidateElement(Node node) { diff --git a/spring-context/src/main/java/org/springframework/validation/AbstractBindingResult.java b/spring-context/src/main/java/org/springframework/validation/AbstractBindingResult.java index 0c47a413a94..eb8b3104685 100644 --- a/spring-context/src/main/java/org/springframework/validation/AbstractBindingResult.java +++ b/spring-context/src/main/java/org/springframework/validation/AbstractBindingResult.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2020 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. @@ -105,7 +105,7 @@ public abstract class AbstractBindingResult extends AbstractErrors implements Bi public void rejectValue(@Nullable String field, String errorCode, @Nullable Object[] errorArgs, @Nullable String defaultMessage) { - if ("".equals(getNestedPath()) && !StringUtils.hasLength(field)) { + if (!StringUtils.hasLength(getNestedPath()) && !StringUtils.hasLength(field)) { // We're at the top of the nested object hierarchy, // so the present level is not a field but rather the top object. // The best we can do is register a global error here... diff --git a/spring-context/src/main/java/org/springframework/validation/beanvalidation/SpringValidatorAdapter.java b/spring-context/src/main/java/org/springframework/validation/beanvalidation/SpringValidatorAdapter.java index fc5b0e22e44..5439bfa5588 100644 --- a/spring-context/src/main/java/org/springframework/validation/beanvalidation/SpringValidatorAdapter.java +++ b/spring-context/src/main/java/org/springframework/validation/beanvalidation/SpringValidatorAdapter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 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. @@ -311,7 +311,7 @@ public class SpringValidatorAdapter implements SmartValidator, javax.validation. @Nullable protected Object getRejectedValue(String field, ConstraintViolation violation, BindingResult bindingResult) { Object invalidValue = violation.getInvalidValue(); - if (!"".equals(field) && !field.contains("[]") && + if (!field.isEmpty() && !field.contains("[]") && (invalidValue == violation.getLeafBean() || field.contains("[") || field.contains("."))) { // Possibly a bean constraint with property path: retrieve the actual property value. // However, explicitly avoid this for "address[]" style paths that we can't handle. 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 9feda4082f5..39d79fb2af5 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 @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 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. @@ -340,7 +340,7 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol URL url = resourceUrls.nextElement(); result.add(convertClassLoaderURL(url)); } - if ("".equals(path)) { + if (!StringUtils.hasLength(path)) { // The above result is likely to be incomplete, i.e. only containing file system references. // We need to have pointers to each of the jar files on the classpath as well... addAllClassLoaderJarRoots(cl, result); @@ -639,7 +639,7 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol if (logger.isTraceEnabled()) { logger.trace("Looking for matching resources in jar file [" + jarFileUrl + "]"); } - if (!"".equals(rootEntryPath) && !rootEntryPath.endsWith("/")) { + if (StringUtils.hasLength(rootEntryPath) && !rootEntryPath.endsWith("/")) { // Root entry path must end with slash to allow for proper matching. // The Sun JRE does not return a slash here, but BEA JRockit does. rootEntryPath = rootEntryPath + "/"; diff --git a/spring-core/src/main/java/org/springframework/util/StringUtils.java b/spring-core/src/main/java/org/springframework/util/StringUtils.java index 5eb623f94a5..f627c8bf836 100644 --- a/spring-core/src/main/java/org/springframework/util/StringUtils.java +++ b/spring-core/src/main/java/org/springframework/util/StringUtils.java @@ -331,6 +331,16 @@ public abstract class StringUtils { return sb.toString(); } + /** + * Test if the given {@code String} matches the given single character. + * @param str the {@code String} to check + * @param singleCharacter the character to compare to + * @since 5.2.9 + */ + public static boolean matchesCharacter(@Nullable String str, char singleCharacter) { + return (str != null && str.length() == 1 && str.charAt(0) == singleCharacter); + } + /** * Test if the given {@code String} starts with the specified prefix, * ignoring upper/lower case. @@ -713,7 +723,7 @@ public abstract class StringUtils { pathElements.add(0, TOP_PATH); } // If nothing else left, at least explicitly point to current path. - if (pathElements.size() == 1 && "".equals(pathElements.getLast()) && !prefix.endsWith(FOLDER_SEPARATOR)) { + if (pathElements.size() == 1 && pathElements.getLast().isEmpty() && !prefix.endsWith(FOLDER_SEPARATOR)) { pathElements.add(0, CURRENT_PATH); } diff --git a/spring-jms/src/main/java/org/springframework/jms/config/AbstractListenerContainerParser.java b/spring-jms/src/main/java/org/springframework/jms/config/AbstractListenerContainerParser.java index 86d4fec4f6c..a8e66fd602c 100644 --- a/spring-jms/src/main/java/org/springframework/jms/config/AbstractListenerContainerParser.java +++ b/spring-jms/src/main/java/org/springframework/jms/config/AbstractListenerContainerParser.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2020 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. @@ -255,7 +255,7 @@ abstract class AbstractListenerContainerParser implements BeanDefinitionParser { else if (DESTINATION_TYPE_TOPIC.equals(destinationType)) { pubSubDomain = true; } - else if ("".equals(destinationType) || DESTINATION_TYPE_QUEUE.equals(destinationType)) { + else if (!StringUtils.hasLength(destinationType) || DESTINATION_TYPE_QUEUE.equals(destinationType)) { // the default: queue } else { diff --git a/spring-jms/src/main/java/org/springframework/jms/config/JmsListenerContainerParser.java b/spring-jms/src/main/java/org/springframework/jms/config/JmsListenerContainerParser.java index d00bb4b64db..0738ecfae1c 100644 --- a/spring-jms/src/main/java/org/springframework/jms/config/JmsListenerContainerParser.java +++ b/spring-jms/src/main/java/org/springframework/jms/config/JmsListenerContainerParser.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2020 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. @@ -64,10 +64,10 @@ class JmsListenerContainerParser extends AbstractListenerContainerParser { String containerType = containerEle.getAttribute(CONTAINER_TYPE_ATTRIBUTE); String containerClass = containerEle.getAttribute(CONTAINER_CLASS_ATTRIBUTE); - if (!"".equals(containerClass)) { - return null; // Not supported + if (StringUtils.hasLength(containerClass)) { + return null; // not supported } - else if ("".equals(containerType) || containerType.startsWith("default")) { + else if (!StringUtils.hasLength(containerType) || containerType.startsWith("default")) { factoryDef.setBeanClassName("org.springframework.jms.config.DefaultJmsListenerContainerFactory"); } else if (containerType.startsWith("simple")) { @@ -91,10 +91,10 @@ class JmsListenerContainerParser extends AbstractListenerContainerParser { String containerType = containerEle.getAttribute(CONTAINER_TYPE_ATTRIBUTE); String containerClass = containerEle.getAttribute(CONTAINER_CLASS_ATTRIBUTE); - if (!"".equals(containerClass)) { + if (StringUtils.hasLength(containerClass)) { containerDef.setBeanClassName(containerClass); } - else if ("".equals(containerType) || containerType.startsWith("default")) { + else if (!StringUtils.hasLength(containerType) || containerType.startsWith("default")) { containerDef.setBeanClassName("org.springframework.jms.listener.DefaultMessageListenerContainer"); } else if (containerType.startsWith("simple")) { diff --git a/spring-test/src/main/java/org/springframework/test/context/jdbc/MergedSqlConfig.java b/spring-test/src/main/java/org/springframework/test/context/jdbc/MergedSqlConfig.java index 38ea9292132..72a1cab574e 100644 --- a/spring-test/src/main/java/org/springframework/test/context/jdbc/MergedSqlConfig.java +++ b/spring-test/src/main/java/org/springframework/test/context/jdbc/MergedSqlConfig.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 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. @@ -235,7 +235,7 @@ class MergedSqlConfig { private static String getString(AnnotationAttributes attributes, String attributeName, String defaultValue) { String value = attributes.getString(attributeName); - if ("".equals(value)) { + if (value.isEmpty()) { value = defaultValue; } return value; diff --git a/spring-web/src/main/java/org/springframework/http/server/DefaultRequestPath.java b/spring-web/src/main/java/org/springframework/http/server/DefaultRequestPath.java index ff5355a76c5..321af8a77bf 100644 --- a/spring-web/src/main/java/org/springframework/http/server/DefaultRequestPath.java +++ b/spring-web/src/main/java/org/springframework/http/server/DefaultRequestPath.java @@ -50,7 +50,7 @@ class DefaultRequestPath implements RequestPath { } private static PathContainer initContextPath(PathContainer path, @Nullable String contextPath) { - if (!StringUtils.hasText(contextPath) || "/".equals(contextPath)) { + if (!StringUtils.hasText(contextPath) || StringUtils.matchesCharacter(contextPath, '/')) { return PathContainer.parsePath(""); } @@ -59,7 +59,7 @@ class DefaultRequestPath implements RequestPath { int length = contextPath.length(); int counter = 0; - for (int i=0; i < path.elements().size(); i++) { + for (int i = 0; i < path.elements().size(); i++) { PathContainer.Element element = path.elements().get(i); counter += element.value().length(); if (length == counter) { diff --git a/spring-web/src/main/java/org/springframework/web/util/UrlPathHelper.java b/spring-web/src/main/java/org/springframework/web/util/UrlPathHelper.java index 6b0e38bba07..bad4e81cd63 100644 --- a/spring-web/src/main/java/org/springframework/web/util/UrlPathHelper.java +++ b/spring-web/src/main/java/org/springframework/web/util/UrlPathHelper.java @@ -170,7 +170,7 @@ public class UrlPathHelper { } // Else, use path within current servlet mapping if applicable String rest = getPathWithinServletMapping(request); - if (!"".equals(rest)) { + if (StringUtils.hasLength(rest)) { return rest; } else { @@ -362,7 +362,7 @@ public class UrlPathHelper { if (contextPath == null) { contextPath = request.getContextPath(); } - if ("/".equals(contextPath)) { + if (StringUtils.matchesCharacter(contextPath, '/')) { // Invalid case, but happens for includes on Jetty: silently adapt it. contextPath = ""; } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/AbstractUrlHandlerMapping.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/AbstractUrlHandlerMapping.java index 861363c76dd..0e12e0f7482 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/AbstractUrlHandlerMapping.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/AbstractUrlHandlerMapping.java @@ -31,6 +31,7 @@ import org.springframework.context.ApplicationContext; import org.springframework.lang.Nullable; import org.springframework.util.Assert; import org.springframework.util.CollectionUtils; +import org.springframework.util.StringUtils; import org.springframework.web.servlet.HandlerExecutionChain; /** @@ -127,7 +128,7 @@ public abstract class AbstractUrlHandlerMapping extends AbstractHandlerMapping i // We need to care for the default handler directly, since we need to // expose the PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE for it as well. Object rawHandler = null; - if ("/".equals(lookupPath)) { + if (StringUtils.matchesCharacter(lookupPath, '/')) { rawHandler = getRootHandler(); } if (rawHandler == null) { diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceUrlEncodingFilter.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceUrlEncodingFilter.java index b91892cdebf..27b483505a8 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceUrlEncodingFilter.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceUrlEncodingFilter.java @@ -31,6 +31,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.lang.Nullable; +import org.springframework.util.StringUtils; import org.springframework.web.filter.GenericFilterBean; import org.springframework.web.util.UrlPathHelper; @@ -100,7 +101,7 @@ public class ResourceUrlEncodingFilter extends GenericFilterBean { throw new LookupPathIndexException(lookupPath, requestUri); } this.prefixLookupPath = requestUri.substring(0, this.indexLookupPath); - if ("/".equals(lookupPath) && !"/".equals(requestUri)) { + if (StringUtils.matchesCharacter(lookupPath, '/') && !StringUtils.matchesCharacter(requestUri, '/')) { String contextPath = pathHelper.getContextPath(this); if (requestUri.equals(contextPath)) { this.indexLookupPath = requestUri.length(); diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/tags/form/ErrorsTag.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/tags/form/ErrorsTag.java index 9a7876ffcb3..106f359f987 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/tags/form/ErrorsTag.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/tags/form/ErrorsTag.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2020 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. @@ -260,7 +260,7 @@ public class ErrorsTag extends AbstractHtmlElementBodyTag implements BodyTag { @Override protected String autogenerateId() throws JspException { String path = getPropertyPath(); - if ("".equals(path) || "*".equals(path)) { + if (!StringUtils.hasLength(path) || "*".equals(path)) { path = (String) this.pageContext.getAttribute( FormTag.MODEL_ATTRIBUTE_VARIABLE_NAME, PageContext.REQUEST_SCOPE); }