Skip to content

Commit 2d22596

Browse files
committed
Fix JavaParser.dependenciesFromResources to always return resources even when there are already matching entries in ~/.rewrite/classpath.
This fixes a scenario where the function could return different values based on the pre-existing contents of that directory. Before this change dependenciesFromResources() preferred a jar that is already extracted into ~/.rewrite/classpath over classpath resources. But since it's a prefix match the same prefix might match a different jar in ~/.rewrite/classpath than it does in the jar. For example a jar contains "snakeyaml-2.2.jar" and ~/.rewrite/classpath contains "snakeyaml-1.3.jar" the dependenciesFromResources() when called with the query "snakeyaml" will use 1.3, even though the author of the recipe/module packed in 2.2 and expects that to be used
1 parent ddc560f commit 2d22596

File tree

3 files changed

+28
-33
lines changed

3 files changed

+28
-33
lines changed

rewrite-java-test/src/test/java/org/openrewrite/java/JavaParserTest.java

+13-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@
2929
import org.openrewrite.test.RewriteTest;
3030

3131
import java.io.IOException;
32+
import java.nio.file.Files;
3233
import java.nio.file.Path;
34+
import java.util.List;
3335
import java.util.stream.Stream;
3436

3537
import static org.assertj.core.api.Assertions.assertThat;
@@ -47,7 +49,7 @@ void incompleteAssignment() {
4749
"""
4850
@Deprecated(since=)
4951
public class A {}
50-
"""
52+
"""
5153
)
5254
);
5355
}
@@ -88,10 +90,18 @@ public class PersistenceManagerImpl {
8890
}
8991

9092
@Test
91-
void dependenciesFromResources(@TempDir Path temp) {
93+
void dependenciesFromResources(@TempDir Path temp) throws Exception {
9294
JavaParserExecutionContextView ctx = JavaParserExecutionContextView.view(new InMemoryExecutionContext());
9395
ctx.setParserClasspathDownloadTarget(temp.toFile());
94-
assertThat(JavaParser.dependenciesFromResources(ctx, "guava-31.0-jre")).isNotEmpty();
96+
// Put a decoy file in the target directory to ensure that it is not used
97+
Files.write(temp.resolve("guava-30.0-jre.jar"), "decoy for test purposes; not a real jar".getBytes());
98+
List<Path> classpath = JavaParser.dependenciesFromResources(ctx, "guava");
99+
assertThat(classpath)
100+
.singleElement()
101+
.matches(Files::exists, "File extracted from classpath resources exists on disk")
102+
.matches(path -> path.endsWith("guava-31.0-jre.jar"),
103+
"classpathFromResources should return guava-31.0-jre.jar from resources, even when the target " +
104+
"directory contains guava-30.0-jre.jar which has the same prefix");
95105
}
96106

97107
@Test

rewrite-java/src/main/java/org/openrewrite/java/JavaParser.java

+4-19
Original file line numberDiff line numberDiff line change
@@ -109,30 +109,15 @@ static List<Path> dependenciesFromClasspath(String... artifactNames) {
109109
}
110110

111111
static List<Path> dependenciesFromResources(ExecutionContext ctx, String... artifactNamesWithVersions) {
112+
if(artifactNamesWithVersions.length == 0) {
113+
return Collections.emptyList();
114+
}
112115
List<Path> artifacts = new ArrayList<>(artifactNamesWithVersions.length);
113116
Set<String> missingArtifactNames = new LinkedHashSet<>(artifactNamesWithVersions.length);
117+
missingArtifactNames.addAll(Arrays.asList(artifactNamesWithVersions));
114118
File resourceTarget = JavaParserExecutionContextView.view(ctx)
115119
.getParserClasspathDownloadTarget();
116120

117-
nextArtifact:
118-
for (String artifactName : artifactNamesWithVersions) {
119-
Pattern jarPattern = Pattern.compile("[/\\\\]" + artifactName + "-?.*\\.jar$");
120-
File[] extracted = resourceTarget.listFiles();
121-
if (extracted != null) {
122-
for (File file : extracted) {
123-
if (jarPattern.matcher(file.getPath()).find()) {
124-
artifacts.add(file.toPath());
125-
continue nextArtifact;
126-
}
127-
}
128-
}
129-
missingArtifactNames.add(artifactName);
130-
}
131-
132-
if (missingArtifactNames.isEmpty()) {
133-
return artifacts;
134-
}
135-
136121
Class<?> caller;
137122
try {
138123
// StackWalker is only available in Java 15+, but right now we only use classloader isolated

rewrite-maven/src/test/java/org/openrewrite/maven/MavenParserTest.java

+11-11
Original file line numberDiff line numberDiff line change
@@ -1130,17 +1130,17 @@ void indirectBomImportedFromParent() {
11301130
"""
11311131
<project>
11321132
<modelVersion>4.0.0</modelVersion>
1133-
1133+
11341134
<artifactId>b</artifactId>
11351135
<groupId>org.openrewrite.maven</groupId>
11361136
<version>0.1.0-SNAPSHOT</version>
11371137
<packaging>pom</packaging>
1138-
1138+
11391139
<properties>
11401140
<maven.compiler.source>1.8</maven.compiler.source>
11411141
<maven.compiler.target>1.8</maven.compiler.target>
11421142
</properties>
1143-
1143+
11441144
<dependencyManagement>
11451145
<dependencies>
11461146
<dependency>
@@ -1161,12 +1161,12 @@ void indirectBomImportedFromParent() {
11611161
"""
11621162
<project>
11631163
<modelVersion>4.0.0</modelVersion>
1164-
1164+
11651165
<artifactId>c</artifactId>
11661166
<groupId>org.openrewrite.maven</groupId>
11671167
<version>0.1.0-SNAPSHOT</version>
11681168
<packaging>pom</packaging>
1169-
1169+
11701170
<dependencyManagement>
11711171
<dependencies>
11721172
<dependency>
@@ -1176,7 +1176,7 @@ void indirectBomImportedFromParent() {
11761176
</dependency>
11771177
</dependencies>
11781178
</dependencyManagement>
1179-
</project>
1179+
</pro ject>
11801180
"""
11811181
)
11821182
),
@@ -1185,11 +1185,11 @@ void indirectBomImportedFromParent() {
11851185
"""
11861186
<project>
11871187
<modelVersion>4.0.0</modelVersion>
1188-
1188+
11891189
<groupId>org.openrewrite.maven</groupId>
11901190
<artifactId>d</artifactId>
11911191
<version>0.1.0-SNAPSHOT</version>
1192-
1192+
11931193
<properties>
11941194
<maven.compiler.source>1.8</maven.compiler.source>
11951195
<maven.compiler.target>1.8</maven.compiler.target>
@@ -1532,7 +1532,7 @@ void managedDependencyInTransitiveAndPom() {
15321532
<artifactId>a</artifactId>
15331533
<version>1.0.0</version>
15341534
<packaging>jar</packaging>
1535-
1535+
15361536
<dependencyManagement>
15371537
<dependencies>
15381538
<dependency>
@@ -1606,7 +1606,7 @@ void profileNoJdkActivation() {
16061606
<groupId>com.mycompany.app</groupId>
16071607
<artifactId>my-app</artifactId>
16081608
<version>1</version>
1609-
1609+
16101610
<profiles>
16111611
<profile>
16121612
<id>old-jdk</id>
@@ -2474,7 +2474,7 @@ void pluginManagement() {
24742474
<groupId>org.openrewrite.maven</groupId>
24752475
<artifactId>a</artifactId>
24762476
<version>0.1.0-SNAPSHOT</version>
2477-
2477+
24782478
<build>
24792479
<pluginManagement>
24802480
<plugins>

0 commit comments

Comments
 (0)