Skip to content

Commit

Permalink
Efficient checks for empty strings and single character matches
Browse files Browse the repository at this point in the history
Closes gh-25552
Closes gh-25553
  • Loading branch information
jhoeller committed Aug 25, 2020
1 parent 0d4040a commit 04df9b8
Show file tree
Hide file tree
Showing 15 changed files with 54 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -164,10 +164,10 @@ public ValueHolder getIndexedArgumentValue(int index, @Nullable Class<?> require
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;
Expand Down Expand Up @@ -277,17 +277,19 @@ public ValueHolder getGenericArgumentValue(Class<?> requiredType, String require
* @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 &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -446,8 +446,8 @@ else if (SCOPE_KEY.equals(property)) {
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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1523,7 +1523,7 @@ public boolean nodeNameEquals(Node node, String desiredName) {
* 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);
}

/**
Expand All @@ -1534,7 +1534,7 @@ public boolean isDefaultNamespace(Node node) {
}

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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -105,7 +105,7 @@ public void reject(String errorCode, @Nullable Object[] errorArgs, @Nullable Str
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...
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -311,7 +311,7 @@ protected MessageSourceResolvable getResolvableField(String objectName, String f
@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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -340,7 +340,7 @@ protected Set<Resource> doFindAllClassPathResources(String path) throws IOExcept
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);
Expand Down Expand Up @@ -639,7 +639,7 @@ protected Set<Resource> doFindPathMatchingJarResources(Resource rootDirResource,
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 + "/";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,16 @@ public static String trimTrailingCharacter(String str, char trailingCharacter) {
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.
Expand Down Expand Up @@ -713,7 +723,7 @@ else if (TOP_PATH.equals(element)) {
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);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -255,7 +255,7 @@ else if (DESTINATION_TYPE_DURABLE_TOPIC.equals(destinationType)) {
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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -64,10 +64,10 @@ protected RootBeanDefinition createContainerFactory(String factoryId, Element co

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")) {
Expand All @@ -91,10 +91,10 @@ protected RootBeanDefinition createContainer(Element containerEle, Element liste

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")) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -235,7 +235,7 @@ private static <E extends Enum<?>> E getEnum(AnnotationAttributes attributes, St

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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private DefaultRequestPath(RequestPath requestPath, String contextPath) {
}

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("");
}

Expand All @@ -59,7 +59,7 @@ private static PathContainer initContextPath(PathContainer path, @Nullable Strin
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public String getLookupPathForRequest(HttpServletRequest request) {
}
// Else, use path within current servlet mapping if applicable
String rest = getPathWithinServletMapping(request);
if (!"".equals(rest)) {
if (StringUtils.hasLength(rest)) {
return rest;
}
else {
Expand Down Expand Up @@ -362,7 +362,7 @@ public String getContextPath(HttpServletRequest request) {
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 = "";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
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;

/**
Expand Down Expand Up @@ -127,7 +128,7 @@ protected Object getHandlerInternal(HttpServletRequest request) throws Exception
// 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
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;

Expand Down Expand Up @@ -100,7 +101,7 @@ private void initLookupPath(ResourceUrlProvider urlProvider) {
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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -260,7 +260,7 @@ public String getDelimiter() {
@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);
}
Expand Down

0 comments on commit 04df9b8

Please sign in to comment.