From 3792c71a8768b8959de33155f1a4e0457ed760d6 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Mon, 22 May 2023 13:24:12 +0200 Subject: [PATCH] [MNG-6829] Replace StringUtils#isEmpty(String) and #isNotEmpty(String) (#272) * [MNG-6829] Replace StringUtils#isEmpty(String) and #isNotEmpty(String) Continuation of https://issues.apache.org/jira/browse/MNG-6829 Review requested of @elharo Use this link to re-run the recipe: https://public.moderne.io/recipes/org.openrewrite.java.migrate.apache.commons.lang.IsNotEmptyToJdk?organizationId=QXBhY2hlIE1hdmVu Co-authored-by: Moderne * Remove duplicate null check Co-authored-by: Stephan Schroevers * Apply Spotless --------- Co-authored-by: Moderne Co-authored-by: Stephan Schroevers --- .../enforcer/rules/BannedRepositories.java | 3 +-- .../enforcer/rules/EvaluateBeanshell.java | 3 +-- .../enforcer/rules/RequireActiveProfile.java | 3 +-- .../maven/enforcer/rules/RequireOS.java | 25 ++++++++++--------- .../enforcer/rules/RequirePluginVersions.java | 10 ++++---- .../version/AbstractVersionEnforcer.java | 7 +++--- .../rules/version/RequireJavaVersion.java | 2 +- 7 files changed, 25 insertions(+), 28 deletions(-) diff --git a/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/BannedRepositories.java b/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/BannedRepositories.java index 9284f635..cff0840f 100644 --- a/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/BannedRepositories.java +++ b/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/BannedRepositories.java @@ -29,7 +29,6 @@ import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.enforcer.rule.api.EnforcerRuleException; import org.apache.maven.project.MavenProject; -import org.codehaus.plexus.util.StringUtils; /** * This rule checks that this project's maven session whether have banned repositories. @@ -91,7 +90,7 @@ public void execute() throws EnforcerRuleException { String errMsg = repoErrMsg + pluginRepoErrMsg; - if (errMsg != null && !StringUtils.isEmpty(errMsg)) { + if (errMsg != null && !errMsg.isEmpty()) { throw new EnforcerRuleException(errMsg); } } diff --git a/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/EvaluateBeanshell.java b/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/EvaluateBeanshell.java index 07dfb7c8..8bf59367 100644 --- a/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/EvaluateBeanshell.java +++ b/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/EvaluateBeanshell.java @@ -28,7 +28,6 @@ import org.apache.maven.enforcer.rule.api.EnforcerRuleException; import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException; import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator; -import org.codehaus.plexus.util.StringUtils; /** * Rule for Maven Enforcer using Beanshell to evaluate a conditional expression. @@ -70,7 +69,7 @@ public void execute() throws EnforcerRuleException { getLog().debug("Echo script : " + script); if (!evaluateCondition(script)) { String message = getMessage(); - if (StringUtils.isEmpty(message)) { + if (message == null || message.isEmpty()) { message = "The expression \"" + condition + "\" is not true."; } throw new EnforcerRuleException(message); diff --git a/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/RequireActiveProfile.java b/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/RequireActiveProfile.java index 58878ce5..7df8486a 100644 --- a/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/RequireActiveProfile.java +++ b/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/RequireActiveProfile.java @@ -28,7 +28,6 @@ import org.apache.maven.enforcer.rule.api.EnforcerRuleException; import org.apache.maven.project.MavenProject; -import org.codehaus.plexus.util.StringUtils; /** * This rule checks that some profiles are active. @@ -72,7 +71,7 @@ public void setAll(boolean all) { @Override public void execute() throws EnforcerRuleException { List missingProfiles = new ArrayList<>(); - if (StringUtils.isNotEmpty(profiles)) { + if (profiles != null && !profiles.isEmpty()) { String[] profileIds = profiles.split(","); for (String profileId : profileIds) { if (!isProfileActive(project, profileId)) { diff --git a/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/RequireOS.java b/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/RequireOS.java index eae734aa..e6051436 100644 --- a/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/RequireOS.java +++ b/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/RequireOS.java @@ -103,16 +103,16 @@ public void execute() throws EnforcerRuleException { if (isValidFamily(this.family)) { if (!isAllowed()) { String message = getMessage(); - if (StringUtils.isEmpty(message)) { + if (message == null || message.isEmpty()) { // @formatter:off - message = ("OS Arch: " + message = "OS Arch: " + Os.OS_ARCH + " Family: " + Os.OS_FAMILY + " Name: " + Os.OS_NAME + " Version: " + Os.OS_VERSION + " is not allowed by" + (arch != null ? " Arch=" + arch : "") + (family != null ? " Family=" + family : "") + (name != null ? " Name=" + name : "") - + (version != null ? " Version=" + version : "")); + + (version != null ? " Version=" + version : ""); // @formatter:on } throw new EnforcerRuleException(message); @@ -152,10 +152,10 @@ public boolean isAllowed() { * @return true if all parameters are empty. */ public boolean allParamsEmpty() { - return (StringUtils.isEmpty(family) - && StringUtils.isEmpty(arch) - && StringUtils.isEmpty(name) - && StringUtils.isEmpty(version)); + return (family == null || family.isEmpty()) + && (arch == null || arch.isEmpty()) + && (name == null || name.isEmpty()) + && (version == null || version.isEmpty()); } /** @@ -221,7 +221,8 @@ public boolean isValidFamily(String theFamily) { // in case they are checking !family theFamily = StringUtils.stripStart(theFamily, "!"); - return (StringUtils.isEmpty(theFamily) || Os.getValidFamilies().contains(theFamily)); + return (theFamily == null || theFamily.isEmpty()) + || Os.getValidFamilies().contains(theFamily); } /** @@ -271,16 +272,16 @@ public void setDisplay(boolean display) { public String getCacheId() { // return the hashcodes of all the parameters StringBuilder b = new StringBuilder(); - if (StringUtils.isNotEmpty(version)) { + if (version != null && !version.isEmpty()) { b.append(version.hashCode()); } - if (StringUtils.isNotEmpty(name)) { + if (name != null && !name.isEmpty()) { b.append(name.hashCode()); } - if (StringUtils.isNotEmpty(arch)) { + if (arch != null && !arch.isEmpty()) { b.append(arch.hashCode()); } - if (StringUtils.isNotEmpty(family)) { + if (family != null && !family.isEmpty()) { b.append(family.hashCode()); } return b.toString(); diff --git a/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/RequirePluginVersions.java b/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/RequirePluginVersions.java index 1c9db6e5..e18e7abc 100644 --- a/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/RequirePluginVersions.java +++ b/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/RequirePluginVersions.java @@ -289,7 +289,7 @@ private void handleMessagesToTheUser(MavenProject project, List failures newMsg.append(System.lineSeparator()); } String message = getMessage(); - if (StringUtils.isNotEmpty(message)) { + if (message != null && !message.isEmpty()) { newMsg.append(message); } @@ -347,7 +347,7 @@ Set removeUncheckedPlugins(Collection uncheckedPlugins, Set combineUncheckedPlugins( Collection uncheckedPlugins, String uncheckedPluginsList) { // if the comma list is empty, then there's nothing to do here. - if (StringUtils.isNotEmpty(uncheckedPluginsList)) { + if (uncheckedPluginsList != null && !uncheckedPluginsList.isEmpty()) { // make sure there is a collection to add to. if (uncheckedPlugins == null) { uncheckedPlugins = new HashSet<>(); @@ -489,7 +489,7 @@ private Set getBoundPlugins(MavenProject project, String thePhases) String[] lifecyclePhases = thePhases.split(","); for (int i = 0; i < lifecyclePhases.length; i++) { String lifecyclePhase = lifecyclePhases[i]; - if (StringUtils.isNotEmpty(lifecyclePhase)) { + if (lifecyclePhase != null && !lifecyclePhase.isEmpty()) { try { Lifecycle lifecycle = getLifecycleForPhase(lifecyclePhase); getLog().debug("getBoundPlugins(): " + project.getId() + " " + lifecyclePhase + " " @@ -561,7 +561,7 @@ public boolean hasValidVersionSpecified(Plugin source, List plugi } private boolean isValidVersion(String version) { - return StringUtils.isNotEmpty(version) && !StringUtils.isWhitespace(version); + return (version != null && !version.isEmpty()) && !StringUtils.isWhitespace(version); } private boolean isMatchingPlugin(Plugin source, PluginWrapper plugin) { @@ -610,7 +610,7 @@ private Set getAllPlugins(MavenProject project, Lifecycle lifecycle) getLog().debug(" lifecycleMapping = " + entry.getKey()); String pluginsForLifecycle = (String) entry.getValue(); getLog().debug(" plugins = " + pluginsForLifecycle); - if (StringUtils.isNotEmpty(pluginsForLifecycle)) { + if (pluginsForLifecycle != null && !pluginsForLifecycle.isEmpty()) { String pluginList[] = pluginsForLifecycle.split(","); for (String plugin : pluginList) { plugin = StringUtils.strip(plugin); diff --git a/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/version/AbstractVersionEnforcer.java b/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/version/AbstractVersionEnforcer.java index 946a58de..80ffbb4f 100644 --- a/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/version/AbstractVersionEnforcer.java +++ b/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/version/AbstractVersionEnforcer.java @@ -23,7 +23,6 @@ import org.apache.maven.artifact.versioning.VersionRange; import org.apache.maven.enforcer.rule.api.EnforcerRuleException; import org.apache.maven.enforcer.rules.AbstractStandardEnforcerRule; -import org.codehaus.plexus.util.StringUtils; import static org.apache.maven.enforcer.rules.utils.ArtifactMatcher.containsVersion; @@ -62,7 +61,7 @@ public void enforceVersion(String variableName, String requiredVersionRange, Art throws EnforcerRuleException // CHECKSTYLE_ON: LineLength { - if (StringUtils.isEmpty(requiredVersionRange)) { + if (requiredVersionRange == null || requiredVersionRange.isEmpty()) { throw new EnforcerRuleException(variableName + " version can't be empty."); } else { @@ -81,7 +80,7 @@ public void enforceVersion(String variableName, String requiredVersionRange, Art } else { String message = getMessage(); - if (StringUtils.isEmpty(message)) { + if (message == null || message.isEmpty()) { message = msg + " is not in the allowed range " + toString(vr) + "."; } @@ -106,7 +105,7 @@ protected static String toString(VersionRange vr) { @Override public String getCacheId() { - if (StringUtils.isNotEmpty(version)) { + if (version != null && !version.isEmpty()) { // return the hashcodes of the parameter that matters return "" + version.hashCode(); } else { diff --git a/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/version/RequireJavaVersion.java b/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/version/RequireJavaVersion.java index 1a37c497..da14b2c6 100644 --- a/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/version/RequireJavaVersion.java +++ b/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/version/RequireJavaVersion.java @@ -119,7 +119,7 @@ public static String normalizeJDKVersion(String theJdkVersion) { String section = iter.next(); section = section.replaceAll("[^0-9]", ""); - if (StringUtils.isNotEmpty(section)) { + if (section != null && !section.isEmpty()) { buffer.append(Integer.parseInt(section)); if (i != 2) {