From 2d8528d5bd33160a5971e684b88e92ec02c0d2c9 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Thu, 22 Oct 2020 17:09:33 +0100 Subject: [PATCH] Adapt to deprecation of StringUtils.isEmpty(Object) See gh-23774 --- .../boot/autoconfigure/condition/ConditionMessage.java | 8 ++++---- .../boot/autoconfigure/domain/EntityScanPackages.java | 2 +- .../boot/test/util/TestPropertyValues.java | 10 +++++----- .../docker/transport/DockerEngineException.java | 4 ++-- .../boot/logging/log4j2/Log4J2LoggingSystem.java | 2 +- .../boot/web/context/WebServerPortFileWriter.java | 2 +- .../boot/web/embedded/netty/CompressionCustomizer.java | 4 ++-- .../embedded/undertow/UndertowServletWebServer.java | 2 +- .../boot/web/server/SslConfigurationValidator.java | 2 +- .../boot/web/servlet/error/DefaultErrorAttributes.java | 5 +++-- 10 files changed, 21 insertions(+), 20 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ConditionMessage.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ConditionMessage.java index 8c7cc97e09..71c30e0933 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ConditionMessage.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ConditionMessage.java @@ -296,11 +296,11 @@ public final class ConditionMessage { * @return a built {@link ConditionMessage} */ public ConditionMessage because(String reason) { - if (StringUtils.isEmpty(reason)) { - return new ConditionMessage(ConditionMessage.this, this.condition); + if (StringUtils.hasLength(reason)) { + return new ConditionMessage(ConditionMessage.this, + StringUtils.hasLength(this.condition) ? this.condition + " " + reason : reason); } - return new ConditionMessage(ConditionMessage.this, - StringUtils.isEmpty(this.condition) ? reason : this.condition + " " + reason); + return new ConditionMessage(ConditionMessage.this, this.condition); } } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/domain/EntityScanPackages.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/domain/EntityScanPackages.java index 8e1a424ad0..0cbafed3f2 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/domain/EntityScanPackages.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/domain/EntityScanPackages.java @@ -140,7 +140,7 @@ public class EntityScanPackages { } if (packagesToScan.isEmpty()) { String packageName = ClassUtils.getPackageName(metadata.getClassName()); - Assert.state(!StringUtils.isEmpty(packageName), "@EntityScan cannot be used with the default package"); + Assert.state(StringUtils.hasLength(packageName), "@EntityScan cannot be used with the default package"); return Collections.singleton(packageName); } return packagesToScan; diff --git a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/util/TestPropertyValues.java b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/util/TestPropertyValues.java index e9ca8b6ede..5695fb884e 100644 --- a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/util/TestPropertyValues.java +++ b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/util/TestPropertyValues.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-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. @@ -277,10 +277,10 @@ public final class TestPropertyValues { } private static Pair of(String name, String value) { - if (StringUtils.isEmpty(name) && StringUtils.isEmpty(value)) { - return null; + if (StringUtils.hasLength(name) || StringUtils.hasLength(value)) { + return new Pair(name, value); } - return new Pair(name, value); + return null; } } @@ -309,7 +309,7 @@ public final class TestPropertyValues { private String setOrClear(String name, String value) { Assert.notNull(name, "Name must not be null"); - if (StringUtils.isEmpty(value)) { + if (!StringUtils.hasLength(value)) { return (String) System.getProperties().remove(name); } return (String) System.getProperties().setProperty(name, value); diff --git a/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/docker/transport/DockerEngineException.java b/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/docker/transport/DockerEngineException.java index b7780f8605..e521f4a8af 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/docker/transport/DockerEngineException.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/docker/transport/DockerEngineException.java @@ -87,10 +87,10 @@ public class DockerEngineException extends RuntimeException { Assert.notNull(uri, "URI must not be null"); StringBuilder message = new StringBuilder( "Docker API call to '" + host + uri + "' failed with status code " + statusCode); - if (!StringUtils.isEmpty(reasonPhrase)) { + if (StringUtils.hasLength(reasonPhrase)) { message.append(" \"").append(reasonPhrase).append("\""); } - if (responseMessage != null && !StringUtils.isEmpty(responseMessage.getMessage())) { + if (responseMessage != null && StringUtils.hasLength(responseMessage.getMessage())) { message.append(" and message \"").append(responseMessage.getMessage()).append("\""); } if (errors != null && !errors.isEmpty()) { diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystem.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystem.java index 3195bb0721..1d90defc54 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystem.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystem.java @@ -258,7 +258,7 @@ public class Log4J2LoggingSystem extends Slf4JLoggingSystem { } private String getSubName(String name) { - if (StringUtils.isEmpty(name)) { + if (!StringUtils.hasLength(name)) { return null; } int nested = name.lastIndexOf('$'); diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/context/WebServerPortFileWriter.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/context/WebServerPortFileWriter.java index f647421660..7b953c1c33 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/context/WebServerPortFileWriter.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/context/WebServerPortFileWriter.java @@ -105,7 +105,7 @@ public class WebServerPortFileWriter implements ApplicationListener { String contentType = response.responseHeaders().get(HttpHeaderNames.CONTENT_TYPE); - if (StringUtils.isEmpty(contentType)) { + if (!StringUtils.hasLength(contentType)) { return false; } try { diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/undertow/UndertowServletWebServer.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/undertow/UndertowServletWebServer.java index 1a90823563..4c8c511f90 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/undertow/UndertowServletWebServer.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/undertow/UndertowServletWebServer.java @@ -123,7 +123,7 @@ public class UndertowServletWebServer extends UndertowWebServer { @Override protected HttpHandler createHttpHandler() { HttpHandler handler = super.createHttpHandler(); - if (!StringUtils.isEmpty(this.contextPath)) { + if (StringUtils.hasLength(this.contextPath)) { handler = Handlers.path().addPrefixPath(this.contextPath, handler); } return handler; diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/server/SslConfigurationValidator.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/server/SslConfigurationValidator.java index a04027619a..88fff39537 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/server/SslConfigurationValidator.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/server/SslConfigurationValidator.java @@ -34,7 +34,7 @@ public final class SslConfigurationValidator { } public static void validateKeyAlias(KeyStore keyStore, String keyAlias) { - if (!StringUtils.isEmpty(keyAlias)) { + if (StringUtils.hasLength(keyAlias)) { try { Assert.state(keyStore.containsAlias(keyAlias), () -> String.format("Keystore does not contain specified alias '%s'", keyAlias)); diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/error/DefaultErrorAttributes.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/error/DefaultErrorAttributes.java index 50495325e0..e6c5d12242 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/error/DefaultErrorAttributes.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/error/DefaultErrorAttributes.java @@ -32,6 +32,7 @@ import org.springframework.boot.web.error.ErrorAttributeOptions.Include; import org.springframework.core.Ordered; import org.springframework.core.annotation.Order; import org.springframework.http.HttpStatus; +import org.springframework.util.ObjectUtils; import org.springframework.util.StringUtils; import org.springframework.validation.BindingResult; import org.springframework.validation.ObjectError; @@ -199,10 +200,10 @@ public class DefaultErrorAttributes implements ErrorAttributes, HandlerException */ protected String getMessage(WebRequest webRequest, Throwable error) { Object message = getAttribute(webRequest, RequestDispatcher.ERROR_MESSAGE); - if (!StringUtils.isEmpty(message)) { + if (!ObjectUtils.isEmpty(message)) { return message.toString(); } - if (error != null && !StringUtils.isEmpty(error.getMessage())) { + if (error != null && StringUtils.hasLength(error.getMessage())) { return error.getMessage(); } return "No message available";