From ac18e3015cbe61ab5ddd5d82ef64c908b3f91c4b Mon Sep 17 00:00:00 2001 From: Yanming Zhou Date: Tue, 19 Dec 2023 22:27:26 -0800 Subject: [PATCH] Use `.isEmpty()` where feasible See gh-38739 --- .../java/org/springframework/boot/build/bom/BomPlugin.java | 2 +- .../boot/actuate/health/HealthEndpointSupport.java | 2 +- .../boot/autoconfigure/condition/OnBeanCondition.java | 6 +++--- .../AutoConfigureAnnotationProcessor.java | 2 +- .../boot/configurationprocessor/json/JSONStringer.java | 4 ++-- .../boot/configurationprocessor/metadata/ItemMetadata.java | 4 ++-- .../org/springframework/boot/maven/AbstractAotMojo.java | 2 +- .../org/springframework/boot/maven/AbstractRunMojo.java | 2 +- .../org/springframework/boot/maven/CommandLineBuilder.java | 2 +- .../java/org/springframework/boot/StartupInfoLogger.java | 6 +++--- .../context/properties/bind/DataObjectPropertyName.java | 4 ++-- .../boot/context/properties/bind/MapBinder.java | 2 +- .../properties/source/ConfigurationPropertyName.java | 6 +++--- .../properties/source/SystemEnvironmentPropertyMapper.java | 6 +++--- .../java/org/springframework/boot/json/BasicJsonParser.java | 4 ++-- .../springframework/boot/logging/log4j2/ColorConverter.java | 2 +- .../boot/logging/logback/LogbackLoggingSystem.java | 4 ++-- .../boot/r2dbc/ConnectionFactoryBuilder.java | 2 +- 18 files changed, 31 insertions(+), 31 deletions(-) diff --git a/buildSrc/src/main/java/org/springframework/boot/build/bom/BomPlugin.java b/buildSrc/src/main/java/org/springframework/boot/build/bom/BomPlugin.java index a18d10481d8..6cbd5ae787b 100644 --- a/buildSrc/src/main/java/org/springframework/boot/build/bom/BomPlugin.java +++ b/buildSrc/src/main/java/org/springframework/boot/build/bom/BomPlugin.java @@ -221,7 +221,7 @@ public class BomPlugin implements Plugin { .collect(Collectors.toSet()); Node target = dependency; for (String classifier : classifiers) { - if (classifier.length() > 0) { + if (!classifier.isEmpty()) { if (target == null) { target = new Node(null, "dependency"); target.appendNode("groupId", groupId); diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/HealthEndpointSupport.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/HealthEndpointSupport.java index b15aa920591..79445877bd6 100644 --- a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/HealthEndpointSupport.java +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/HealthEndpointSupport.java @@ -127,7 +127,7 @@ abstract class HealthEndpointSupport { private String getName(String[] path, int pathOffset) { StringBuilder name = new StringBuilder(); while (pathOffset < path.length) { - name.append((name.length() != 0) ? "/" : ""); + name.append((!name.isEmpty()) ? "/" : ""); name.append(path[pathOffset]); pathOffset++; } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/OnBeanCondition.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/OnBeanCondition.java index 1de90f0c917..60f0f90cdd5 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/OnBeanCondition.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/OnBeanCondition.java @@ -332,7 +332,7 @@ class OnBeanCondition extends FilteringSpringBootCondition implements Configurat private void appendMessageForNoMatches(StringBuilder reason, Collection unmatched, String description) { if (!unmatched.isEmpty()) { - if (reason.length() > 0) { + if (!reason.isEmpty()) { reason.append(" and "); } reason.append("did not find any beans "); @@ -347,7 +347,7 @@ class OnBeanCondition extends FilteringSpringBootCondition implements Configurat appendMessageForMatches(reason, matchResult.getMatchedAnnotations(), "annotated with"); appendMessageForMatches(reason, matchResult.getMatchedTypes(), "of type"); if (!matchResult.getMatchedNames().isEmpty()) { - if (reason.length() > 0) { + if (!reason.isEmpty()) { reason.append(" and "); } reason.append("found beans named "); @@ -360,7 +360,7 @@ class OnBeanCondition extends FilteringSpringBootCondition implements Configurat String description) { if (!matches.isEmpty()) { matches.forEach((key, value) -> { - if (reason.length() > 0) { + if (!reason.isEmpty()) { reason.append(" and "); } reason.append("found beans "); diff --git a/spring-boot-project/spring-boot-tools/spring-boot-autoconfigure-processor/src/main/java/org/springframework/boot/autoconfigureprocessor/AutoConfigureAnnotationProcessor.java b/spring-boot-project/spring-boot-tools/spring-boot-autoconfigure-processor/src/main/java/org/springframework/boot/autoconfigureprocessor/AutoConfigureAnnotationProcessor.java index 2137997890a..90099cbe8fe 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-autoconfigure-processor/src/main/java/org/springframework/boot/autoconfigureprocessor/AutoConfigureAnnotationProcessor.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-autoconfigure-processor/src/main/java/org/springframework/boot/autoconfigureprocessor/AutoConfigureAnnotationProcessor.java @@ -345,7 +345,7 @@ public class AutoConfigureAnnotationProcessor extends AbstractProcessor { } StringBuilder result = new StringBuilder(); for (Object item : list) { - result.append((result.length() != 0) ? "," : ""); + result.append((!result.isEmpty()) ? "," : ""); result.append(item); } return result.toString(); diff --git a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/json-shade/java/org/springframework/boot/configurationprocessor/json/JSONStringer.java b/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/json-shade/java/org/springframework/boot/configurationprocessor/json/JSONStringer.java index 23d41637ab3..29028b60585 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/json-shade/java/org/springframework/boot/configurationprocessor/json/JSONStringer.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/json-shade/java/org/springframework/boot/configurationprocessor/json/JSONStringer.java @@ -173,7 +173,7 @@ public class JSONStringer { * @throws JSONException if processing of json failed */ JSONStringer open(Scope empty, String openBracket) throws JSONException { - if (this.stack.isEmpty() && this.out.length() > 0) { + if (this.stack.isEmpty() && !this.out.isEmpty()) { throw new JSONException("Nesting problem: multiple top-level roots"); } beforeValue(); @@ -423,7 +423,7 @@ public class JSONStringer { */ @Override public String toString() { - return this.out.length() == 0 ? null : this.out.toString(); + return this.out.isEmpty() ? null : this.out.toString(); } } diff --git a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/metadata/ItemMetadata.java b/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/metadata/ItemMetadata.java index 315a281774e..f2f1e7e5e2f 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/metadata/ItemMetadata.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/metadata/ItemMetadata.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2023 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. @@ -65,7 +65,7 @@ public final class ItemMetadata implements Comparable { fullName.append(prefix); } if (name != null) { - if (fullName.length() > 0) { + if (!fullName.isEmpty()) { fullName.append('.'); } fullName.append(ConfigurationMetadata.toDashedCase(name)); diff --git a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/AbstractAotMojo.java b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/AbstractAotMojo.java index 9b6707794d0..8c6c9599ae3 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/AbstractAotMojo.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/AbstractAotMojo.java @@ -227,7 +227,7 @@ public abstract class AbstractAotMojo extends AbstractDependencyFilterMojo { } boolean hasReportedErrors() { - return this.message.length() > 0; + return !this.message.isEmpty(); } @Override diff --git a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/AbstractRunMojo.java b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/AbstractRunMojo.java index c2c6a914772..98688bcaecb 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/AbstractRunMojo.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/AbstractRunMojo.java @@ -329,7 +329,7 @@ public abstract class AbstractRunMojo extends AbstractDependencyFilterMojo { try { StringBuilder classpath = new StringBuilder(); for (URL ele : getClassPathUrls()) { - if (classpath.length() > 0) { + if (!classpath.isEmpty()) { classpath.append(File.pathSeparator); } classpath.append(new File(ele.toURI())); diff --git a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/CommandLineBuilder.java b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/CommandLineBuilder.java index 1fa13246508..d2a04dd6910 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/CommandLineBuilder.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/CommandLineBuilder.java @@ -98,7 +98,7 @@ final class CommandLineBuilder { static String build(List classpathElements) { StringBuilder classpath = new StringBuilder(); for (URL element : classpathElements) { - if (classpath.length() > 0) { + if (!classpath.isEmpty()) { classpath.append(File.pathSeparator); } classpath.append(toFile(element)); diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/StartupInfoLogger.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/StartupInfoLogger.java index 1f3104bb60b..804319fbb0c 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/StartupInfoLogger.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/StartupInfoLogger.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2022 the original author or authors. + * Copyright 2012-2023 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. @@ -121,7 +121,7 @@ class StartupInfoLogger { } append(context, "started by ", () -> System.getProperty("user.name")); append(context, "in ", () -> System.getProperty("user.dir")); - if (context.length() > 0) { + if (!context.isEmpty()) { message.append(" ("); message.append(context); message.append(")"); @@ -143,7 +143,7 @@ class StartupInfoLogger { value = defaultValue; } if (StringUtils.hasLength(value)) { - message.append((message.length() > 0) ? " " : ""); + message.append((!message.isEmpty()) ? " " : ""); message.append(prefix); message.append(value); } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/DataObjectPropertyName.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/DataObjectPropertyName.java index 041a439b2f9..009e6ad563a 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/DataObjectPropertyName.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/DataObjectPropertyName.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2023 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. @@ -52,7 +52,7 @@ public abstract class DataObjectPropertyName { } else { ch = (ch != '_') ? ch : '-'; - if (Character.isUpperCase(ch) && result.length() > 0 && result.charAt(result.length() - 1) != '-') { + if (Character.isUpperCase(ch) && !result.isEmpty() && result.charAt(result.length() - 1) != '-') { result.append('-'); } result.append(Character.toLowerCase(ch)); diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/MapBinder.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/MapBinder.java index 3f1eaf61f80..1329c7a043a 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/MapBinder.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/MapBinder.java @@ -211,7 +211,7 @@ class MapBinder extends AggregateBinder> { private String getKeyName(ConfigurationPropertyName name) { StringBuilder result = new StringBuilder(); for (int i = this.root.getNumberOfElements(); i < name.getNumberOfElements(); i++) { - if (result.length() != 0) { + if (!result.isEmpty()) { result.append('.'); } result.append(name.getElement(i, Form.ORIGINAL)); diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/ConfigurationPropertyName.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/ConfigurationPropertyName.java index 85a76d37263..027f1b384b8 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/ConfigurationPropertyName.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/ConfigurationPropertyName.java @@ -543,7 +543,7 @@ public final class ConfigurationPropertyName implements Comparable 0 && !indexed) { + if (!result.isEmpty() && !indexed) { result.append('.'); } if (indexed) { @@ -615,7 +615,7 @@ public final class ConfigurationPropertyName implements Comparable elementValueProcessor) { Assert.notNull(name, "Name must not be null"); - if (name.length() == 0) { + if (name.isEmpty()) { return EMPTY; } Elements elements = new ElementsParser(name, separator).parse(elementValueProcessor); diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/SystemEnvironmentPropertyMapper.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/SystemEnvironmentPropertyMapper.java index f921bbca4ba..aec68b63888 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/SystemEnvironmentPropertyMapper.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/SystemEnvironmentPropertyMapper.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2023 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. @@ -57,7 +57,7 @@ final class SystemEnvironmentPropertyMapper implements PropertyMapper { private String convertName(ConfigurationPropertyName name, int numberOfElements) { StringBuilder result = new StringBuilder(); for (int i = 0; i < numberOfElements; i++) { - if (result.length() > 0) { + if (!result.isEmpty()) { result.append('_'); } result.append(name.getElement(i, Form.UNIFORM).toUpperCase(Locale.ENGLISH)); @@ -68,7 +68,7 @@ final class SystemEnvironmentPropertyMapper implements PropertyMapper { private String convertLegacyName(ConfigurationPropertyName name) { StringBuilder result = new StringBuilder(); for (int i = 0; i < name.getNumberOfElements(); i++) { - if (result.length() > 0) { + if (!result.isEmpty()) { result.append('_'); } result.append(convertLegacyNameElement(name.getElement(i, Form.ORIGINAL))); diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/json/BasicJsonParser.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/json/BasicJsonParser.java index dad9038123b..130245d5bb9 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/json/BasicJsonParser.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/json/BasicJsonParser.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2022 the original author or authors. + * Copyright 2012-2023 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. @@ -158,7 +158,7 @@ public class BasicJsonParser extends AbstractJsonParser { } index++; } - if (build.length() > 0) { + if (!build.isEmpty()) { list.add(build.toString().trim()); } return list; diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/ColorConverter.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/ColorConverter.java index 84bcedb4048..86abf0eb81b 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/ColorConverter.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/ColorConverter.java @@ -130,7 +130,7 @@ public final class ColorConverter extends LogEventPatternConverter { for (PatternFormatter formatter : this.formatters) { formatter.format(event, buf); } - if (buf.length() > 0) { + if (!buf.isEmpty()) { AnsiElement element = this.styling; if (element == null) { // Assume highlighting diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/LogbackLoggingSystem.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/LogbackLoggingSystem.java index 9915b0a5501..e3cdfaac962 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/LogbackLoggingSystem.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/LogbackLoggingSystem.java @@ -258,14 +258,14 @@ public class LogbackLoggingSystem extends AbstractLoggingSystem implements BeanF List suppressedExceptions = new ArrayList<>(); for (Status status : loggerContext.getStatusManager().getCopyOfStatusList()) { if (status.getLevel() == Status.ERROR) { - errors.append((errors.length() > 0) ? String.format("%n") : ""); + errors.append((!errors.isEmpty()) ? String.format("%n") : ""); errors.append(status.toString()); if (status.getThrowable() != null) { suppressedExceptions.add(status.getThrowable()); } } } - if (errors.length() == 0) { + if (errors.isEmpty()) { if (!StatusUtil.contextHasStatusListener(loggerContext)) { StatusPrinter.printInCaseOfErrorsOrWarnings(loggerContext); } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/r2dbc/ConnectionFactoryBuilder.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/r2dbc/ConnectionFactoryBuilder.java index e69eca5d33b..738cdbbb9eb 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/r2dbc/ConnectionFactoryBuilder.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/r2dbc/ConnectionFactoryBuilder.java @@ -212,7 +212,7 @@ public final class ConnectionFactoryBuilder { private ConnectionFactoryOptions delegateFactoryOptions(ConnectionFactoryOptions options) { String protocol = toString(options.getRequiredValue(ConnectionFactoryOptions.PROTOCOL)); - if (protocol.trim().length() == 0) { + if (protocol.trim().isEmpty()) { throw new IllegalArgumentException(String.format("Protocol %s is not valid.", protocol)); } String[] protocols = protocol.split(COLON, 2);