From 3cebbf7a151f6063fd149d838a0dccd8a8464e76 Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Fri, 17 Nov 2023 16:50:51 +0100 Subject: [PATCH] Remove usage of old StringTokenizer (#1306) --- .../versioning/DefaultArtifactVersion.java | 24 +++++++-------- ...DefaultLifecycleTaskSegmentCalculator.java | 11 ++++--- .../internal/MojoDescriptorCreator.java | 29 +++++++++---------- 3 files changed, 30 insertions(+), 34 deletions(-) diff --git a/maven-artifact/src/main/java/org/apache/maven/artifact/versioning/DefaultArtifactVersion.java b/maven-artifact/src/main/java/org/apache/maven/artifact/versioning/DefaultArtifactVersion.java index bdb0ee8233..039b383aab 100644 --- a/maven-artifact/src/main/java/org/apache/maven/artifact/versioning/DefaultArtifactVersion.java +++ b/maven-artifact/src/main/java/org/apache/maven/artifact/versioning/DefaultArtifactVersion.java @@ -18,8 +18,6 @@ */ package org.apache.maven.artifact.versioning; -import java.util.StringTokenizer; - /** * Default implementation of artifact versioning. * @@ -123,29 +121,30 @@ public class DefaultArtifactVersion implements ArtifactVersion { } else { boolean fallback = false; - StringTokenizer tok = new StringTokenizer(part1, "."); - if (tok.hasMoreTokens()) { - majorVersion = getNextIntegerToken(tok); + String[] tok = part1.split("\\."); + int idx = 0; + if (idx < tok.length) { + majorVersion = getNextIntegerToken(tok[idx++]); if (majorVersion == null) { fallback = true; } } else { fallback = true; } - if (tok.hasMoreTokens()) { - minorVersion = getNextIntegerToken(tok); + if (idx < tok.length) { + minorVersion = getNextIntegerToken(tok[idx++]); if (minorVersion == null) { fallback = true; } } - if (tok.hasMoreTokens()) { - incrementalVersion = getNextIntegerToken(tok); + if (idx < tok.length) { + incrementalVersion = getNextIntegerToken(tok[idx++]); if (incrementalVersion == null) { fallback = true; } } - if (tok.hasMoreTokens()) { - qualifier = tok.nextToken(); + if (idx < tok.length) { + qualifier = tok[idx++]; fallback = isDigits(qualifier); } @@ -178,8 +177,7 @@ public class DefaultArtifactVersion implements ArtifactVersion { return true; } - private static Integer getNextIntegerToken(StringTokenizer tok) { - String s = tok.nextToken(); + private static Integer getNextIntegerToken(String s) { if ((s.length() > 1) && s.startsWith("0")) { return null; } diff --git a/maven-core/src/main/java/org/apache/maven/lifecycle/internal/DefaultLifecycleTaskSegmentCalculator.java b/maven-core/src/main/java/org/apache/maven/lifecycle/internal/DefaultLifecycleTaskSegmentCalculator.java index 5a50ed18ad..78dc0b7728 100644 --- a/maven-core/src/main/java/org/apache/maven/lifecycle/internal/DefaultLifecycleTaskSegmentCalculator.java +++ b/maven-core/src/main/java/org/apache/maven/lifecycle/internal/DefaultLifecycleTaskSegmentCalculator.java @@ -24,7 +24,8 @@ import javax.inject.Singleton; import java.util.ArrayList; import java.util.List; -import java.util.StringTokenizer; +import java.util.stream.Collectors; +import java.util.stream.Stream; import org.apache.maven.execution.MavenSession; import org.apache.maven.lifecycle.LifecycleNotFoundException; @@ -73,11 +74,9 @@ public class DefaultLifecycleTaskSegmentCalculator implements LifecycleTaskSegme if ((tasks == null || tasks.isEmpty()) && (rootProject.getDefaultGoal() != null && !rootProject.getDefaultGoal().isEmpty())) { - StringTokenizer tokenizer = new StringTokenizer(rootProject.getDefaultGoal()); - tasks = new ArrayList<>(); - while (tokenizer.hasMoreTokens()) { - tasks.add(tokenizer.nextToken()); - } + tasks = Stream.of(rootProject.getDefaultGoal().split("\\s+")) + .filter(g -> !g.isEmpty()) + .collect(Collectors.toList()); } return calculateTaskSegments(session, tasks); diff --git a/maven-core/src/main/java/org/apache/maven/lifecycle/internal/MojoDescriptorCreator.java b/maven-core/src/main/java/org/apache/maven/lifecycle/internal/MojoDescriptorCreator.java index bea8c35894..ec0baabf2c 100644 --- a/maven-core/src/main/java/org/apache/maven/lifecycle/internal/MojoDescriptorCreator.java +++ b/maven-core/src/main/java/org/apache/maven/lifecycle/internal/MojoDescriptorCreator.java @@ -26,7 +26,6 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.List; -import java.util.StringTokenizer; import org.apache.maven.api.xml.XmlNode; import org.apache.maven.execution.MavenSession; @@ -127,9 +126,9 @@ public class MojoDescriptorCreator { Plugin plugin = null; - StringTokenizer tok = new StringTokenizer(task, ":"); + String[] tok = task.split(":"); - int numTokens = tok.countTokens(); + int numTokens = tok.length; if (numTokens >= 4) { // We have everything that we need @@ -142,19 +141,19 @@ public class MojoDescriptorCreator { // goal // plugin = new Plugin(); - plugin.setGroupId(tok.nextToken()); - plugin.setArtifactId(tok.nextToken()); - plugin.setVersion(tok.nextToken()); - goal = tok.nextToken(); + plugin.setGroupId(tok[0]); + plugin.setArtifactId(tok[1]); + plugin.setVersion(tok[2]); + goal = tok[3]; // This won't be valid, but it constructs something easy to read in the error message - while (tok.hasMoreTokens()) { - goal += ":" + tok.nextToken(); + for (int idx = 4; idx < tok.length; idx++) { + goal += ":" + tok[idx]; } } else if (numTokens == 3) { // groupId:artifactId:goal or pluginPrefix:version:goal (since Maven 3.9.0) - String firstToken = tok.nextToken(); + String firstToken = tok[0]; // groupId or pluginPrefix? heuristics: groupId contains dot (.) but not pluginPrefix if (firstToken.contains(".")) { // We have everything that we need except the version @@ -168,22 +167,22 @@ public class MojoDescriptorCreator { // plugin = new Plugin(); plugin.setGroupId(firstToken); - plugin.setArtifactId(tok.nextToken()); + plugin.setArtifactId(tok[1]); } else { // pluginPrefix:version:goal, like remote-resources:3.5.0:process plugin = findPluginForPrefix(firstToken, session); - plugin.setVersion(tok.nextToken()); + plugin.setVersion(tok[1]); } - goal = tok.nextToken(); + goal = tok[2]; } else { // We have a prefix and goal // // idea:idea // - String prefix = tok.nextToken(); + String prefix = tok[0]; if (numTokens == 2) { - goal = tok.nextToken(); + goal = tok[1]; } else { // goal was missing - pass through to MojoNotFoundException goal = "";