Skip to content

Commit c229022

Browse files
committed
Remove lingering references to JavaSourceSetUpdater from dependency recipes.
Preserve JavaSourceSetUpdater itself for now to minimize ClassNotFound errors during transition.
1 parent ce8a026 commit c229022

8 files changed

Lines changed: 5 additions & 709 deletions

File tree

rewrite-gradle/src/main/java/org/openrewrite/gradle/ChangeDependency.java

Lines changed: 2 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,13 @@
3030
import org.openrewrite.internal.StringUtils;
3131
import org.openrewrite.java.JavaIsoVisitor;
3232
import org.openrewrite.java.JavaVisitor;
33-
import org.openrewrite.java.marker.JavaProject;
34-
import org.openrewrite.java.marker.JavaSourceSet;
3533
import org.openrewrite.java.tree.J;
3634
import org.openrewrite.java.tree.JavaSourceFile;
3735
import org.openrewrite.kotlin.tree.K;
3836
import org.openrewrite.marker.Markup;
3937
import org.openrewrite.maven.MavenDownloadingException;
4038
import org.openrewrite.maven.tree.*;
4139
import org.openrewrite.maven.table.MavenMetadataFailures;
42-
import org.openrewrite.maven.utilities.JavaSourceSetUpdater;
4340
import org.openrewrite.properties.PropertiesVisitor;
4441
import org.openrewrite.properties.tree.Properties;
4542
import org.openrewrite.semver.DependencyMatcher;
@@ -174,8 +171,6 @@ public static class Accumulator {
174171
Map<String, Object> versionVariableUpdates = new HashMap<>();
175172
Map<String, Set<GroupArtifact>> versionVariableUsages = new HashMap<>();
176173
Set<GroupArtifact> failedResolutions = new HashSet<>();
177-
Map<JavaProject, ResolvedDependency> modulesWithOldDependency = new HashMap<>();
178-
Map<JavaProject, List<MavenRepository>> moduleRepositories = new HashMap<>();
179174
}
180175

181176
@Override
@@ -202,21 +197,6 @@ public boolean isAcceptable(SourceFile sourceFile, ExecutionContext ctx) {
202197
if (gradleProject == null) {
203198
return (J) tree;
204199
}
205-
// Record this module if it has the old dependency
206-
Optional<JavaProject> maybeJp = tree.getMarkers().findFirst(JavaProject.class);
207-
if (maybeJp.isPresent() && !acc.modulesWithOldDependency.containsKey(maybeJp.get())) {
208-
outer:
209-
for (GradleDependencyConfiguration config : gradleProject.getConfigurations()) {
210-
for (ResolvedDependency resolved : config.getDirectResolvedShallow()) {
211-
if (StringUtils.matchesGlob(resolved.getGroupId(), oldGroupId) &&
212-
StringUtils.matchesGlob(resolved.getArtifactId(), oldArtifactId)) {
213-
acc.modulesWithOldDependency.put(maybeJp.get(), resolved);
214-
acc.moduleRepositories.put(maybeJp.get(), gradleProject.getMavenRepositories());
215-
break outer;
216-
}
217-
}
218-
}
219-
}
220200
}
221201
return super.visit(tree, ctx);
222202
}
@@ -500,23 +480,14 @@ private GradleProject updateGradleModel(GradleProject gp, ExecutionContext ctx)
500480
});
501481

502482
DependencyMatcher propsMatcher = requireNonNull(DependencyMatcher.build(oldGroupId + ":" + oldArtifactId).getValue());
503-
boolean hasModulesWithOldDep = !acc.modulesWithOldDependency.isEmpty();
504483
return new TreeVisitor<Tree, ExecutionContext>() {
505-
@Nullable
506-
private JavaSourceSetUpdater updater;
507-
private final Map<String, JavaSourceSet> updatedSourceSets = new HashMap<>();
508-
509484
@Override
510485
public boolean isAcceptable(SourceFile sourceFile, ExecutionContext ctx) {
511486
if (sourceFile instanceof Properties.File) {
512487
return sourceFile.getSourcePath().endsWith(GRADLE_PROPERTIES_FILE_NAME);
513488
}
514-
if ((sourceFile instanceof G.CompilationUnit || sourceFile instanceof K.CompilationUnit)
515-
&& sourceFile.getMarkers().findFirst(GradleProject.class).isPresent()) {
516-
return true;
517-
}
518-
// Accept Java source files for JavaSourceSet updates
519-
return hasModulesWithOldDep && sourceFile instanceof JavaSourceFile;
489+
return (sourceFile instanceof G.CompilationUnit || sourceFile instanceof K.CompilationUnit)
490+
&& sourceFile.getMarkers().findFirst(GradleProject.class).isPresent();
520491
}
521492

522493
@Override
@@ -546,52 +517,8 @@ public Properties visitEntry(Properties.Entry entry, ExecutionContext ctx) {
546517
}
547518
return tree;
548519
}
549-
// Update JavaSourceSet marker on source files that have one
550-
if (hasModulesWithOldDep && tree instanceof SourceFile &&
551-
((SourceFile) tree).getMarkers().findFirst(JavaSourceSet.class).isPresent()) {
552-
return updateJavaSourceSet((SourceFile) tree, ctx);
553-
}
554520
return gradleVisitor.visit(tree, ctx);
555521
}
556-
557-
private SourceFile updateJavaSourceSet(SourceFile sf, ExecutionContext ctx) {
558-
Optional<JavaProject> maybeJp = sf.getMarkers().findFirst(JavaProject.class);
559-
if (!maybeJp.isPresent()) {
560-
return sf;
561-
}
562-
ResolvedDependency oldDep = acc.modulesWithOldDependency.get(maybeJp.get());
563-
if (oldDep == null) {
564-
return sf;
565-
}
566-
if (updater == null) {
567-
updater = new JavaSourceSetUpdater(ctx);
568-
}
569-
JavaProject jp = maybeJp.get();
570-
return JavaSourceSet.updateOnSourceFile(sf, updatedSourceSets, sourceSet -> {
571-
String effectiveNewGroupId = newGroupId != null ? newGroupId : oldDep.getGroupId();
572-
String effectiveNewArtifactId = newArtifactId != null ? newArtifactId : oldDep.getArtifactId();
573-
ResolvedGroupArtifactVersion newGav = new ResolvedGroupArtifactVersion(
574-
oldDep.getGav().getRepository(),
575-
effectiveNewGroupId, effectiveNewArtifactId, oldDep.getVersion(), null);
576-
ResolvedDependency newDep = oldDep
577-
.withGav(newGav)
578-
.withRepository(findRemoteRepository(jp));
579-
return updater.changeDependency(sourceSet, oldDep, newDep);
580-
});
581-
}
582-
583-
private MavenRepository findRemoteRepository(JavaProject jp) {
584-
List<MavenRepository> repos = acc.moduleRepositories.get(jp);
585-
if (repos != null) {
586-
for (MavenRepository repo : repos) {
587-
String uri = repo.getUri();
588-
if (uri != null && (uri.startsWith("http://") || uri.startsWith("https://"))) {
589-
return repo;
590-
}
591-
}
592-
}
593-
return MavenRepository.MAVEN_CENTRAL;
594-
}
595522
};
596523
}
597524

rewrite-gradle/src/main/java/org/openrewrite/gradle/UpgradeDependencyVersion.java

Lines changed: 1 addition & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,10 @@
3333
import org.openrewrite.java.JavaIsoVisitor;
3434
import org.openrewrite.java.JavaVisitor;
3535
import org.openrewrite.java.MethodMatcher;
36-
import org.openrewrite.java.marker.JavaProject;
37-
import org.openrewrite.java.marker.JavaSourceSet;
3836
import org.openrewrite.java.tree.Expression;
3937
import org.openrewrite.java.tree.J;
4038
import org.openrewrite.java.tree.JavaSourceFile;
4139
import org.openrewrite.kotlin.tree.K;
42-
import org.openrewrite.maven.utilities.JavaSourceSetUpdater;
4340
import org.openrewrite.marker.Markup;
4441
import org.openrewrite.maven.MavenDownloadingException;
4542
import org.openrewrite.maven.internal.MavenPomDownloader;
@@ -134,8 +131,6 @@ public static class DependencyVersionState {
134131
Map<GroupArtifact, @Nullable Object> gaToNewVersion = new HashMap<>();
135132

136133
Map<String, Map<GroupArtifact, Set<String>>> configurationPerGAPerModule = new HashMap<>();
137-
138-
Map<JavaProject, Set<ResolvedDependency>> modulesWithDependency = new HashMap<>();
139134
}
140135

141136
@Override
@@ -161,24 +156,6 @@ public boolean isAcceptable(SourceFile sourceFile, ExecutionContext ctx) {
161156
public @Nullable J visit(@Nullable Tree tree, ExecutionContext ctx) {
162157
if (tree instanceof JavaSourceFile) {
163158
gradleProject = tree.getMarkers().findFirst(GradleProject.class).orElse(null);
164-
// Record modules with matching resolved dependencies for JavaSourceSet updates
165-
if (gradleProject != null) {
166-
Optional<JavaProject> maybeJp = tree.getMarkers().findFirst(JavaProject.class);
167-
if (maybeJp.isPresent() && !acc.modulesWithDependency.containsKey(maybeJp.get())) {
168-
DependencyMatcher depMatcher = new DependencyMatcher(groupId, artifactId, null);
169-
Set<ResolvedDependency> matched = new HashSet<>();
170-
for (GradleDependencyConfiguration config : gradleProject.getConfigurations()) {
171-
for (ResolvedDependency resolved : config.getDirectResolvedShallow()) {
172-
if (depMatcher.matches(resolved.getGroupId(), resolved.getArtifactId())) {
173-
matched.add(resolved);
174-
}
175-
}
176-
}
177-
if (!matched.isEmpty()) {
178-
acc.modulesWithDependency.put(maybeJp.get(), matched);
179-
}
180-
}
181-
}
182159
}
183160
return super.visit(tree, ctx);
184161
}
@@ -311,21 +288,13 @@ private void gatherVariables(GradleDependency gradleDependency) {
311288

312289
@Override
313290
public TreeVisitor<?, ExecutionContext> getVisitor(DependencyVersionState acc) {
314-
boolean hasModulesWithDep = !acc.modulesWithDependency.isEmpty();
315291
return new TreeVisitor<Tree, ExecutionContext>() {
316292
private final UpdateGradle updateGradle = new UpdateGradle(acc);
317293
private final UpdateProperties updateProperties = new UpdateProperties(acc);
318-
@Nullable
319-
private JavaSourceSetUpdater jssUpdater;
320-
private final Map<String, JavaSourceSet> updatedSourceSets = new HashMap<>();
321294

322295
@Override
323296
public boolean isAcceptable(SourceFile sf, ExecutionContext ctx) {
324-
if (updateProperties.isAcceptable(sf, ctx) || updateGradle.isAcceptable(sf, ctx)) {
325-
return true;
326-
}
327-
return hasModulesWithDep && sf instanceof JavaSourceFile
328-
&& !(sf instanceof G.CompilationUnit) && !(sf instanceof K.CompilationUnit);
297+
return updateProperties.isAcceptable(sf, ctx) || updateGradle.isAcceptable(sf, ctx);
329298
}
330299

331300
@Override
@@ -334,11 +303,6 @@ public boolean isAcceptable(SourceFile sf, ExecutionContext ctx) {
334303
Tree t = tree;
335304
if (t instanceof SourceFile) {
336305
SourceFile sf = (SourceFile) t;
337-
// Handle regular Java source files for JavaSourceSet updates
338-
if (hasModulesWithDep && sf instanceof JavaSourceFile
339-
&& !(sf instanceof G.CompilationUnit) && !(sf instanceof K.CompilationUnit)) {
340-
return updateJavaSourceSet(sf, ctx);
341-
}
342306
if (updateProperties.isAcceptable(sf, ctx)) {
343307
t = updateProperties.visitNonNull(t, ctx);
344308
} else if (updateGradle.isAcceptable(sf, ctx)) {
@@ -395,41 +359,6 @@ public boolean isAcceptable(SourceFile sf, ExecutionContext ctx) {
395359
return t;
396360
}
397361

398-
private SourceFile updateJavaSourceSet(SourceFile sf, ExecutionContext ctx) {
399-
Optional<JavaProject> maybeJp = sf.getMarkers().findFirst(JavaProject.class);
400-
if (!maybeJp.isPresent()) {
401-
return sf;
402-
}
403-
Set<ResolvedDependency> oldDeps = acc.modulesWithDependency.get(maybeJp.get());
404-
if (oldDeps == null || oldDeps.isEmpty()) {
405-
return sf;
406-
}
407-
return JavaSourceSet.updateOnSourceFile(sf, updatedSourceSets, sourceSet -> {
408-
if (sourceSet.getGavToTypes().isEmpty()) {
409-
return sourceSet;
410-
}
411-
if (jssUpdater == null) {
412-
jssUpdater = new JavaSourceSetUpdater(ctx);
413-
}
414-
JavaSourceSet result = sourceSet;
415-
for (ResolvedDependency oldDep : oldDeps) {
416-
GroupArtifact ga = new GroupArtifact(oldDep.getGroupId(), oldDep.getArtifactId());
417-
Object newVersionObj = acc.gaToNewVersion.get(ga);
418-
if (!(newVersionObj instanceof String)) {
419-
continue;
420-
}
421-
String resolvedNewVersion = (String) newVersionObj;
422-
ResolvedGroupArtifactVersion newGav = new ResolvedGroupArtifactVersion(
423-
oldDep.getGav().getRepository(),
424-
oldDep.getGroupId(), oldDep.getArtifactId(), resolvedNewVersion, null);
425-
ResolvedDependency newDep = oldDep
426-
.withGav(newGav)
427-
.withRepository(MavenRepository.MAVEN_CENTRAL);
428-
result = jssUpdater.changeDependency(result, oldDep, newDep);
429-
}
430-
return result;
431-
});
432-
}
433362
};
434363
}
435364

0 commit comments

Comments
 (0)