From 2f39dc3a3a588d3446b5612a535fe8f4a4b24131 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Wed, 10 May 2023 11:29:11 +0000 Subject: [PATCH] [MNG-6829] Replace any StringUtils#isEmpty(String) and #isNotEmpty(String) 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 --- .../maven/plugins/help/AbstractHelpMojo.java | 3 +- .../maven/plugins/help/DescribeMojo.java | 40 +++++++++---------- .../maven/plugins/help/EffectivePomMojo.java | 2 +- .../plugins/help/EffectiveSettingsMojo.java | 2 +- .../maven/plugins/help/EvaluateMojo.java | 8 ++-- 5 files changed, 27 insertions(+), 28 deletions(-) diff --git a/src/main/java/org/apache/maven/plugins/help/AbstractHelpMojo.java b/src/main/java/org/apache/maven/plugins/help/AbstractHelpMojo.java index d9512f1e..0439b836 100644 --- a/src/main/java/org/apache/maven/plugins/help/AbstractHelpMojo.java +++ b/src/main/java/org/apache/maven/plugins/help/AbstractHelpMojo.java @@ -33,7 +33,6 @@ import org.apache.maven.project.MavenProject; import org.apache.maven.project.ProjectBuilder; import org.apache.maven.project.ProjectBuildingRequest; -import org.codehaus.plexus.util.StringUtils; import org.codehaus.plexus.util.WriterFactory; import org.eclipse.aether.RepositoryException; import org.eclipse.aether.RepositorySystem; @@ -129,7 +128,7 @@ protected static void writeFile(File output, String content) throws IOException */ protected org.eclipse.aether.artifact.Artifact getAetherArtifact(String artifactString, String type) throws MojoExecutionException { - if (StringUtils.isEmpty(artifactString)) { + if (artifactString == null || artifactString.isEmpty()) { throw new IllegalArgumentException("artifact parameter could not be empty"); } diff --git a/src/main/java/org/apache/maven/plugins/help/DescribeMojo.java b/src/main/java/org/apache/maven/plugins/help/DescribeMojo.java index 24fd3a11..7cf0ec8d 100644 --- a/src/main/java/org/apache/maven/plugins/help/DescribeMojo.java +++ b/src/main/java/org/apache/maven/plugins/help/DescribeMojo.java @@ -213,14 +213,14 @@ public void execute() throws MojoExecutionException, MojoFailureException { StringBuilder descriptionBuffer = new StringBuilder(); boolean describePlugin = true; - if (StringUtils.isNotEmpty(cmd)) { + if (cmd != null && !cmd.isEmpty()) { describePlugin = describeCommand(descriptionBuffer); } if (describePlugin) { PluginInfo pi = parsePluginLookupInfo(); PluginDescriptor descriptor = lookupPluginDescriptor(pi); - if (StringUtils.isNotEmpty(goal)) { + if (goal != null && !goal.isEmpty()) { MojoDescriptor mojo = descriptor.getMojo(goal); if (mojo == null) { throw new MojoFailureException( @@ -331,7 +331,7 @@ private PluginDescriptor lookupPluginDescriptor(PluginInfo pi) throws MojoExecut */ private PluginInfo parsePluginLookupInfo() throws MojoFailureException { PluginInfo pi = new PluginInfo(); - if (StringUtils.isNotEmpty(plugin)) { + if (plugin != null && !plugin.isEmpty()) { if (plugin.indexOf(':') > -1) { String[] pluginParts = plugin.split(":"); @@ -479,7 +479,7 @@ private void describeMojoGuts(MojoDescriptor md, StringBuilder buffer, boolean f deprecation = NO_REASON; } - if (StringUtils.isNotEmpty(deprecation)) { + if (deprecation != null && !deprecation.isEmpty()) { append( buffer, MessageUtils.buffer().warning("Deprecated. " + deprecation).toString(), @@ -498,7 +498,7 @@ private void describeMojoGuts(MojoDescriptor md, StringBuilder buffer, boolean f append(buffer, "Language", md.getLanguage(), 1); String phase = md.getPhase(); - if (StringUtils.isNotEmpty(phase)) { + if (phase != null && !phase.isEmpty()) { append(buffer, "Bound to phase", phase, 1); } @@ -506,17 +506,17 @@ private void describeMojoGuts(MojoDescriptor md, StringBuilder buffer, boolean f String eLife = md.getExecuteLifecycle(); String ePhase = md.getExecutePhase(); - if (StringUtils.isNotEmpty(eGoal) || StringUtils.isNotEmpty(ePhase)) { + if ((eGoal != null && !eGoal.isEmpty()) || (ePhase != null && !ePhase.isEmpty())) { append(buffer, "Before this goal executes, it will call:", 1); - if (StringUtils.isNotEmpty(eGoal)) { + if (eGoal != null && !eGoal.isEmpty()) { append(buffer, "Single goal", "'" + eGoal + "'", 2); } - if (StringUtils.isNotEmpty(ePhase)) { + if (ePhase != null && !ePhase.isEmpty()) { String s = "Phase: '" + ePhase + "'"; - if (StringUtils.isNotEmpty(eLife)) { + if (eLife != null && !eLife.isEmpty()) { s += " in Lifecycle Overlay: '" + eLife + "'"; } @@ -568,7 +568,7 @@ private void describeMojoParameters(MojoDescriptor md, StringBuilder buffer) md.getMojoConfiguration().getChild(parameter.getName()).getAttribute("default-value", null); } - if (StringUtils.isNotEmpty(defaultVal)) { + if (defaultVal != null && !defaultVal.isEmpty()) { defaultVal = " (Default: " + MessageUtils.buffer().strong(defaultVal) + ")"; } else { defaultVal = ""; @@ -576,7 +576,7 @@ private void describeMojoParameters(MojoDescriptor md, StringBuilder buffer) append(buffer, MessageUtils.buffer().strong(parameter.getName()) + defaultVal, 2); String alias = parameter.getAlias(); - if (!StringUtils.isEmpty(alias)) { + if (!(alias == null || alias.isEmpty())) { append(buffer, "Alias", alias, 3); } @@ -585,13 +585,13 @@ private void describeMojoParameters(MojoDescriptor md, StringBuilder buffer) } String expression = parameter.getExpression(); - if (StringUtils.isEmpty(expression)) { + if (expression == null || expression.isEmpty()) { // expression is ALWAYS null, this is a bug in PluginDescriptorBuilder (cf. MNG-4941). // Fixed with Maven-3.0.1 expression = md.getMojoConfiguration().getChild(parameter.getName()).getValue(null); } - if (StringUtils.isNotEmpty(expression)) { + if (expression != null && !expression.isEmpty()) { Matcher matcher = EXPRESSION.matcher(expression); if (matcher.matches()) { append(buffer, "User property", matcher.group(1), 3); @@ -607,7 +607,7 @@ private void describeMojoParameters(MojoDescriptor md, StringBuilder buffer) deprecation = NO_REASON; } - if (StringUtils.isNotEmpty(deprecation)) { + if (deprecation != null && !deprecation.isEmpty()) { append( buffer, MessageUtils.buffer() @@ -663,7 +663,7 @@ private boolean describeCommand(StringBuilder descriptionBuffer) throws MojoExec for (String key : phases) { descriptionBuffer.append("* ").append(key).append(": "); String value = defaultLifecyclePhases.get(key); - if (StringUtils.isNotEmpty(value)) { + if (value != null && !value.isEmpty()) { for (StringTokenizer tok = new StringTokenizer(value, ","); tok.hasMoreTokens(); ) { descriptionBuffer.append(tok.nextToken().trim()); @@ -774,7 +774,7 @@ private static List toLines(String text, int indent, int indentSize, int */ private static void append(StringBuilder sb, String description, int indent) throws MojoFailureException, MojoExecutionException { - if (StringUtils.isEmpty(description)) { + if (description == null || description.isEmpty()) { sb.append(UNKNOWN).append(LS); return; } @@ -798,11 +798,11 @@ private static void append(StringBuilder sb, String description, int indent) */ private static void append(StringBuilder sb, String key, String value, int indent) throws MojoFailureException, MojoExecutionException { - if (StringUtils.isEmpty(key)) { + if (key == null || key.isEmpty()) { throw new IllegalArgumentException("Key is required!"); } - if (StringUtils.isEmpty(value)) { + if (value == null || value.isEmpty()) { value = UNKNOWN; } @@ -827,7 +827,7 @@ private static void append(StringBuilder sb, String key, String value, int inden */ private static void appendAsParagraph(StringBuilder sb, String key, String value, int indent) throws MojoFailureException, MojoExecutionException { - if (StringUtils.isEmpty(value)) { + if (value == null || value.isEmpty()) { value = UNKNOWN; } @@ -891,7 +891,7 @@ private boolean isReportGoal(MojoDescriptor md) { * @return The effective description string, never null. */ private static String toDescription(String description) { - if (StringUtils.isNotEmpty(description)) { + if (description != null && !description.isEmpty()) { return new HtmlToPlainTextConverter().convert(description); } diff --git a/src/main/java/org/apache/maven/plugins/help/EffectivePomMojo.java b/src/main/java/org/apache/maven/plugins/help/EffectivePomMojo.java index 921da5d5..4e421487 100644 --- a/src/main/java/org/apache/maven/plugins/help/EffectivePomMojo.java +++ b/src/main/java/org/apache/maven/plugins/help/EffectivePomMojo.java @@ -91,7 +91,7 @@ public class EffectivePomMojo extends AbstractEffectiveMojo { /** {@inheritDoc} */ public void execute() throws MojoExecutionException { - if (StringUtils.isNotEmpty(artifact)) { + if (artifact != null && !artifact.isEmpty()) { project = getMavenProject(artifact); projects = Collections.singletonList(project); } diff --git a/src/main/java/org/apache/maven/plugins/help/EffectiveSettingsMojo.java b/src/main/java/org/apache/maven/plugins/help/EffectiveSettingsMojo.java index 532d445d..cf237086 100644 --- a/src/main/java/org/apache/maven/plugins/help/EffectiveSettingsMojo.java +++ b/src/main/java/org/apache/maven/plugins/help/EffectiveSettingsMojo.java @@ -245,7 +245,7 @@ private static String getHostName() { */ private static String getUserName() { String userName = System.getProperty("user.name"); - if (StringUtils.isEmpty(userName)) { + if (userName == null || userName.isEmpty()) { return "unknown"; } diff --git a/src/main/java/org/apache/maven/plugins/help/EvaluateMojo.java b/src/main/java/org/apache/maven/plugins/help/EvaluateMojo.java index 89a5d41c..aa2f2502 100644 --- a/src/main/java/org/apache/maven/plugins/help/EvaluateMojo.java +++ b/src/main/java/org/apache/maven/plugins/help/EvaluateMojo.java @@ -161,7 +161,7 @@ public void execute() throws MojoExecutionException, MojoFailureException { validateParameters(); - if (StringUtils.isNotEmpty(artifact)) { + if (artifact != null && !artifact.isEmpty()) { project = getMavenProject(artifact); } @@ -453,8 +453,8 @@ private void addAlias(XStream xstreamObject, File jarFile, String packageFilter) private File getArtifactFile(String artifactId) throws MojoExecutionException, RepositoryException { List dependencies = getHelpPluginPom().getDependencies(); for (Dependency dependency : dependencies) { - if (("org.apache.maven".equals(dependency.getGroupId()))) { - if ((artifactId.equals(dependency.getArtifactId()))) { + if ("org.apache.maven".equals(dependency.getGroupId())) { + if (artifactId.equals(dependency.getArtifactId())) { Artifact mavenArtifact = new DefaultArtifact( dependency.getGroupId(), dependency.getArtifactId(), "jar", dependency.getVersion()); @@ -498,7 +498,7 @@ private MavenProject getHelpPluginPom() throws MojoExecutionException { * @return the plural of the name */ private static String pluralize(String name) { - if (StringUtils.isEmpty(name)) { + if (name == null || name.isEmpty()) { throw new IllegalArgumentException("name is required"); }