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 931d06254ff..b74cd479f67 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,7 +29,9 @@ import org.openrewrite.test.RewriteTest; import java.io.IOException; +import java.nio.file.Files; import java.nio.file.Path; +import java.util.List; import java.util.stream.Stream; import static org.assertj.core.api.Assertions.assertThat; @@ -47,7 +49,7 @@ void incompleteAssignment() { """ @Deprecated(since=) public class A {} - """ + """ ) ); } @@ -88,10 +90,18 @@ public class PersistenceManagerImpl { } @Test - void dependenciesFromResources(@TempDir Path temp) { + void dependenciesFromResources(@TempDir Path temp) throws Exception { JavaParserExecutionContextView ctx = JavaParserExecutionContextView.view(new InMemoryExecutionContext()); ctx.setParserClasspathDownloadTarget(temp.toFile()); - assertThat(JavaParser.dependenciesFromResources(ctx, "guava-31.0-jre")).isNotEmpty(); + // Put a decoy file in the target directory to ensure that it is not used + Files.write(temp.resolve("guava-30.0-jre.jar"), "decoy for test purposes; not a real jar".getBytes()); + List classpath = JavaParser.dependenciesFromResources(ctx, "guava"); + assertThat(classpath) + .singleElement() + .matches(Files::exists, "File extracted from classpath resources exists on disk") + .matches(path -> path.endsWith("guava-31.0-jre.jar"), + "classpathFromResources should return guava-31.0-jre.jar from resources, even when the target " + + "directory contains guava-30.0-jre.jar which has the same prefix"); } @Test 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 aa27110fc6b..e912c6b12b6 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/JavaParser.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/JavaParser.java @@ -109,30 +109,15 @@ static List dependenciesFromClasspath(String... artifactNames) { } static List dependenciesFromResources(ExecutionContext ctx, String... artifactNamesWithVersions) { + if(artifactNamesWithVersions.length == 0) { + return Collections.emptyList(); + } List artifacts = new ArrayList<>(artifactNamesWithVersions.length); Set missingArtifactNames = new LinkedHashSet<>(artifactNamesWithVersions.length); + missingArtifactNames.addAll(Arrays.asList(artifactNamesWithVersions)); File resourceTarget = JavaParserExecutionContextView.view(ctx) .getParserClasspathDownloadTarget(); - nextArtifact: - for (String artifactName : artifactNamesWithVersions) { - Pattern jarPattern = Pattern.compile("[/\\\\]" + artifactName + "-?.*\\.jar$"); - File[] extracted = resourceTarget.listFiles(); - if (extracted != null) { - for (File file : extracted) { - if (jarPattern.matcher(file.getPath()).find()) { - artifacts.add(file.toPath()); - continue nextArtifact; - } - } - } - missingArtifactNames.add(artifactName); - } - - if (missingArtifactNames.isEmpty()) { - return artifacts; - } - Class caller; try { // StackWalker is only available in Java 15+, but right now we only use classloader isolated diff --git a/rewrite-maven/src/test/java/org/openrewrite/maven/MavenParserTest.java b/rewrite-maven/src/test/java/org/openrewrite/maven/MavenParserTest.java index 0b65ed80e6d..1db19cdcc6a 100644 --- a/rewrite-maven/src/test/java/org/openrewrite/maven/MavenParserTest.java +++ b/rewrite-maven/src/test/java/org/openrewrite/maven/MavenParserTest.java @@ -1130,17 +1130,17 @@ void indirectBomImportedFromParent() { """ 4.0.0 - + b org.openrewrite.maven 0.1.0-SNAPSHOT pom - + 1.8 1.8 - + @@ -1161,12 +1161,12 @@ void indirectBomImportedFromParent() { """ 4.0.0 - + c org.openrewrite.maven 0.1.0-SNAPSHOT pom - + @@ -1176,7 +1176,7 @@ void indirectBomImportedFromParent() { - + """ ) ), @@ -1185,11 +1185,11 @@ void indirectBomImportedFromParent() { """ 4.0.0 - + org.openrewrite.maven d 0.1.0-SNAPSHOT - + 1.8 1.8 @@ -1532,7 +1532,7 @@ void managedDependencyInTransitiveAndPom() { a 1.0.0 jar - + @@ -1606,7 +1606,7 @@ void profileNoJdkActivation() { com.mycompany.app my-app 1 - + old-jdk @@ -2474,7 +2474,7 @@ void pluginManagement() { org.openrewrite.maven a 0.1.0-SNAPSHOT - +