Deprecate StringUtils::trimWhitespace and variants

This commits deprecates
- StringUtils::trimWhitespace in favor of String::strip
- StringUtils::trimLeadingWhitespace in favor of String::stripLeading
- StringUtils::trimTrailingWhitespace in favor of String::stripTrailing

Closes gh-27769
This commit is contained in:
Arjen Poutsma 2021-12-06 13:33:30 +01:00
parent 982ba0e86d
commit 81af7330f6
10 changed files with 28 additions and 21 deletions

View File

@ -261,7 +261,7 @@ public abstract class AbstractAspectJAdvice implements Advice, AspectJPrecedence
public void setArgumentNamesFromStringArray(String... args) {
this.argumentNames = new String[args.length];
for (int i = 0; i < args.length; i++) {
this.argumentNames[i] = StringUtils.trimWhitespace(args[i]);
this.argumentNames[i] = args[i].strip();
if (!isVariableName(this.argumentNames[i])) {
throw new IllegalArgumentException(
"'argumentNames' property of AbstractAspectJAdvice contains an argument name '" +

View File

@ -486,7 +486,7 @@ public class AspectJAdviceParameterNameDiscoverer implements ParameterNameDiscov
}
String[] tokens = StringUtils.tokenizeToStringArray(argsSpec, ",");
for (int i = 0; i < tokens.length; i++) {
tokens[i] = StringUtils.trimWhitespace(tokens[i]);
tokens[i] = tokens[i].strip();
String varName = maybeExtractVariableName(tokens[i]);
if (varName != null) {
varNames.add(varName);

View File

@ -25,7 +25,6 @@ import org.springframework.beans.factory.FactoryBean;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.PatternMatchUtils;
import org.springframework.util.StringUtils;
/**
* Auto proxy creator that identifies beans to proxy via a list of names.
@ -69,7 +68,7 @@ public class BeanNameAutoProxyCreator extends AbstractAutoProxyCreator {
Assert.notEmpty(beanNames, "'beanNames' must not be empty");
this.beanNames = new ArrayList<>(beanNames.length);
for (String mappedName : beanNames) {
this.beanNames.add(StringUtils.trimWhitespace(mappedName));
this.beanNames.add(mappedName.strip());
}
}

View File

@ -24,7 +24,6 @@ import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
/**
* Abstract base regular expression pointcut bean. JavaBean properties are:
@ -81,7 +80,7 @@ public abstract class AbstractRegexpMethodPointcut extends StaticMethodMatcherPo
Assert.notEmpty(patterns, "'patterns' must not be empty");
this.patterns = new String[patterns.length];
for (int i = 0; i < patterns.length; i++) {
this.patterns[i] = StringUtils.trimWhitespace(patterns[i]);
this.patterns[i] = patterns[i].strip();
}
initPatternRepresentation(this.patterns);
}
@ -111,7 +110,7 @@ public abstract class AbstractRegexpMethodPointcut extends StaticMethodMatcherPo
Assert.notEmpty(excludedPatterns, "'excludedPatterns' must not be empty");
this.excludedPatterns = new String[excludedPatterns.length];
for (int i = 0; i < excludedPatterns.length; i++) {
this.excludedPatterns[i] = StringUtils.trimWhitespace(excludedPatterns[i]);
this.excludedPatterns[i] = excludedPatterns[i].strip();
}
initExcludedPatternRepresentation(this.excludedPatterns);
}

View File

@ -428,31 +428,31 @@ public class PropertiesBeanDefinitionReader extends AbstractBeanDefinitionReader
int beginIndex = prefixWithSep.length();
for (Map.Entry<?, ?> entry : map.entrySet()) {
String key = StringUtils.trimWhitespace((String) entry.getKey());
String key = ((String) entry.getKey()).strip();
if (key.startsWith(prefixWithSep)) {
String property = key.substring(beginIndex);
if (CLASS_KEY.equals(property)) {
className = StringUtils.trimWhitespace((String) entry.getValue());
className = ((String) entry.getValue()).strip();
}
else if (PARENT_KEY.equals(property)) {
parent = StringUtils.trimWhitespace((String) entry.getValue());
parent = ((String) entry.getValue()).strip();
}
else if (ABSTRACT_KEY.equals(property)) {
String val = StringUtils.trimWhitespace((String) entry.getValue());
String val = ((String) entry.getValue()).strip();
isAbstract = TRUE_VALUE.equals(val);
}
else if (SCOPE_KEY.equals(property)) {
// Spring 2.0 style
scope = StringUtils.trimWhitespace((String) entry.getValue());
scope = ((String) entry.getValue()).strip();
}
else if (SINGLETON_KEY.equals(property)) {
// Spring 1.2 style
String val = StringUtils.trimWhitespace((String) entry.getValue());
String val = ((String) entry.getValue()).strip();
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());
String val = ((String) entry.getValue()).strip();
lazyInit = TRUE_VALUE.equals(val);
}
else if (property.startsWith(CONSTRUCTOR_ARG_PREFIX)) {
@ -469,7 +469,7 @@ public class PropertiesBeanDefinitionReader extends AbstractBeanDefinitionReader
// This isn't a real property, but a reference to another prototype
// Extract property name: property is of form dog(ref)
property = property.substring(0, property.length() - REF_SUFFIX.length());
String ref = StringUtils.trimWhitespace((String) entry.getValue());
String ref = ((String) entry.getValue()).strip();
// It doesn't matter if the referenced bean hasn't yet been registered:
// this will ensure that the reference is resolved at runtime.

View File

@ -79,8 +79,8 @@ public class SimpleConstructorNamespaceHandler implements NamespaceHandler {
@Override
public BeanDefinitionHolder decorate(Node node, BeanDefinitionHolder definition, ParserContext parserContext) {
if (node instanceof Attr attr) {
String argName = StringUtils.trimWhitespace(parserContext.getDelegate().getLocalName(attr));
String argValue = StringUtils.trimWhitespace(attr.getValue());
String argName = parserContext.getDelegate().getLocalName(attr).strip();
String argValue = attr.getValue().strip();
ConstructorArgumentValues cvs = definition.getBeanDefinition().getConstructorArgumentValues();
boolean ref = false;

View File

@ -218,7 +218,9 @@ public abstract class StringUtils {
* @param str the {@code String} to check
* @return the trimmed {@code String}
* @see java.lang.Character#isWhitespace
* @deprecated in favor of {@link String#strip()}
*/
@Deprecated
public static String trimWhitespace(String str) {
if (!hasLength(str)) {
return str;
@ -255,7 +257,9 @@ public abstract class StringUtils {
* @param str the {@code String} to check
* @return the trimmed {@code String}
* @see java.lang.Character#isWhitespace
* @deprecated in favor of {@link String#stripLeading()}
*/
@Deprecated
public static String trimLeadingWhitespace(String str) {
if (!hasLength(str)) {
return str;
@ -269,7 +273,9 @@ public abstract class StringUtils {
* @param str the {@code String} to check
* @return the trimmed {@code String}
* @see java.lang.Character#isWhitespace
* @deprecated in favor of {@link String#stripTrailing()}
*/
@Deprecated
public static String trimTrailingWhitespace(String str) {
if (!hasLength(str)) {
return str;
@ -843,7 +849,7 @@ public abstract class StringUtils {
// code sans the separator between the country code and the variant.
int endIndexOfCountryCode = localeString.indexOf(country, language.length()) + country.length();
// Strip off any leading '_' and whitespace, what's left is the variant.
variant = trimLeadingWhitespace(localeString.substring(endIndexOfCountryCode));
variant = localeString.substring(endIndexOfCountryCode).stripLeading();
if (variant.startsWith("_")) {
variant = trimLeadingCharacter(variant, '_');
}

View File

@ -67,6 +67,7 @@ class StringUtilsTests {
}
@Test
@Deprecated
void trimWhitespace() {
assertThat(StringUtils.trimWhitespace(null)).isEqualTo(null);
assertThat(StringUtils.trimWhitespace("")).isEqualTo("");
@ -97,6 +98,7 @@ class StringUtilsTests {
}
@Test
@Deprecated
void trimLeadingWhitespace() {
assertThat(StringUtils.trimLeadingWhitespace(null)).isEqualTo(null);
assertThat(StringUtils.trimLeadingWhitespace("")).isEqualTo("");
@ -112,6 +114,7 @@ class StringUtilsTests {
}
@Test
@Deprecated
void trimTrailingWhitespace() {
assertThat(StringUtils.trimTrailingWhitespace(null)).isEqualTo(null);
assertThat(StringUtils.trimTrailingWhitespace("")).isEqualTo("");

View File

@ -145,14 +145,14 @@ class TxAdviceBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
private void addRollbackRuleAttributesTo(List<RollbackRuleAttribute> rollbackRules, String rollbackForValue) {
String[] exceptionTypeNames = StringUtils.commaDelimitedListToStringArray(rollbackForValue);
for (String typeName : exceptionTypeNames) {
rollbackRules.add(new RollbackRuleAttribute(StringUtils.trimWhitespace(typeName)));
rollbackRules.add(new RollbackRuleAttribute(typeName.strip()));
}
}
private void addNoRollbackRuleAttributesTo(List<RollbackRuleAttribute> rollbackRules, String noRollbackForValue) {
String[] exceptionTypeNames = StringUtils.commaDelimitedListToStringArray(noRollbackForValue);
for (String typeName : exceptionTypeNames) {
rollbackRules.add(new NoRollbackRuleAttribute(StringUtils.trimWhitespace(typeName)));
rollbackRules.add(new NoRollbackRuleAttribute(typeName.strip()));
}
}

View File

@ -54,7 +54,7 @@ public class TransactionAttributeEditor extends PropertyEditorSupport {
RuleBasedTransactionAttribute attr = new RuleBasedTransactionAttribute();
for (String token : tokens) {
// Trim leading and trailing whitespace.
String trimmedToken = StringUtils.trimWhitespace(token.trim());
String trimmedToken = token.strip();
// Check whether token contains illegal whitespace within text.
if (StringUtils.containsWhitespace(trimmedToken)) {
throw new IllegalArgumentException(