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 11e33cbbce..7a9233ea48 100644 --- a/spring-core/src/main/java/org/springframework/util/StringUtils.java +++ b/spring-core/src/main/java/org/springframework/util/StringUtils.java @@ -76,15 +76,20 @@ public abstract class StringUtils { //--------------------------------------------------------------------- /** - * Check whether the given {@code String} is empty. + * Check whether the given object (possibly a {@code String}) is empty. + * This is effectly a shortcut for {@code !hasLength(String)}. *
This method accepts any Object as an argument, comparing it to * {@code null} and the empty String. As a consequence, this method * will never return {@code true} for a non-null non-String object. *
The Object signature is useful for general attribute handling code * that commonly deals with Strings but generally has to iterate over * Objects since attributes may e.g. be primitive value objects as well. - * @param str the candidate String + *
Note: If the object is typed to {@code String} upfront, prefer
+ * {@link #hasLength(String)} or {@link #hasText(String)} instead.
+ * @param str the candidate object (possibly a {@code String})
* @since 3.2.1
+ * @see #hasLength(String)
+ * @see #hasText(String)
*/
public static boolean isEmpty(@Nullable Object str) {
return (str == null || "".equals(str));
@@ -103,7 +108,8 @@ public abstract class StringUtils {
*
* @param str the {@code CharSequence} to check (may be {@code null})
* @return {@code true} if the {@code CharSequence} is not {@code null} and has length
- * @see #hasText(String)
+ * @see #hasLength(String)
+ * @see #hasText(CharSequence)
*/
public static boolean hasLength(@Nullable CharSequence str) {
return (str != null && str.length() > 0);
@@ -137,6 +143,8 @@ public abstract class StringUtils {
* @param str the {@code CharSequence} to check (may be {@code null})
* @return {@code true} if the {@code CharSequence} is not {@code null},
* its length is greater than 0, and it does not contain whitespace only
+ * @see #hasText(String)
+ * @see #hasLength(CharSequence)
* @see Character#isWhitespace
*/
public static boolean hasText(@Nullable CharSequence str) {
@@ -152,6 +160,8 @@ public abstract class StringUtils {
* @return {@code true} if the {@code String} is not {@code null}, its
* length is greater than 0, and it does not contain whitespace only
* @see #hasText(CharSequence)
+ * @see #hasLength(String)
+ * @see Character#isWhitespace
*/
public static boolean hasText(@Nullable String str) {
return (str != null && !str.isEmpty() && containsText(str));
diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java b/spring-jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java
index 032e29f68c..de61f26b82 100644
--- a/spring-jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java
+++ b/spring-jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java
@@ -576,7 +576,7 @@ public class JdbcTemplate extends JdbcAccessor implements JdbcOperations {
}
private String appendSql(@Nullable String sql, String statement) {
- return (StringUtils.isEmpty(sql) ? statement : sql + "; " + statement);
+ return (StringUtils.hasLength(sql) ? sql + "; " + statement : statement);
}
@Override
diff --git a/spring-tx/src/main/java/org/springframework/transaction/interceptor/TransactionAspectSupport.java b/spring-tx/src/main/java/org/springframework/transaction/interceptor/TransactionAspectSupport.java
index 7c3a4797cb..00fcc72931 100644
--- a/spring-tx/src/main/java/org/springframework/transaction/interceptor/TransactionAspectSupport.java
+++ b/spring-tx/src/main/java/org/springframework/transaction/interceptor/TransactionAspectSupport.java
@@ -581,8 +581,8 @@ public abstract class TransactionAspectSupport implements BeanFactoryAware, Init
// The TransactionInfo.hasTransaction() method will return false. We created it only
// to preserve the integrity of the ThreadLocal stack maintained in this class.
if (logger.isTraceEnabled()) {
- logger.trace("Don't need to create transaction for [" + joinpointIdentification +
- "]: This method isn't transactional.");
+ logger.trace("No need to create transaction for [" + joinpointIdentification +
+ "]: This method is not transactional.");
}
}
diff --git a/spring-web/src/main/java/org/springframework/http/HttpHeaders.java b/spring-web/src/main/java/org/springframework/http/HttpHeaders.java
index 594be96c10..68243145fb 100644
--- a/spring-web/src/main/java/org/springframework/http/HttpHeaders.java
+++ b/spring-web/src/main/java/org/springframework/http/HttpHeaders.java
@@ -704,7 +704,7 @@ public class HttpHeaders implements MultiValueMap Provides options to create {@link UriBuilder} instances with a common
* base URI, alternative encoding mode strategies, among others.
*
- *
* @author Rossen Stoyanchev
* @since 5.0
* @see UriComponentsBuilder
@@ -222,31 +221,27 @@ public class DefaultUriBuilderFactory implements UriBuilderFactory {
private final UriComponentsBuilder uriComponentsBuilder;
-
public DefaultUriBuilder(String uriTemplate) {
this.uriComponentsBuilder = initUriComponentsBuilder(uriTemplate);
}
private UriComponentsBuilder initUriComponentsBuilder(String uriTemplate) {
UriComponentsBuilder result;
- if (StringUtils.isEmpty(uriTemplate)) {
- result = baseUri != null ? baseUri.cloneBuilder() : UriComponentsBuilder.newInstance();
+ if (!StringUtils.hasLength(uriTemplate)) {
+ result = (baseUri != null ? baseUri.cloneBuilder() : UriComponentsBuilder.newInstance());
}
else if (baseUri != null) {
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(uriTemplate);
UriComponents uri = builder.build();
- result = uri.getHost() == null ? baseUri.cloneBuilder().uriComponents(uri) : builder;
+ result = (uri.getHost() == null ? baseUri.cloneBuilder().uriComponents(uri) : builder);
}
else {
result = UriComponentsBuilder.fromUriString(uriTemplate);
}
-
if (encodingMode.equals(EncodingMode.TEMPLATE_AND_VALUES)) {
result.encode();
}
-
parsePathIfNecessary(result);
-
return result;
}
diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/ExchangeStrategies.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/ExchangeStrategies.java
index baefea67b5..804fbd9a42 100644
--- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/ExchangeStrategies.java
+++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/ExchangeStrategies.java
@@ -51,7 +51,7 @@ public interface ExchangeStrategies {
// Static builder methods
/**
- * Return a new {@code ExchangeStrategies} with default configuration
+ * Return an {@code ExchangeStrategies} instance with default configuration
* provided by {@link ClientCodecConfigurer}.
*/
static ExchangeStrategies withDefaults() {
diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/resource/VersionResourceResolver.java b/spring-webflux/src/main/java/org/springframework/web/reactive/resource/VersionResourceResolver.java
index 65e6f1284f..acb0ce4f65 100644
--- a/spring-webflux/src/main/java/org/springframework/web/reactive/resource/VersionResourceResolver.java
+++ b/spring-webflux/src/main/java/org/springframework/web/reactive/resource/VersionResourceResolver.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2018 the original author or authors.
+ * Copyright 2002-2019 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.
@@ -172,7 +172,7 @@ public class VersionResourceResolver extends AbstractResourceResolver {
}
String candidate = versionStrategy.extractVersion(requestPath);
- if (StringUtils.isEmpty(candidate)) {
+ if (!StringUtils.hasLength(candidate)) {
return Mono.empty();
}
diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/RedirectView.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/RedirectView.java
index a9e7cc90eb..4b3ae4f0b5 100644
--- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/RedirectView.java
+++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/RedirectView.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2018 the original author or authors.
+ * Copyright 2002-2019 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.
@@ -44,8 +44,8 @@ import org.springframework.web.util.UriUtils;
* URI template in which case the URI template variables will be replaced with
* values from the model or with URI variables from the current request.
*
- * By default {@link HttpStatus#SEE_OTHER} is used but alternate status
- * codes may be via constructor or setters arguments.
+ * By default {@link HttpStatus#SEE_OTHER} is used but alternate status codes
+ * may be via constructor or setters arguments.
*
* @author Sebastien Deleuze
* @author Rossen Stoyanchev
@@ -56,10 +56,10 @@ public class RedirectView extends AbstractUrlBasedView {
private static final Pattern URI_TEMPLATE_VARIABLE_PATTERN = Pattern.compile("\\{([^/]+?)\\}");
- private boolean contextRelative = true;
-
private HttpStatus statusCode = HttpStatus.SEE_OTHER;
+ private boolean contextRelative = true;
+
private boolean propagateQuery = false;
@Nullable
@@ -91,22 +91,6 @@ public class RedirectView extends AbstractUrlBasedView {
}
- /**
- * Whether to interpret a given redirect URLs that starts with a slash ("/")
- * as relative to the current context path ({@code true}, the default) or to
- * the web server root ({@code false}).
- */
- public void setContextRelative(boolean contextRelative) {
- this.contextRelative = contextRelative;
- }
-
- /**
- * Whether to interpret URLs as relative to the current context path.
- */
- public boolean isContextRelative() {
- return this.contextRelative;
- }
-
/**
* Set an alternate redirect status code such as
* {@link HttpStatus#TEMPORARY_REDIRECT} or
@@ -124,6 +108,22 @@ public class RedirectView extends AbstractUrlBasedView {
return this.statusCode;
}
+ /**
+ * Whether to interpret a given redirect URLs that starts with a slash ("/")
+ * as relative to the current context path ({@code true}, the default) or to
+ * the web server root ({@code false}).
+ */
+ public void setContextRelative(boolean contextRelative) {
+ this.contextRelative = contextRelative;
+ }
+
+ /**
+ * Whether to interpret URLs as relative to the current context path.
+ */
+ public boolean isContextRelative() {
+ return this.contextRelative;
+ }
+
/**
* Whether to append the query string of the current URL to the redirect URL
* ({@code true}) or not ({@code false}, the default).
@@ -309,7 +309,7 @@ public class RedirectView extends AbstractUrlBasedView {
return false;
}
String targetHost = UriComponentsBuilder.fromUriString(targetUrl).build().getHost();
- if (StringUtils.isEmpty(targetHost)) {
+ if (!StringUtils.hasLength(targetHost)) {
return false;
}
for (String host : this.hosts) {
diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/DispatcherServlet.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/DispatcherServlet.java
index e4eefbc268..b39e5ff33f 100644
--- a/spring-webmvc/src/main/java/org/springframework/web/servlet/DispatcherServlet.java
+++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/DispatcherServlet.java
@@ -963,11 +963,12 @@ public class DispatcherServlet extends FrameworkServlet {
params = (request.getParameterMap().isEmpty() ? "" : "masked");
}
- String query = StringUtils.isEmpty(request.getQueryString()) ? "" : "?" + request.getQueryString();
+ String queryString = request.getQueryString();
+ String queryClause = (StringUtils.hasLength(queryString) ? "?" + queryString : "");
String dispatchType = (!request.getDispatcherType().equals(DispatcherType.REQUEST) ?
"\"" + request.getDispatcherType().name() + "\" dispatch for " : "");
String message = (dispatchType + request.getMethod() + " \"" + getRequestUri(request) +
- query + "\", parameters={" + params + "}");
+ queryClause + "\", parameters={" + params + "}");
if (traceOn) {
List By default there is no warn logging although sub-classes like
+ * to the logger's configuration. If {@code null} or empty String is passed, warn logging
+ * is turned off.
+ * By default there is no warn logging although subclasses like
* {@link org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver}
* can change that default. Specify this setting to activate warn logging into a specific
* category. Alternatively, override the {@link #logException} method for custom logging.
@@ -109,7 +110,7 @@ public abstract class AbstractHandlerExceptionResolver implements HandlerExcepti
* @see java.util.logging.Logger#getLogger(String)
*/
public void setWarnLogCategory(String loggerName) {
- this.warnLogger = (!StringUtils.isEmpty(loggerName) ? LogFactory.getLog(loggerName) : null);
+ this.warnLogger = (StringUtils.hasLength(loggerName) ? LogFactory.getLog(loggerName) : null);
}
/**
diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/MvcUriComponentsBuilder.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/MvcUriComponentsBuilder.java
index 5d54bcd3f5..e3093a24f3 100644
--- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/MvcUriComponentsBuilder.java
+++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/MvcUriComponentsBuilder.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2018 the original author or authors.
+ * Copyright 2002-2019 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.
@@ -578,7 +578,7 @@ public class MvcUriComponentsBuilder {
return "/";
}
String[] paths = mapping.path();
- if (ObjectUtils.isEmpty(paths) || StringUtils.isEmpty(paths[0])) {
+ if (ObjectUtils.isEmpty(paths) || !StringUtils.hasLength(paths[0])) {
return "/";
}
if (paths.length > 1 && logger.isTraceEnabled()) {
@@ -594,7 +594,7 @@ public class MvcUriComponentsBuilder {
throw new IllegalArgumentException("No @RequestMapping on: " + method.toGenericString());
}
String[] paths = requestMapping.path();
- if (ObjectUtils.isEmpty(paths) || StringUtils.isEmpty(paths[0])) {
+ if (ObjectUtils.isEmpty(paths) || !StringUtils.hasLength(paths[0])) {
return "/";
}
if (paths.length > 1 && logger.isTraceEnabled()) {
diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/PathVariableMethodArgumentResolver.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/PathVariableMethodArgumentResolver.java
index 50652bbe90..526eb0355a 100644
--- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/PathVariableMethodArgumentResolver.java
+++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/PathVariableMethodArgumentResolver.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2017 the original author or authors.
+ * Copyright 2002-2019 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.
@@ -125,7 +125,7 @@ public class PathVariableMethodArgumentResolver extends AbstractNamedValueMethod
}
PathVariable ann = parameter.getParameterAnnotation(PathVariable.class);
- String name = (ann != null && !StringUtils.isEmpty(ann.value()) ? ann.value() : parameter.getParameterName());
+ String name = (ann != null && StringUtils.hasLength(ann.value()) ? ann.value() : parameter.getParameterName());
String formatted = formatUriValue(conversionService, new TypeDescriptor(parameter.nestedIfOptional()), value);
uriVariables.put(name, formatted);
}
diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/VersionResourceResolver.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/VersionResourceResolver.java
index dbd38804b5..4ebda707a2 100644
--- a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/VersionResourceResolver.java
+++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/VersionResourceResolver.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2018 the original author or authors.
+ * Copyright 2002-2019 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.
@@ -168,7 +168,7 @@ public class VersionResourceResolver extends AbstractResourceResolver {
}
String candidateVersion = versionStrategy.extractVersion(requestPath);
- if (StringUtils.isEmpty(candidateVersion)) {
+ if (!StringUtils.hasLength(candidateVersion)) {
return null;
}
diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/support/ServletUriComponentsBuilder.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/support/ServletUriComponentsBuilder.java
index ba4911d4a1..673517c44a 100644
--- a/spring-webmvc/src/main/java/org/springframework/web/servlet/support/ServletUriComponentsBuilder.java
+++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/support/ServletUriComponentsBuilder.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2018 the original author or authors.
+ * Copyright 2002-2019 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.
@@ -206,7 +206,7 @@ public class ServletUriComponentsBuilder extends UriComponentsBuilder {
String extension = null;
if (this.originalPath != null) {
extension = UriUtils.extractFileExtension(this.originalPath);
- if (!StringUtils.isEmpty(extension)) {
+ if (StringUtils.hasLength(extension)) {
int end = this.originalPath.length() - (extension.length() + 1);
replacePath(this.originalPath.substring(0, end));
}
diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/view/RedirectView.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/view/RedirectView.java
index c6ee5ec016..77313e7cee 100644
--- a/spring-webmvc/src/main/java/org/springframework/web/servlet/view/RedirectView.java
+++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/view/RedirectView.java
@@ -650,7 +650,7 @@ public class RedirectView extends AbstractUrlBasedView implements SmartView {
return false;
}
String targetHost = UriComponentsBuilder.fromUriString(targetUrl).build().getHost();
- if (StringUtils.isEmpty(targetHost)) {
+ if (!StringUtils.hasLength(targetHost)) {
return false;
}
for (String host : getHosts()) {
diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/messaging/SubProtocolWebSocketHandler.java b/spring-websocket/src/main/java/org/springframework/web/socket/messaging/SubProtocolWebSocketHandler.java
index 31b9d15d92..a87d7863aa 100644
--- a/spring-websocket/src/main/java/org/springframework/web/socket/messaging/SubProtocolWebSocketHandler.java
+++ b/spring-websocket/src/main/java/org/springframework/web/socket/messaging/SubProtocolWebSocketHandler.java
@@ -429,7 +429,7 @@ public class SubProtocolWebSocketHandler
}
SubProtocolHandler handler;
- if (!StringUtils.isEmpty(protocol)) {
+ if (StringUtils.hasLength(protocol)) {
handler = this.protocolHandlerLookup.get(protocol);
if (handler == null) {
throw new IllegalStateException(
diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/AbstractHttpSendingTransportHandler.java b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/AbstractHttpSendingTransportHandler.java
index 4681f53f57..0aa17c5552 100644
--- a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/AbstractHttpSendingTransportHandler.java
+++ b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/AbstractHttpSendingTransportHandler.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2018 the original author or authors.
+ * Copyright 2002-2019 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.
@@ -118,7 +118,7 @@ public abstract class AbstractHttpSendingTransportHandler extends AbstractTransp
String query = request.getURI().getQuery();
MultiValueMap