Efficient checks for empty strings and single character matches

Closes gh-25552
Closes gh-25553
This commit is contained in:
Juergen Hoeller 2020-08-25 16:17:12 +02:00
parent 0d4040aa63
commit 04df9b8f49
15 changed files with 54 additions and 40 deletions

View File

@ -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<ValueHolder> usedValueHolders) {
public ValueHolder getGenericArgumentValue(@Nullable Class<?> requiredType, @Nullable String requiredName,
@Nullable Set<ValueHolder> 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 &&

View File

@ -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());

View File

@ -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) {

View File

@ -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...

View File

@ -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<Object> 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.

View File

@ -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 + "/";

View File

@ -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);
}

View File

@ -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 {

View File

@ -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")) {

View File

@ -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;

View File

@ -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) {

View File

@ -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 = "";
}

View File

@ -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) {

View File

@ -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();

View File

@ -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);
}