Skip to content

Commit 04df9b8

Browse files
committed
Efficient checks for empty strings and single character matches
Closes gh-25552 Closes gh-25553
1 parent 0d4040a commit 04df9b8

File tree

15 files changed

+54
-40
lines changed

15 files changed

+54
-40
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/config/ConstructorArgumentValues.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 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.
@@ -164,10 +164,10 @@ public ValueHolder getIndexedArgumentValue(int index, @Nullable Class<?> require
164164
Assert.isTrue(index >= 0, "Index must not be negative");
165165
ValueHolder valueHolder = this.indexedArgumentValues.get(index);
166166
if (valueHolder != null &&
167-
(valueHolder.getType() == null ||
168-
(requiredType != null && ClassUtils.matchesTypeName(requiredType, valueHolder.getType()))) &&
169-
(valueHolder.getName() == null || "".equals(requiredName) ||
170-
(requiredName != null && requiredName.equals(valueHolder.getName())))) {
167+
(valueHolder.getType() == null || (requiredType != null &&
168+
ClassUtils.matchesTypeName(requiredType, valueHolder.getType()))) &&
169+
(valueHolder.getName() == null || (requiredName != null &&
170+
(requiredName.isEmpty() || requiredName.equals(valueHolder.getName()))))) {
171171
return valueHolder;
172172
}
173173
return null;
@@ -277,17 +277,19 @@ public ValueHolder getGenericArgumentValue(Class<?> requiredType, String require
277277
* @return the ValueHolder for the argument, or {@code null} if none found
278278
*/
279279
@Nullable
280-
public ValueHolder getGenericArgumentValue(@Nullable Class<?> requiredType, @Nullable String requiredName, @Nullable Set<ValueHolder> usedValueHolders) {
280+
public ValueHolder getGenericArgumentValue(@Nullable Class<?> requiredType, @Nullable String requiredName,
281+
@Nullable Set<ValueHolder> usedValueHolders) {
282+
281283
for (ValueHolder valueHolder : this.genericArgumentValues) {
282284
if (usedValueHolders != null && usedValueHolders.contains(valueHolder)) {
283285
continue;
284286
}
285-
if (valueHolder.getName() != null && !"".equals(requiredName) &&
286-
(requiredName == null || !valueHolder.getName().equals(requiredName))) {
287+
if (valueHolder.getName() != null && (requiredName == null ||
288+
(!requiredName.isEmpty() && !requiredName.equals(valueHolder.getName())))) {
287289
continue;
288290
}
289-
if (valueHolder.getType() != null &&
290-
(requiredType == null || !ClassUtils.matchesTypeName(requiredType, valueHolder.getType()))) {
291+
if (valueHolder.getType() != null && (requiredType == null ||
292+
!ClassUtils.matchesTypeName(requiredType, valueHolder.getType()))) {
291293
continue;
292294
}
293295
if (requiredType != null && valueHolder.getType() == null && valueHolder.getName() == null &&

spring-beans/src/main/java/org/springframework/beans/factory/support/PropertiesBeanDefinitionReader.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -446,8 +446,8 @@ else if (SCOPE_KEY.equals(property)) {
446446
else if (SINGLETON_KEY.equals(property)) {
447447
// Spring 1.2 style
448448
String val = StringUtils.trimWhitespace((String) entry.getValue());
449-
scope = ("".equals(val) || TRUE_VALUE.equals(val) ? BeanDefinition.SCOPE_SINGLETON :
450-
BeanDefinition.SCOPE_PROTOTYPE);
449+
scope = (!StringUtils.hasLength(val) || TRUE_VALUE.equals(val) ?
450+
BeanDefinition.SCOPE_SINGLETON : BeanDefinition.SCOPE_PROTOTYPE);
451451
}
452452
else if (LAZY_INIT_KEY.equals(property)) {
453453
String val = StringUtils.trimWhitespace((String) entry.getValue());

spring-beans/src/main/java/org/springframework/beans/factory/xml/BeanDefinitionParserDelegate.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1523,7 +1523,7 @@ public boolean nodeNameEquals(Node node, String desiredName) {
15231523
* Determine whether the given URI indicates the default namespace.
15241524
*/
15251525
public boolean isDefaultNamespace(@Nullable String namespaceUri) {
1526-
return (!StringUtils.hasLength(namespaceUri) || BEANS_NAMESPACE_URI.equals(namespaceUri));
1526+
return !StringUtils.hasLength(namespaceUri) || BEANS_NAMESPACE_URI.equals(namespaceUri);
15271527
}
15281528

15291529
/**
@@ -1534,7 +1534,7 @@ public boolean isDefaultNamespace(Node node) {
15341534
}
15351535

15361536
private boolean isDefaultValue(String value) {
1537-
return (DEFAULT_VALUE.equals(value) || "".equals(value));
1537+
return !StringUtils.hasLength(value) || DEFAULT_VALUE.equals(value);
15381538
}
15391539

15401540
private boolean isCandidateElement(Node node) {

spring-context/src/main/java/org/springframework/validation/AbstractBindingResult.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2020 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.
@@ -105,7 +105,7 @@ public void reject(String errorCode, @Nullable Object[] errorArgs, @Nullable Str
105105
public void rejectValue(@Nullable String field, String errorCode, @Nullable Object[] errorArgs,
106106
@Nullable String defaultMessage) {
107107

108-
if ("".equals(getNestedPath()) && !StringUtils.hasLength(field)) {
108+
if (!StringUtils.hasLength(getNestedPath()) && !StringUtils.hasLength(field)) {
109109
// We're at the top of the nested object hierarchy,
110110
// so the present level is not a field but rather the top object.
111111
// The best we can do is register a global error here...

spring-context/src/main/java/org/springframework/validation/beanvalidation/SpringValidatorAdapter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 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.
@@ -311,7 +311,7 @@ protected MessageSourceResolvable getResolvableField(String objectName, String f
311311
@Nullable
312312
protected Object getRejectedValue(String field, ConstraintViolation<Object> violation, BindingResult bindingResult) {
313313
Object invalidValue = violation.getInvalidValue();
314-
if (!"".equals(field) && !field.contains("[]") &&
314+
if (!field.isEmpty() && !field.contains("[]") &&
315315
(invalidValue == violation.getLeafBean() || field.contains("[") || field.contains("."))) {
316316
// Possibly a bean constraint with property path: retrieve the actual property value.
317317
// However, explicitly avoid this for "address[]" style paths that we can't handle.

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 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.
@@ -340,7 +340,7 @@ protected Set<Resource> doFindAllClassPathResources(String path) throws IOExcept
340340
URL url = resourceUrls.nextElement();
341341
result.add(convertClassLoaderURL(url));
342342
}
343-
if ("".equals(path)) {
343+
if (!StringUtils.hasLength(path)) {
344344
// The above result is likely to be incomplete, i.e. only containing file system references.
345345
// We need to have pointers to each of the jar files on the classpath as well...
346346
addAllClassLoaderJarRoots(cl, result);
@@ -639,7 +639,7 @@ protected Set<Resource> doFindPathMatchingJarResources(Resource rootDirResource,
639639
if (logger.isTraceEnabled()) {
640640
logger.trace("Looking for matching resources in jar file [" + jarFileUrl + "]");
641641
}
642-
if (!"".equals(rootEntryPath) && !rootEntryPath.endsWith("/")) {
642+
if (StringUtils.hasLength(rootEntryPath) && !rootEntryPath.endsWith("/")) {
643643
// Root entry path must end with slash to allow for proper matching.
644644
// The Sun JRE does not return a slash here, but BEA JRockit does.
645645
rootEntryPath = rootEntryPath + "/";

spring-core/src/main/java/org/springframework/util/StringUtils.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,16 @@ public static String trimTrailingCharacter(String str, char trailingCharacter) {
331331
return sb.toString();
332332
}
333333

334+
/**
335+
* Test if the given {@code String} matches the given single character.
336+
* @param str the {@code String} to check
337+
* @param singleCharacter the character to compare to
338+
* @since 5.2.9
339+
*/
340+
public static boolean matchesCharacter(@Nullable String str, char singleCharacter) {
341+
return (str != null && str.length() == 1 && str.charAt(0) == singleCharacter);
342+
}
343+
334344
/**
335345
* Test if the given {@code String} starts with the specified prefix,
336346
* ignoring upper/lower case.
@@ -713,7 +723,7 @@ else if (TOP_PATH.equals(element)) {
713723
pathElements.add(0, TOP_PATH);
714724
}
715725
// If nothing else left, at least explicitly point to current path.
716-
if (pathElements.size() == 1 && "".equals(pathElements.getLast()) && !prefix.endsWith(FOLDER_SEPARATOR)) {
726+
if (pathElements.size() == 1 && pathElements.getLast().isEmpty() && !prefix.endsWith(FOLDER_SEPARATOR)) {
717727
pathElements.add(0, CURRENT_PATH);
718728
}
719729

spring-jms/src/main/java/org/springframework/jms/config/AbstractListenerContainerParser.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2020 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.
@@ -255,7 +255,7 @@ else if (DESTINATION_TYPE_DURABLE_TOPIC.equals(destinationType)) {
255255
else if (DESTINATION_TYPE_TOPIC.equals(destinationType)) {
256256
pubSubDomain = true;
257257
}
258-
else if ("".equals(destinationType) || DESTINATION_TYPE_QUEUE.equals(destinationType)) {
258+
else if (!StringUtils.hasLength(destinationType) || DESTINATION_TYPE_QUEUE.equals(destinationType)) {
259259
// the default: queue
260260
}
261261
else {

spring-jms/src/main/java/org/springframework/jms/config/JmsListenerContainerParser.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2020 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.
@@ -64,10 +64,10 @@ protected RootBeanDefinition createContainerFactory(String factoryId, Element co
6464

6565
String containerType = containerEle.getAttribute(CONTAINER_TYPE_ATTRIBUTE);
6666
String containerClass = containerEle.getAttribute(CONTAINER_CLASS_ATTRIBUTE);
67-
if (!"".equals(containerClass)) {
68-
return null; // Not supported
67+
if (StringUtils.hasLength(containerClass)) {
68+
return null; // not supported
6969
}
70-
else if ("".equals(containerType) || containerType.startsWith("default")) {
70+
else if (!StringUtils.hasLength(containerType) || containerType.startsWith("default")) {
7171
factoryDef.setBeanClassName("org.springframework.jms.config.DefaultJmsListenerContainerFactory");
7272
}
7373
else if (containerType.startsWith("simple")) {
@@ -91,10 +91,10 @@ protected RootBeanDefinition createContainer(Element containerEle, Element liste
9191

9292
String containerType = containerEle.getAttribute(CONTAINER_TYPE_ATTRIBUTE);
9393
String containerClass = containerEle.getAttribute(CONTAINER_CLASS_ATTRIBUTE);
94-
if (!"".equals(containerClass)) {
94+
if (StringUtils.hasLength(containerClass)) {
9595
containerDef.setBeanClassName(containerClass);
9696
}
97-
else if ("".equals(containerType) || containerType.startsWith("default")) {
97+
else if (!StringUtils.hasLength(containerType) || containerType.startsWith("default")) {
9898
containerDef.setBeanClassName("org.springframework.jms.listener.DefaultMessageListenerContainer");
9999
}
100100
else if (containerType.startsWith("simple")) {

spring-test/src/main/java/org/springframework/test/context/jdbc/MergedSqlConfig.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 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.
@@ -235,7 +235,7 @@ private static <E extends Enum<?>> E getEnum(AnnotationAttributes attributes, St
235235

236236
private static String getString(AnnotationAttributes attributes, String attributeName, String defaultValue) {
237237
String value = attributes.getString(attributeName);
238-
if ("".equals(value)) {
238+
if (value.isEmpty()) {
239239
value = defaultValue;
240240
}
241241
return value;

0 commit comments

Comments
 (0)