Skip to content

Commit

Permalink
Deprecate StringUtils.isEmpty(Object) and replace remaining usage
Browse files Browse the repository at this point in the history
Closes gh-25945
  • Loading branch information
jhoeller committed Oct 21, 2020
1 parent 07769dd commit 621295d
Show file tree
Hide file tree
Showing 8 changed files with 13 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2020 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.
Expand Down Expand Up @@ -40,7 +40,6 @@
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

/**
* {@link AutowireCandidateResolver} implementation that matches bean definition qualifiers
Expand Down Expand Up @@ -188,7 +187,7 @@ protected boolean checkQualifiers(BeanDefinitionHolder bdHolder, Annotation[] an
foundMeta = true;
// Only accept fallback match if @Qualifier annotation has a value...
// Otherwise it is just a marker for a custom qualifier annotation.
if ((fallbackToMeta && StringUtils.isEmpty(AnnotationUtils.getValue(metaAnn))) ||
if ((fallbackToMeta && ObjectUtils.isEmpty(AnnotationUtils.getValue(metaAnn))) ||
!checkQualifier(bdHolder, metaAnn, typeConverter)) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ public static boolean isEmpty(@Nullable Object[] array) {
* @see Optional#isPresent()
* @see ObjectUtils#isEmpty(Object[])
* @see StringUtils#hasLength(CharSequence)
* @see StringUtils#isEmpty(Object)
* @see CollectionUtils#isEmpty(java.util.Collection)
* @see CollectionUtils#isEmpty(java.util.Map)
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,10 @@ public abstract class StringUtils {
* {@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)
* @deprecated as of 5.3, in favor of {@link #hasLength(String)} and
* {@link #hasText(String)} (or {@link ObjectUtils#isEmpty(Object)})
*/
@Deprecated
public static boolean isEmpty(@Nullable Object str) {
return (str == null || "".equals(str));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.springframework.expression.spel.ExpressionState;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

/**
* Represents the elvis operator ?:. For an expression "a?:b" if a is not null, the value
Expand Down Expand Up @@ -52,7 +51,7 @@ public Elvis(int startPos, int endPos, SpelNodeImpl... args) {
public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
TypedValue value = this.children[0].getValueInternal(state);
// If this check is changed, the generateCode method will need changing too
if (!StringUtils.isEmpty(value.getValue())) {
if (value.getValue() != null && !"".equals(value.getValue())) {

This comment has been minimized.

Copy link
@sabi0

sabi0 Oct 21, 2020

It seems ObjectUtils.isNotEmpty should be used here. Or the javadoc should be updated to mention that an empty String is considered 'null' here (unlike, e.g. an empty List).

This comment has been minimized.

Copy link
@jhoeller

jhoeller Oct 21, 2020

Author Contributor

This is mostly for strict alignment with SpEL's compilation code where it also only checks against empty Strings. Empty Strings do have a special role in SpEL expressions which are inherently derived from original Strings, whereas any other empty values are user-specific in some form. At this point I'm not trying to change SpEL's semantics there, just making the special empty String handling more explicit.

return value;
}
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.springframework.util.AlternativeJdkIdGenerator;
import org.springframework.util.Assert;
import org.springframework.util.IdGenerator;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.util.concurrent.ListenableFutureCallback;
Expand Down Expand Up @@ -263,7 +264,7 @@ private StompHeaderAccessor createHeaderAccessor(StompCommand command) {
private Message<byte[]> createMessage(StompHeaderAccessor accessor, @Nullable Object payload) {
accessor.updateSimpMessageHeadersFromStompHeaders();
Message<byte[]> message;
if (StringUtils.isEmpty(payload) || (payload instanceof byte[] && ((byte[]) payload).length == 0)) {
if (ObjectUtils.isEmpty(payload)) {
message = MessageBuilder.createMessage(EMPTY_PAYLOAD, accessor.getMessageHeaders());
}
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -683,13 +683,12 @@ static class OffsetDateTimeDeserializer extends JsonDeserializer<OffsetDateTime>

@Override
public OffsetDateTime deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
final String value = jsonParser.getValueAsString();
if (StringUtils.isEmpty(value)) {
String value = jsonParser.getValueAsString();
if (!StringUtils.hasLength(value)) {
return null;
}
try {
return OffsetDateTime.parse(value);

}
catch (DateTimeParseException exception) {
return OffsetDateTime.parse(value + CURRENT_ZONE_OFFSET);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,10 @@ private String getPartName(MethodParameter methodParam, @Nullable RequestPart re
if (requestPart != null) {
name = requestPart.name();
}
if (StringUtils.isEmpty(name)) {
if (!StringUtils.hasLength(name)) {
name = methodParam.getParameterName();
}
if (StringUtils.isEmpty(name)) {
if (!StringUtils.hasLength(name)) {
throw new IllegalArgumentException("Request part name for argument type [" +
methodParam.getNestedParameterType().getName() +
"] not specified, and parameter name information not found in class file either.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public boolean equalsCode(CloseStatus other) {
* @since 5.3
*/
public static CloseStatus create(int code, @Nullable String reason) {
if (StringUtils.isEmpty(reason)) {
if (!StringUtils.hasText(reason)) {
switch (code) {
case 1000:
return NORMAL;
Expand Down

0 comments on commit 621295d

Please sign in to comment.