Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
a7949ac8
S
spring-framework
项目概览
爱吃血肠
/
spring-framework
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
spring-framework
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
a7949ac8
编写于
5月 03, 2019
作者:
J
Juergen Hoeller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Consistent use of StringUtils.hasLength(String) vs isEmpty(Object)
上级
84266d71
变更
18
隐藏空白更改
内联
并排
Showing
18 changed file
with
49 addition
and
42 deletion
+49
-42
spring-core/src/main/java/org/springframework/util/StringUtils.java
...e/src/main/java/org/springframework/util/StringUtils.java
+13
-3
spring-jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java
...main/java/org/springframework/jdbc/core/JdbcTemplate.java
+1
-1
spring-web/src/main/java/org/springframework/http/HttpHeaders.java
...b/src/main/java/org/springframework/http/HttpHeaders.java
+1
-1
spring-web/src/main/java/org/springframework/http/server/reactive/UndertowServerHttpRequest.java
...ework/http/server/reactive/UndertowServerHttpRequest.java
+1
-1
spring-web/src/main/java/org/springframework/web/method/annotation/RequestParamMethodArgumentResolver.java
...method/annotation/RequestParamMethodArgumentResolver.java
+3
-3
spring-web/src/main/java/org/springframework/web/util/DefaultUriBuilderFactory.java
...rg/springframework/web/util/DefaultUriBuilderFactory.java
+4
-9
spring-webflux/src/main/java/org/springframework/web/reactive/resource/VersionResourceResolver.java
...mework/web/reactive/resource/VersionResourceResolver.java
+2
-2
spring-webflux/src/main/java/org/springframework/web/reactive/result/view/RedirectView.java
...pringframework/web/reactive/result/view/RedirectView.java
+1
-1
spring-webmvc/src/main/java/org/springframework/web/servlet/DispatcherServlet.java
...va/org/springframework/web/servlet/DispatcherServlet.java
+3
-2
spring-webmvc/src/main/java/org/springframework/web/servlet/handler/AbstractHandlerExceptionResolver.java
...web/servlet/handler/AbstractHandlerExceptionResolver.java
+4
-3
spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/MvcUriComponentsBuilder.java
...ervlet/mvc/method/annotation/MvcUriComponentsBuilder.java
+3
-3
spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/PathVariableMethodArgumentResolver.java
...method/annotation/PathVariableMethodArgumentResolver.java
+2
-2
spring-webmvc/src/main/java/org/springframework/web/servlet/resource/VersionResourceResolver.java
...amework/web/servlet/resource/VersionResourceResolver.java
+2
-2
spring-webmvc/src/main/java/org/springframework/web/servlet/support/ServletUriComponentsBuilder.java
...work/web/servlet/support/ServletUriComponentsBuilder.java
+2
-2
spring-webmvc/src/main/java/org/springframework/web/servlet/view/RedirectView.java
...va/org/springframework/web/servlet/view/RedirectView.java
+1
-1
spring-websocket/src/main/java/org/springframework/web/socket/messaging/SubProtocolWebSocketHandler.java
...ork/web/socket/messaging/SubProtocolWebSocketHandler.java
+2
-2
spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/AbstractHttpSendingTransportHandler.java
...ransport/handler/AbstractHttpSendingTransportHandler.java
+2
-2
spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/session/WebSocketServerSockJsSession.java
...ockjs/transport/session/WebSocketServerSockJsSession.java
+2
-2
未找到文件。
spring-core/src/main/java/org/springframework/util/StringUtils.java
浏览文件 @
a7949ac8
...
...
@@ -75,15 +75,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)}.
* <p>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.
* <p>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
* <p><b>Note: If the object is typed to {@code String} upfront, prefer
* {@link #hasLength(String)} or {@link #hasText(String)} instead.</b>
* @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
));
...
...
@@ -102,7 +107,8 @@ public abstract class StringUtils {
* </pre>
* @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
);
...
...
@@ -136,6 +142,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
)
{
...
...
@@ -151,6 +159,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
));
...
...
spring-jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java
浏览文件 @
a7949ac8
...
...
@@ -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
...
...
spring-web/src/main/java/org/springframework/http/HttpHeaders.java
浏览文件 @
a7949ac8
...
...
@@ -699,7 +699,7 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable
*/
public
Set
<
HttpMethod
>
getAllow
()
{
String
value
=
getFirst
(
ALLOW
);
if
(
!
StringUtils
.
isEmpty
(
value
))
{
if
(
StringUtils
.
hasLength
(
value
))
{
String
[]
tokens
=
StringUtils
.
tokenizeToStringArray
(
value
,
","
);
List
<
HttpMethod
>
result
=
new
ArrayList
<>(
tokens
.
length
);
for
(
String
token
:
tokens
)
{
...
...
spring-web/src/main/java/org/springframework/http/server/reactive/UndertowServerHttpRequest.java
浏览文件 @
a7949ac8
...
...
@@ -74,7 +74,7 @@ class UndertowServerHttpRequest extends AbstractServerHttpRequest {
Assert
.
notNull
(
exchange
,
"HttpServerExchange is required."
);
String
requestURL
=
exchange
.
getRequestURL
();
String
query
=
exchange
.
getQueryString
();
String
requestUriAndQuery
=
StringUtils
.
isEmpty
(
query
)
?
requestURL
:
requestURL
+
"?"
+
query
;
String
requestUriAndQuery
=
(
StringUtils
.
hasLength
(
query
)
?
requestURL
+
"?"
+
query
:
requestURL
)
;
return
new
URI
(
requestUriAndQuery
);
}
...
...
spring-web/src/main/java/org/springframework/web/method/annotation/RequestParamMethodArgumentResolver.java
浏览文件 @
a7949ac8
/*
* Copyright 2002-201
8
the original author or authors.
* Copyright 2002-201
9
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.
...
...
@@ -213,8 +213,8 @@ public class RequestParamMethodArgumentResolver extends AbstractNamedValueMethod
}
RequestParam
requestParam
=
parameter
.
getParameterAnnotation
(
RequestParam
.
class
);
String
name
=
(
requestParam
==
null
||
StringUtils
.
isEmpty
(
requestParam
.
name
())
?
parameter
.
getParameterName
()
:
requestParam
.
n
ame
());
String
name
=
(
requestParam
!=
null
&&
StringUtils
.
hasLength
(
requestParam
.
name
())
?
requestParam
.
name
()
:
parameter
.
getParameterN
ame
());
Assert
.
state
(
name
!=
null
,
"Unresolvable parameter name"
);
if
(
value
==
null
)
{
...
...
spring-web/src/main/java/org/springframework/web/util/DefaultUriBuilderFactory.java
浏览文件 @
a7949ac8
/*
* Copyright 2002-201
8
the original author or authors.
* Copyright 2002-201
9
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.
...
...
@@ -33,7 +33,6 @@ import org.springframework.util.StringUtils;
* <p>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
;
}
...
...
spring-webflux/src/main/java/org/springframework/web/reactive/resource/VersionResourceResolver.java
浏览文件 @
a7949ac8
/*
* Copyright 2002-201
8
the original author or authors.
* Copyright 2002-201
9
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
();
}
...
...
spring-webflux/src/main/java/org/springframework/web/reactive/result/view/RedirectView.java
浏览文件 @
a7949ac8
...
...
@@ -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
)
{
...
...
spring-webmvc/src/main/java/org/springframework/web/servlet/DispatcherServlet.java
浏览文件 @
a7949ac8
...
...
@@ -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
+
"}"
);
query
Clause
+
"\", parameters={"
+
params
+
"}"
);
if
(
traceOn
)
{
List
<
String
>
values
=
Collections
.
list
(
request
.
getHeaderNames
());
...
...
spring-webmvc/src/main/java/org/springframework/web/servlet/handler/AbstractHandlerExceptionResolver.java
浏览文件 @
a7949ac8
...
...
@@ -100,8 +100,9 @@ public abstract class AbstractHandlerExceptionResolver implements HandlerExcepti
/**
* Set the log category for warn logging. The name will be passed to the underlying logger
* implementation through Commons Logging, getting interpreted as a log category according
* to the logger's configuration. If {@code null} is passed, warn logging is turned off.
* <p>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.
* <p>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
);
}
/**
...
...
spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/MvcUriComponentsBuilder.java
浏览文件 @
a7949ac8
/*
* Copyright 2002-201
8
the original author or authors.
* Copyright 2002-201
9
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
())
{
...
...
spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/PathVariableMethodArgumentResolver.java
浏览文件 @
a7949ac8
/*
* Copyright 2002-201
7
the original author or authors.
* Copyright 2002-201
9
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
);
}
...
...
spring-webmvc/src/main/java/org/springframework/web/servlet/resource/VersionResourceResolver.java
浏览文件 @
a7949ac8
/*
* Copyright 2002-201
8
the original author or authors.
* Copyright 2002-201
9
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
;
}
...
...
spring-webmvc/src/main/java/org/springframework/web/servlet/support/ServletUriComponentsBuilder.java
浏览文件 @
a7949ac8
/*
* Copyright 2002-201
8
the original author or authors.
* Copyright 2002-201
9
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
));
}
...
...
spring-webmvc/src/main/java/org/springframework/web/servlet/view/RedirectView.java
浏览文件 @
a7949ac8
...
...
@@ -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
())
{
...
...
spring-websocket/src/main/java/org/springframework/web/socket/messaging/SubProtocolWebSocketHandler.java
浏览文件 @
a7949ac8
/*
* Copyright 2002-201
8
the original author or authors.
* Copyright 2002-201
9
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.
...
...
@@ -420,7 +420,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
(
...
...
spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/AbstractHttpSendingTransportHandler.java
浏览文件 @
a7949ac8
/*
* Copyright 2002-201
8
the original author or authors.
* Copyright 2002-201
9
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
<
String
,
String
>
params
=
UriComponentsBuilder
.
newInstance
().
query
(
query
).
build
().
getQueryParams
();
String
value
=
params
.
getFirst
(
"c"
);
if
(
StringUtils
.
isEmpty
(
value
))
{
if
(
!
StringUtils
.
hasLength
(
value
))
{
return
null
;
}
String
result
=
UriUtils
.
decode
(
value
,
StandardCharsets
.
UTF_8
);
...
...
spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/session/WebSocketServerSockJsSession.java
浏览文件 @
a7949ac8
/*
* Copyright 2002-201
7
the original author or authors.
* Copyright 2002-201
9
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.
...
...
@@ -179,7 +179,7 @@ public class WebSocketServerSockJsSession extends AbstractSockJsSession implemen
public
void
handleMessage
(
TextMessage
message
,
WebSocketSession
wsSession
)
throws
Exception
{
String
payload
=
message
.
getPayload
();
if
(
StringUtils
.
isEmpty
(
payload
))
{
if
(
!
StringUtils
.
hasLength
(
payload
))
{
return
;
}
String
[]
messages
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录