From d4a237d3ac4cc6a1356538785774242dd8261387 Mon Sep 17 00:00:00 2001 From: Anshuman Mishra Date: Thu, 5 Dec 2024 11:55:52 -0800 Subject: [PATCH 1/2] Update get classpath by artifact name to support bazel --- .../org/openrewrite/java/JavaParserTest.java | 20 +++++++ .../java/org/openrewrite/java/JavaParser.java | 53 ++++++++++++------- 2 files changed, 54 insertions(+), 19 deletions(-) diff --git a/rewrite-java-test/src/test/java/org/openrewrite/java/JavaParserTest.java b/rewrite-java-test/src/test/java/org/openrewrite/java/JavaParserTest.java index 03c6a38ee89..ccfe243aec1 100644 --- a/rewrite-java-test/src/test/java/org/openrewrite/java/JavaParserTest.java +++ b/rewrite-java-test/src/test/java/org/openrewrite/java/JavaParserTest.java @@ -29,8 +29,10 @@ import org.openrewrite.test.RewriteTest; import java.io.IOException; +import java.net.URI; import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.Paths; import java.util.List; import java.util.stream.Stream; @@ -214,4 +216,22 @@ class A { ) ); } + + @Test + void filterArtifacts() { + List classpath = List.of( + URI.create("file:/.m2/repository/com/google/guava/guava-24.1.1/com_google_guava_guava-24.1.1.jar"), + URI.create("file:/.m2/repository/org/threeten/threeten-extra-1.5.0/org_threeten_threeten_extra-1.5.0.jar"), + URI.create("file:/.m2/repository/com/amazonaws/aws-java-sdk-s3-1.11.546/com_amazonaws_aws_java_sdk_s3-1.11.546.jar"), + URI.create("file:/.m2/repository/org/openrewrite/rewrite-java/8.41.1/rewrite-java-8.41.1.jar") + ); + assertThat(JavaParser.filterArtifacts("threeten-extra", classpath)) + .containsOnly(Paths.get("/.m2/repository/org/threeten/threeten-extra-1.5.0/org_threeten_threeten_extra-1.5.0.jar")); + assertThat(JavaParser.filterArtifacts("guava", classpath)) + .containsOnly(Paths.get("/.m2/repository/com/google/guava/guava-24.1.1/com_google_guava_guava-24.1.1.jar")); + assertThat(JavaParser.filterArtifacts("aws-java-sdk-s3", classpath)) + .containsOnly(Paths.get("/.m2/repository/com/amazonaws/aws-java-sdk-s3-1.11.546/com_amazonaws_aws_java_sdk_s3-1.11.546.jar")); + assertThat(JavaParser.filterArtifacts("rewrite-java", classpath)) + .containsOnly(Paths.get("/.m2/repository/org/openrewrite/rewrite-java/8.41.1/rewrite-java-8.41.1.jar")); + } } diff --git a/rewrite-java/src/main/java/org/openrewrite/java/JavaParser.java b/rewrite-java/src/main/java/org/openrewrite/java/JavaParser.java index 98913cb04a7..15747bdf4a8 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/JavaParser.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/JavaParser.java @@ -74,27 +74,11 @@ static List dependenciesFromClasspath(String... artifactNames) { List artifacts = new ArrayList<>(artifactNames.length); List missingArtifactNames = new ArrayList<>(artifactNames.length); for (String artifactName : artifactNames) { - Pattern jarPattern = Pattern.compile(artifactName + "(?:" + "-.*?" + ")?" + "\\.jar$"); - // In a multi-project IDE classpath, some classpath entries aren't jars - Pattern explodedPattern = Pattern.compile("/" + artifactName + "/"); - boolean lacking = true; - for (URI cpEntry : runtimeClasspath) { - if (!"file".equals(cpEntry.getScheme())) { - // exclude any `jar` entries which could result from `Bundle-ClassPath` in `MANIFEST.MF` - continue; - } - String cpEntryString = cpEntry.toString(); - Path path = Paths.get(cpEntry); - if (jarPattern.matcher(cpEntryString).find() || - explodedPattern.matcher(cpEntryString).find() && path.toFile().isDirectory()) { - artifacts.add(path); - lacking = false; - // Do not break because jarPattern matches "foo-bar-1.0.jar" and "foo-1.0.jar" to "foo" - } - } - if (lacking) { + List matchedArtifacts = filterArtifacts(artifactName, runtimeClasspath); + if (matchedArtifacts.isEmpty()) { missingArtifactNames.add(artifactName); } + artifacts.addAll(matchedArtifacts); } if (!missingArtifactNames.isEmpty()) { @@ -107,6 +91,37 @@ static List dependenciesFromClasspath(String... artifactNames) { return artifacts; } + /** + * Filters the classpath entries to find paths that match the given artifact name. + * + * @param artifactName The artifact name to search for. + * @param runtimeClasspath The list of classpath URIs to search within. + * @return List of Paths that match the artifact name. + */ + // VisibleForTesting + static List filterArtifacts(String artifactName, List runtimeClasspath) { + List artifacts = new ArrayList<>(); + // Bazel automatically replaces '-' with '_' when generating jar files. + String normalizedArtifactName = artifactName.replace('-', '_'); + Pattern jarPattern = Pattern.compile("(" + artifactName + "|" + normalizedArtifactName + ")" + "(?:" + "-.*?" + ")?" + "\\.jar$"); + // In a multi-project IDE classpath, some classpath entries aren't jars + Pattern explodedPattern = Pattern.compile("/" + artifactName + "/"); + for (URI cpEntry : runtimeClasspath) { + if (!"file".equals(cpEntry.getScheme())) { + // exclude any `jar` entries which could result from `Bundle-ClassPath` in `MANIFEST.MF` + continue; + } + String cpEntryString = cpEntry.toString(); + Path path = Paths.get(cpEntry); + if (jarPattern.matcher(cpEntryString).find() || + explodedPattern.matcher(cpEntryString).find() && path.toFile().isDirectory()) { + artifacts.add(path); + // Do not break because jarPattern matches "foo-bar-1.0.jar" and "foo-1.0.jar" to "foo" + } + } + return artifacts; + } + static List dependenciesFromResources(ExecutionContext ctx, String... artifactNamesWithVersions) { if (artifactNamesWithVersions.length == 0) { return Collections.emptyList(); From fc81296a47aa251b151c2b2ec05c50147b149f14 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Fri, 6 Dec 2024 12:07:37 +0100 Subject: [PATCH 2/2] Remove string concatenation and add `else` for clarity --- .../src/main/java/org/openrewrite/java/JavaParser.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/rewrite-java/src/main/java/org/openrewrite/java/JavaParser.java b/rewrite-java/src/main/java/org/openrewrite/java/JavaParser.java index 15747bdf4a8..066da57e0fb 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/JavaParser.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/JavaParser.java @@ -77,8 +77,9 @@ static List dependenciesFromClasspath(String... artifactNames) { List matchedArtifacts = filterArtifacts(artifactName, runtimeClasspath); if (matchedArtifacts.isEmpty()) { missingArtifactNames.add(artifactName); + } else { + artifacts.addAll(matchedArtifacts); } - artifacts.addAll(matchedArtifacts); } if (!missingArtifactNames.isEmpty()) { @@ -103,7 +104,7 @@ static List filterArtifacts(String artifactName, List runtimeClasspat List artifacts = new ArrayList<>(); // Bazel automatically replaces '-' with '_' when generating jar files. String normalizedArtifactName = artifactName.replace('-', '_'); - Pattern jarPattern = Pattern.compile("(" + artifactName + "|" + normalizedArtifactName + ")" + "(?:" + "-.*?" + ")?" + "\\.jar$"); + Pattern jarPattern = Pattern.compile(String.format("(%s|%s)(?:-.*?)?\\.jar$", artifactName, normalizedArtifactName)); // In a multi-project IDE classpath, some classpath entries aren't jars Pattern explodedPattern = Pattern.compile("/" + artifactName + "/"); for (URI cpEntry : runtimeClasspath) {