Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update get classpath by artifact name to support bazel #4750

Merged
merged 3 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -214,4 +216,22 @@ class A {
)
);
}

@Test
void filterArtifacts() {
List<URI> 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"));
}
}
54 changes: 35 additions & 19 deletions rewrite-java/src/main/java/org/openrewrite/java/JavaParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,26 +74,11 @@ static List<Path> dependenciesFromClasspath(String... artifactNames) {
List<Path> artifacts = new ArrayList<>(artifactNames.length);
List<String> 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<Path> matchedArtifacts = filterArtifacts(artifactName, runtimeClasspath);
if (matchedArtifacts.isEmpty()) {
missingArtifactNames.add(artifactName);
} else {
artifacts.addAll(matchedArtifacts);
}
}

Expand All @@ -107,6 +92,37 @@ static List<Path> 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<Path> filterArtifacts(String artifactName, List<URI> runtimeClasspath) {
List<Path> artifacts = new ArrayList<>();
// Bazel automatically replaces '-' with '_' when generating jar files.
String normalizedArtifactName = artifactName.replace('-', '_');
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) {
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<Path> dependenciesFromResources(ExecutionContext ctx, String... artifactNamesWithVersions) {
if (artifactNamesWithVersions.length == 0) {
return Collections.emptyList();
Expand Down