Skip to content

Commit a7949ac

Browse files
committed
Consistent use of StringUtils.hasLength(String) vs isEmpty(Object)
1 parent 84266d7 commit a7949ac

File tree

18 files changed

+49
-42
lines changed

18 files changed

+49
-42
lines changed

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,20 @@ public abstract class StringUtils {
7575
//---------------------------------------------------------------------
7676

7777
/**
78-
* Check whether the given {@code String} is empty.
78+
* Check whether the given object (possibly a {@code String}) is empty.
79+
* This is effectly a shortcut for {@code !hasLength(String)}.
7980
* <p>This method accepts any Object as an argument, comparing it to
8081
* {@code null} and the empty String. As a consequence, this method
8182
* will never return {@code true} for a non-null non-String object.
8283
* <p>The Object signature is useful for general attribute handling code
8384
* that commonly deals with Strings but generally has to iterate over
8485
* Objects since attributes may e.g. be primitive value objects as well.
85-
* @param str the candidate String
86+
* <p><b>Note: If the object is typed to {@code String} upfront, prefer
87+
* {@link #hasLength(String)} or {@link #hasText(String)} instead.</b>
88+
* @param str the candidate object (possibly a {@code String})
8689
* @since 3.2.1
90+
* @see #hasLength(String)
91+
* @see #hasText(String)
8792
*/
8893
public static boolean isEmpty(@Nullable Object str) {
8994
return (str == null || "".equals(str));
@@ -102,7 +107,8 @@ public static boolean isEmpty(@Nullable Object str) {
102107
* </pre>
103108
* @param str the {@code CharSequence} to check (may be {@code null})
104109
* @return {@code true} if the {@code CharSequence} is not {@code null} and has length
105-
* @see #hasText(String)
110+
* @see #hasLength(String)
111+
* @see #hasText(CharSequence)
106112
*/
107113
public static boolean hasLength(@Nullable CharSequence str) {
108114
return (str != null && str.length() > 0);
@@ -136,6 +142,8 @@ public static boolean hasLength(@Nullable String str) {
136142
* @param str the {@code CharSequence} to check (may be {@code null})
137143
* @return {@code true} if the {@code CharSequence} is not {@code null},
138144
* its length is greater than 0, and it does not contain whitespace only
145+
* @see #hasText(String)
146+
* @see #hasLength(CharSequence)
139147
* @see Character#isWhitespace
140148
*/
141149
public static boolean hasText(@Nullable CharSequence str) {
@@ -151,6 +159,8 @@ public static boolean hasText(@Nullable CharSequence str) {
151159
* @return {@code true} if the {@code String} is not {@code null}, its
152160
* length is greater than 0, and it does not contain whitespace only
153161
* @see #hasText(CharSequence)
162+
* @see #hasLength(String)
163+
* @see Character#isWhitespace
154164
*/
155165
public static boolean hasText(@Nullable String str) {
156166
return (str != null && !str.isEmpty() && containsText(str));

spring-jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ public int[] doInStatement(Statement stmt) throws SQLException, DataAccessExcept
576576
}
577577

578578
private String appendSql(@Nullable String sql, String statement) {
579-
return (StringUtils.isEmpty(sql) ? statement : sql + "; " + statement);
579+
return (StringUtils.hasLength(sql) ? sql + "; " + statement : statement);
580580
}
581581

582582
@Override

spring-web/src/main/java/org/springframework/http/HttpHeaders.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ public void setAllow(Set<HttpMethod> allowedMethods) {
699699
*/
700700
public Set<HttpMethod> getAllow() {
701701
String value = getFirst(ALLOW);
702-
if (!StringUtils.isEmpty(value)) {
702+
if (StringUtils.hasLength(value)) {
703703
String[] tokens = StringUtils.tokenizeToStringArray(value, ",");
704704
List<HttpMethod> result = new ArrayList<>(tokens.length);
705705
for (String token : tokens) {

spring-web/src/main/java/org/springframework/http/server/reactive/UndertowServerHttpRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ private static URI initUri(HttpServerExchange exchange) throws URISyntaxExceptio
7474
Assert.notNull(exchange, "HttpServerExchange is required.");
7575
String requestURL = exchange.getRequestURL();
7676
String query = exchange.getQueryString();
77-
String requestUriAndQuery = StringUtils.isEmpty(query) ? requestURL : requestURL + "?" + query;
77+
String requestUriAndQuery = (StringUtils.hasLength(query) ? requestURL + "?" + query : requestURL);
7878
return new URI(requestUriAndQuery);
7979
}
8080

spring-web/src/main/java/org/springframework/web/method/annotation/RequestParamMethodArgumentResolver.java

Lines changed: 3 additions & 3 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-2019 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.
@@ -213,8 +213,8 @@ public void contributeMethodArgument(MethodParameter parameter, @Nullable Object
213213
}
214214

215215
RequestParam requestParam = parameter.getParameterAnnotation(RequestParam.class);
216-
String name = (requestParam == null || StringUtils.isEmpty(requestParam.name()) ?
217-
parameter.getParameterName() : requestParam.name());
216+
String name = (requestParam != null && StringUtils.hasLength(requestParam.name()) ?
217+
requestParam.name() : parameter.getParameterName());
218218
Assert.state(name != null, "Unresolvable parameter name");
219219

220220
if (value == null) {

spring-web/src/main/java/org/springframework/web/util/DefaultUriBuilderFactory.java

Lines changed: 4 additions & 9 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-2019 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.
@@ -33,7 +33,6 @@
3333
* <p>Provides options to create {@link UriBuilder} instances with a common
3434
* base URI, alternative encoding mode strategies, among others.
3535
*
36-
*
3736
* @author Rossen Stoyanchev
3837
* @since 5.0
3938
* @see UriComponentsBuilder
@@ -222,31 +221,27 @@ private class DefaultUriBuilder implements UriBuilder {
222221

223222
private final UriComponentsBuilder uriComponentsBuilder;
224223

225-
226224
public DefaultUriBuilder(String uriTemplate) {
227225
this.uriComponentsBuilder = initUriComponentsBuilder(uriTemplate);
228226
}
229227

230228
private UriComponentsBuilder initUriComponentsBuilder(String uriTemplate) {
231229
UriComponentsBuilder result;
232-
if (StringUtils.isEmpty(uriTemplate)) {
233-
result = baseUri != null ? baseUri.cloneBuilder() : UriComponentsBuilder.newInstance();
230+
if (!StringUtils.hasLength(uriTemplate)) {
231+
result = (baseUri != null ? baseUri.cloneBuilder() : UriComponentsBuilder.newInstance());
234232
}
235233
else if (baseUri != null) {
236234
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(uriTemplate);
237235
UriComponents uri = builder.build();
238-
result = uri.getHost() == null ? baseUri.cloneBuilder().uriComponents(uri) : builder;
236+
result = (uri.getHost() == null ? baseUri.cloneBuilder().uriComponents(uri) : builder);
239237
}
240238
else {
241239
result = UriComponentsBuilder.fromUriString(uriTemplate);
242240
}
243-
244241
if (encodingMode.equals(EncodingMode.TEMPLATE_AND_VALUES)) {
245242
result.encode();
246243
}
247-
248244
parsePathIfNecessary(result);
249-
250245
return result;
251246
}
252247

spring-webflux/src/main/java/org/springframework/web/reactive/resource/VersionResourceResolver.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-2019 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.
@@ -172,7 +172,7 @@ private Mono<Resource> resolveVersionedResource(@Nullable ServerWebExchange exch
172172
}
173173

174174
String candidate = versionStrategy.extractVersion(requestPath);
175-
if (StringUtils.isEmpty(candidate)) {
175+
if (!StringUtils.hasLength(candidate)) {
176176
return Mono.empty();
177177
}
178178

spring-webflux/src/main/java/org/springframework/web/reactive/result/view/RedirectView.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ protected boolean isRemoteHost(String targetUrl) {
309309
return false;
310310
}
311311
String targetHost = UriComponentsBuilder.fromUriString(targetUrl).build().getHost();
312-
if (StringUtils.isEmpty(targetHost)) {
312+
if (!StringUtils.hasLength(targetHost)) {
313313
return false;
314314
}
315315
for (String host : this.hosts) {

spring-webmvc/src/main/java/org/springframework/web/servlet/DispatcherServlet.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -963,11 +963,12 @@ private void logRequest(HttpServletRequest request) {
963963
params = (request.getParameterMap().isEmpty() ? "" : "masked");
964964
}
965965

966-
String query = StringUtils.isEmpty(request.getQueryString()) ? "" : "?" + request.getQueryString();
966+
String queryString = request.getQueryString();
967+
String queryClause = (StringUtils.hasLength(queryString) ? "?" + queryString : "");
967968
String dispatchType = (!request.getDispatcherType().equals(DispatcherType.REQUEST) ?
968969
"\"" + request.getDispatcherType().name() + "\" dispatch for " : "");
969970
String message = (dispatchType + request.getMethod() + " \"" + getRequestUri(request) +
970-
query + "\", parameters={" + params + "}");
971+
queryClause + "\", parameters={" + params + "}");
971972

972973
if (traceOn) {
973974
List<String> values = Collections.list(request.getHeaderNames());

spring-webmvc/src/main/java/org/springframework/web/servlet/handler/AbstractHandlerExceptionResolver.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,16 +100,17 @@ public void setMappedHandlerClasses(Class<?>... mappedHandlerClasses) {
100100
/**
101101
* Set the log category for warn logging. The name will be passed to the underlying logger
102102
* implementation through Commons Logging, getting interpreted as a log category according
103-
* to the logger's configuration. If {@code null} is passed, warn logging is turned off.
104-
* <p>By default there is no warn logging although sub-classes like
103+
* to the logger's configuration. If {@code null} or empty String is passed, warn logging
104+
* is turned off.
105+
* <p>By default there is no warn logging although subclasses like
105106
* {@link org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver}
106107
* can change that default. Specify this setting to activate warn logging into a specific
107108
* category. Alternatively, override the {@link #logException} method for custom logging.
108109
* @see org.apache.commons.logging.LogFactory#getLog(String)
109110
* @see java.util.logging.Logger#getLogger(String)
110111
*/
111112
public void setWarnLogCategory(String loggerName) {
112-
this.warnLogger = (!StringUtils.isEmpty(loggerName) ? LogFactory.getLog(loggerName) : null);
113+
this.warnLogger = (StringUtils.hasLength(loggerName) ? LogFactory.getLog(loggerName) : null);
113114
}
114115

115116
/**

0 commit comments

Comments
 (0)