Skip to content

Commit fd11604

Browse files
authored
Revert "Match upstream Maven recipe changes to ChangeDependency (#151)" (#152)
This reverts commit fa0b361.
1 parent fa0b361 commit fd11604

2 files changed

Lines changed: 25 additions & 52 deletions

File tree

src/main/java/org/openrewrite/java/dependencies/ChangeDependency.java

Lines changed: 19 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,13 @@
1818
import lombok.AllArgsConstructor;
1919
import lombok.EqualsAndHashCode;
2020
import lombok.Getter;
21-
import lombok.Value;
2221
import org.jspecify.annotations.Nullable;
2322
import org.openrewrite.*;
24-
import org.openrewrite.maven.ChangeDependencyGroupIdAndArtifactId;
2523

2624
@AllArgsConstructor
2725
@EqualsAndHashCode(callSuper = false)
2826
@Getter
29-
public class ChangeDependency extends ScanningRecipe<ChangeDependency.Accumulator> {
27+
public class ChangeDependency extends Recipe {
3028
// Gradle and Maven shared parameters
3129
@Option(displayName = "Old group ID",
3230
description = "The old group ID to replace. The group ID is the first part of a dependency coordinate 'com.google.guava:guava:VERSION'. Supports glob expressions.",
@@ -92,30 +90,25 @@ public String getDescription() {
9290
@Override
9391
public Validated<Object> validate(ExecutionContext ctx) {
9492
return super.validate(ctx)
95-
.and(getChangeMavenDependency().validate())
96-
.and(getChangeGradleDependency().validate());
97-
}
98-
99-
@Value
100-
public static class Accumulator {
101-
ChangeDependencyGroupIdAndArtifactId.Accumulator mavenAccumulator;
102-
}
103-
104-
@Override
105-
public Accumulator getInitialValue(ExecutionContext ctx) {
106-
return new Accumulator(getChangeMavenDependency().getInitialValue(ctx));
107-
}
108-
109-
@Override
110-
public TreeVisitor<?, ExecutionContext> getScanner(Accumulator acc) {
111-
return getChangeMavenDependency().getScanner(acc.getMavenAccumulator());
93+
.and(((Recipe) new org.openrewrite.gradle.ChangeDependency(
94+
oldGroupId, oldArtifactId, newGroupId, newArtifactId, newVersion, versionPattern, overrideManagedVersion, changeManagedDependency)).validate())
95+
.and(((Recipe) new org.openrewrite.maven.ChangeDependencyGroupIdAndArtifactId(
96+
oldGroupId, oldArtifactId, newGroupId, newArtifactId, newVersion, versionPattern, overrideManagedVersion, changeManagedDependency)).validate());
11297
}
11398

11499
@Override
115-
public TreeVisitor<?, ExecutionContext> getVisitor(Accumulator acc) {
100+
public TreeVisitor<?, ExecutionContext> getVisitor() {
116101
return new TreeVisitor<Tree, ExecutionContext>() {
117-
final TreeVisitor<?, ExecutionContext> mavenVisitor = getChangeMavenDependency().getVisitor(acc.getMavenAccumulator());
118-
final TreeVisitor<?, ExecutionContext> gradleVisitor = getChangeGradleDependency().getVisitor();
102+
final TreeVisitor<?, ExecutionContext> mavenVisitor = new org.openrewrite.maven.ChangeDependencyGroupIdAndArtifactId(
103+
oldGroupId, oldArtifactId,
104+
newGroupId, newArtifactId,
105+
newVersion, versionPattern,
106+
overrideManagedVersion, changeManagedDependency).getVisitor();
107+
final TreeVisitor<?, ExecutionContext> gradleVisitor = new org.openrewrite.gradle.ChangeDependency(
108+
oldGroupId, oldArtifactId,
109+
newGroupId, newArtifactId,
110+
newVersion, versionPattern,
111+
overrideManagedVersion).getVisitor();
119112

120113
@Override
121114
public boolean isAcceptable(SourceFile sourceFile, ExecutionContext ctx) {
@@ -128,29 +121,13 @@ public boolean isAcceptable(SourceFile sourceFile, ExecutionContext ctx) {
128121
return tree;
129122
}
130123
SourceFile s = (SourceFile) tree;
131-
if (mavenVisitor.isAcceptable(s, ctx)) {
132-
s = (SourceFile) mavenVisitor.visitNonNull(s, ctx);
133-
} else if (gradleVisitor.isAcceptable(s, ctx)) {
124+
if (gradleVisitor.isAcceptable(s, ctx)) {
134125
s = (SourceFile) gradleVisitor.visitNonNull(s, ctx);
126+
} else if (mavenVisitor.isAcceptable(s, ctx)) {
127+
s = (SourceFile) mavenVisitor.visitNonNull(s, ctx);
135128
}
136129
return s;
137130
}
138131
};
139132
}
140-
141-
private org.openrewrite.gradle.ChangeDependency getChangeGradleDependency() {
142-
return new org.openrewrite.gradle.ChangeDependency(
143-
oldGroupId, oldArtifactId,
144-
newGroupId, newArtifactId,
145-
newVersion, versionPattern,
146-
overrideManagedVersion, changeManagedDependency);
147-
}
148-
149-
private ChangeDependencyGroupIdAndArtifactId getChangeMavenDependency() {
150-
return new ChangeDependencyGroupIdAndArtifactId(
151-
oldGroupId, oldArtifactId,
152-
newGroupId, newArtifactId,
153-
newVersion, versionPattern,
154-
overrideManagedVersion, changeManagedDependency);
155-
}
156133
}

src/main/java/org/openrewrite/java/dependencies/RelocatedDependencyCheck.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,9 @@ private <T extends Tree> T maybeUpdate(T tree, String groupId, @Nullable String
221221
if (Boolean.TRUE.equals(changeDependencies) && artifactId != null) {
222222
String newGroupId = relocation.getTo().getGroupId();
223223
String newArtifactId = Optional.ofNullable(relocation.getTo().getArtifactId()).orElse(artifactId);
224-
doAfterVisit(changeDependency(this, groupId, artifactId, newGroupId, newArtifactId, ctx));
224+
doAfterVisit(new ChangeDependency(
225+
groupId, artifactId, newGroupId, newArtifactId,
226+
"latest.release", null, null, null).getVisitor());
225227
} else {
226228
return getSearchResultFound(tree, relocation);
227229
}
@@ -263,7 +265,9 @@ private <T extends Tree> T maybeUpdate(T tree, String groupId, @Nullable String
263265
if (Boolean.TRUE.equals(changeDependencies) && artifactId != null) {
264266
String newGroupId = relocation.getTo().getGroupId();
265267
String newArtifactId = Optional.ofNullable(relocation.getTo().getArtifactId()).orElse(artifactId);
266-
doAfterVisit(changeDependency(this, groupId, artifactId, newGroupId, newArtifactId, ctx));
268+
doAfterVisit(new ChangeDependency(
269+
groupId, artifactId, newGroupId, newArtifactId,
270+
"latest.release", null, null, null).getVisitor());
267271
} else {
268272
return getSearchResultFound(tree, relocation);
269273
}
@@ -300,12 +304,4 @@ private <T extends Tree> T getSearchResultFound(T tree, Relocation relocation) {
300304
}
301305
};
302306
}
303-
304-
private static TreeVisitor<?, ExecutionContext> changeDependency(TreeVisitor<?, ExecutionContext> visitor, String oldGroupId, String oldArtifactId, String newGroupId, String newArtifactId, ExecutionContext ctx) {
305-
//call scanner only for the current file as we need the recipe to behave like a non-scanning recipe for now. Later we can change this to support parent poms etc...
306-
ChangeDependency changeDependency = new ChangeDependency(oldGroupId, oldArtifactId, newGroupId, newArtifactId, "latest.release", null, null, null);
307-
ChangeDependency.Accumulator acc = changeDependency.getInitialValue(ctx);
308-
changeDependency.getScanner(acc).visit(visitor.getCursor().firstEnclosingOrThrow(SourceFile.class), ctx);
309-
return changeDependency.getVisitor(acc);
310-
}
311307
}

0 commit comments

Comments
 (0)