Skip to content

Commit

Permalink
refactor: OpenRewrite recipe best practices (#3800)
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek and TeamModerne authored Dec 9, 2023
1 parent 07685db commit d0d73e6
Show file tree
Hide file tree
Showing 98 changed files with 239 additions and 239 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
return new TreeVisitor<Tree, ExecutionContext>() {
@Nullable
@Override
public Tree visit(@Nullable Tree tree, ExecutionContext executionContext) {
public Tree visit(@Nullable Tree tree, ExecutionContext ctx) {
if (tree instanceof SourceFile) {
SourceFile sourceFile = (SourceFile) tree;
Path sourcePath = sourceFile.getSourcePath();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public Accumulator getInitialValue(ExecutionContext ctx) {
public TreeVisitor<?, ExecutionContext> getScanner(Accumulator acc) {
return new TreeVisitor<Tree, ExecutionContext>() {
@Override
public Tree visit(@Nullable Tree tree, ExecutionContext executionContext) {
public Tree visit(@Nullable Tree tree, ExecutionContext ctx) {
assert tree instanceof SourceFile;
Path p = ((SourceFile) tree).getSourcePath();
if(acc.getSourcePaths().contains(p)) {
Expand All @@ -76,11 +76,11 @@ public Collection<? extends SourceFile> generate(Accumulator acc, ExecutionConte
public TreeVisitor<?, ExecutionContext> getVisitor(Accumulator acc) {
return new TreeVisitor<Tree, ExecutionContext>() {
@Override
public @Nullable Tree visit(@Nullable Tree tree, ExecutionContext executionContext) {
public @Nullable Tree visit(@Nullable Tree tree, ExecutionContext ctx) {
if(tree instanceof SourceFile) {
Path p = ((SourceFile) tree).getSourcePath();
if(acc.getDuplicates().contains(p)) {
collidingSourceFiles.insertRow(executionContext, new CollidingSourceFiles.Row(
collidingSourceFiles.insertRow(ctx, new CollidingSourceFiles.Row(
p.toString(),
tree.getClass().toString()
));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public Set<GitProvenance> getInitialValue(ExecutionContext ctx) {
public TreeVisitor<?, ExecutionContext> getScanner(Set<GitProvenance> provenances) {
return new TreeVisitor<Tree, ExecutionContext>() {
@Override
public Tree visit(@Nullable Tree tree, ExecutionContext executionContext) {
public Tree visit(@Nullable Tree tree, ExecutionContext ctx) {
SourceFile sourceFile = (SourceFile) requireNonNull(tree);
sourceFile.getMarkers().findFirst(GitProvenance.class).ifPresent(provenance ->
provenances.add(provenance.withId(DONT_CONSIDER_ID_IN_HASH_CODE)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public String getDescription() {
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new TreeVisitor<Tree, ExecutionContext>() {
@Override
public @Nullable Tree visit(@Nullable Tree tree, ExecutionContext executionContext) {
public @Nullable Tree visit(@Nullable Tree tree, ExecutionContext ctx) {
if (tree == null) {
return null;
}
Expand Down
4 changes: 2 additions & 2 deletions rewrite-core/src/main/java/org/openrewrite/RenameFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
return new TreeVisitor<Tree, ExecutionContext>() {
@Nullable
@Override
public Tree visit(@Nullable Tree tree, ExecutionContext executionContext) {
public Tree visit(@Nullable Tree tree, ExecutionContext ctx) {
if (tree instanceof SourceFile) {
SourceFile sourceFile = (SourceFile) tree;
Path sourcePath = sourceFile.getSourcePath();
Expand All @@ -59,7 +59,7 @@ public Tree visit(@Nullable Tree tree, ExecutionContext executionContext) {
return ((SourceFile) tree).withSourcePath(sourcePath.resolveSibling(fileName).normalize());
}
}
return super.visit(tree, executionContext);
return super.visit(tree, ctx);
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
return new TreeVisitor<Tree, ExecutionContext>() {
@Nullable
@Override
public Tree visit(@Nullable Tree tree, ExecutionContext executionContext) {
public Tree visit(@Nullable Tree tree, ExecutionContext ctx) {
if (tree instanceof SourceFile) {
SourceFile sourceFile = (SourceFile) tree;
Path sourcePath = sourceFile.getSourcePath();
Expand All @@ -72,7 +72,7 @@ public Tree visit(@Nullable Tree tree, ExecutionContext executionContext) {
return sourceFile;
}
}
return super.visit(tree, executionContext);
return super.visit(tree, ctx);
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public String getDescription() {
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new TreeVisitor<Tree, ExecutionContext>() {
@Override
public Tree visit(@Nullable Tree tree, ExecutionContext executionContext) {
public Tree visit(@Nullable Tree tree, ExecutionContext ctx) {
if (tree instanceof SourceFile) {
for (BuildMetadata buildMetadata : tree.getMarkers().findAll(BuildMetadata.class)) {
if (buildMetadata.getMetadata().containsKey(key)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor(AtomicBoolean created) {
Path path = Paths.get(relativeFileName);
return new TreeVisitor<SourceFile, ExecutionContext>() {
@Override
public SourceFile visit(@Nullable Tree tree, ExecutionContext executionContext) {
public SourceFile visit(@Nullable Tree tree, ExecutionContext ctx) {
SourceFile sourceFile = (SourceFile) requireNonNull(tree);
if ((created.get() || Boolean.TRUE.equals(overwriteExisting)) && path.equals(sourceFile.getSourcePath())) {
if (sourceFile instanceof PlainText) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ void checkApplicabilityAgainstOtherSourceTypes() {
Recipe recipe(TreeVisitor<?, ExecutionContext> applicability) {
return toRecipe(() -> Preconditions.check(applicability, new PlainTextVisitor<>() {
@Override
public PlainText visitText(PlainText text, ExecutionContext executionContext) {
public PlainText visitText(PlainText text, ExecutionContext ctx) {
return text.withText("goodbye");
}
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ void panic() {
rewriteRun(
spec -> spec.recipe(toRecipe(() -> new TreeVisitor<>() {
@Override
public Tree visit(@Nullable Tree tree, ExecutionContext executionContext) {
public Tree visit(@Nullable Tree tree, ExecutionContext ctx) {
fail("Should never have reached a visit method");
return tree;
}
Expand Down Expand Up @@ -116,7 +116,7 @@ void deleteFileByReturningNullFromVisit() {
rewriteRun(
spec -> spec.recipe(toRecipe(() -> new PlainTextVisitor<>() {
@Override
public @Nullable PlainText visit(@Nullable Tree tree, ExecutionContext executionContext) {
public @Nullable PlainText visit(@Nullable Tree tree, ExecutionContext ctx) {
return null;
}
})),
Expand Down Expand Up @@ -262,11 +262,11 @@ void accurateReportingOfRecipesMakingChanges() {
private Recipe testRecipe(@Language("markdown") String name) {
return toRecipe(() -> new PlainTextVisitor<>() {
@Override
public PlainText visitText(PlainText text, ExecutionContext executionContext) {
public PlainText visitText(PlainText text, ExecutionContext ctx) {
if (!text.getText().contains(name)) {
return text.withText(name + text.getText());
}
return super.visitText(text, executionContext);
return super.visitText(text, ctx);
}
}).withName(name);
}
Expand Down Expand Up @@ -421,11 +421,11 @@ public String getDescription() {
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new PlainTextVisitor<>() {
@Override
public PlainText visitText(PlainText text, ExecutionContext executionContext) {
public PlainText visitText(PlainText text, ExecutionContext ctx) {
if (!text.getText().contains(getDisplayName())) {
return text.withText(getDisplayName() + text.getText());
}
return super.visitText(text, executionContext);
return super.visitText(text, ctx);
}
};
}
Expand All @@ -448,11 +448,11 @@ public String getDescription() {
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new PlainTextVisitor<>() {
@Override
public PlainText visitText(PlainText text, ExecutionContext executionContext) {
public PlainText visitText(PlainText text, ExecutionContext ctx) {
if (!text.getText().contains(getDisplayName())) {
return text.withText(getDisplayName() + text.getText());
}
return super.visitText(text, executionContext);
return super.visitText(text, ctx);
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public String getDescription() {
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new PlainTextVisitor<>() {
@Override
public PlainText visitText(PlainText text, ExecutionContext executionContext) {
public PlainText visitText(PlainText text, ExecutionContext ctx) {
throw new BoomException();
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void precondition() {
dr.addPrecondition(
toRecipe(() -> new PlainTextVisitor<>() {
@Override
public PlainText visitText(PlainText text, ExecutionContext executionContext) {
public PlainText visitText(PlainText text, ExecutionContext ctx) {
if("1".equals(text.getText())) {
return SearchResult.found(text);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(
new PlainTextVisitor<>() {
@Override
public PlainText visitText(PlainText text, ExecutionContext executionContext) {
public PlainText visitText(PlainText text, ExecutionContext ctx) {
if (!"sam".equals(text.getText())) {
return SearchResult.found(text);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
final MethodMatcher dependencyDsl = new MethodMatcher("DependencyHandlerSpec *(..)");

@Override
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext context) {
J.MethodInvocation m = (J.MethodInvocation) super.visitMethodInvocation(method, context);
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation m = (J.MethodInvocation) super.visitMethodInvocation(method, ctx);
if (!dependencyDsl.matches(m) || !(StringUtils.isBlank(configuration) || m.getSimpleName().equals(configuration))) {
return m;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
final MethodMatcher dependencyDsl = new MethodMatcher("DependencyHandlerSpec *(..)");

@Override
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext context) {
J.MethodInvocation m = (J.MethodInvocation) super.visitMethodInvocation(method, context);
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation m = (J.MethodInvocation) super.visitMethodInvocation(method, ctx);
if (!dependencyDsl.matches(m) || !(StringUtils.isBlank(configuration) || m.getSimpleName().equals(configuration))) {
return m;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
final MethodMatcher dependencyDsl = new MethodMatcher("DependencyHandlerSpec *(..)");

@Override
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext context) {
J.MethodInvocation m = (J.MethodInvocation) super.visitMethodInvocation(method, context);
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation m = (J.MethodInvocation) super.visitMethodInvocation(method, ctx);
if (!dependencyDsl.matches(m) || !(StringUtils.isBlank(configuration) || m.getSimpleName().equals(configuration))) {
return m;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
final MethodMatcher dependencyDsl = new MethodMatcher("DependencyHandlerSpec *(..)");

@Override
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext context) {
J.MethodInvocation m = (J.MethodInvocation) super.visitMethodInvocation(method, context);
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation m = (J.MethodInvocation) super.visitMethodInvocation(method, ctx);
if (!dependencyDsl.matches(m) || !(StringUtils.isBlank(configuration) || m.getSimpleName().equals(configuration))) {
return m;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
final MethodMatcher dependencyDsl = new MethodMatcher("DependencyHandlerSpec *(..)");

@Override
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext context) {
J.MethodInvocation m = (J.MethodInvocation) super.visitMethodInvocation(method, context);
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation m = (J.MethodInvocation) super.visitMethodInvocation(method, ctx);
if (!dependencyDsl.matches(m) || !(StringUtils.isBlank(configuration) || m.getSimpleName().equals(configuration))) {
return m;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public String getDescription() {
public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(new FindGradleProject(FindGradleProject.SearchCriteria.File).getVisitor(), new GroovyIsoVisitor<ExecutionContext>() {
@Override
public J.Assignment visitAssignment(J.Assignment as, ExecutionContext executionContext) {
public J.Assignment visitAssignment(J.Assignment as, ExecutionContext ctx) {
if(!(as.getAssignment() instanceof J.Literal)) {
return as;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
final MethodMatcher dependencyDsl = new MethodMatcher("DependencyHandlerSpec *(..)");
return Preconditions.check(new IsBuildGradle<>(), new GroovyVisitor<ExecutionContext>() {
@Override
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext context) {
J.MethodInvocation m = (J.MethodInvocation) super.visitMethodInvocation(method, context);
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation m = (J.MethodInvocation) super.visitMethodInvocation(method, ctx);
if (!dependencyDsl.matches(m)) {
return m;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
final MethodMatcher dependencyDsl = new MethodMatcher("DependencyHandlerSpec *(..)");
return Preconditions.check(new IsBuildGradle<>(), new GroovyVisitor<ExecutionContext>() {
@Override
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext context) {
J.MethodInvocation m = (J.MethodInvocation) super.visitMethodInvocation(method, context);
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation m = (J.MethodInvocation) super.visitMethodInvocation(method, ctx);
if (!dependencyDsl.matches(m)) {
return m;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ public Properties visitEntry(Properties.Entry entry, ExecutionContext ctx) {
},
new TreeVisitor<Tree, ExecutionContext>() {
@Override
public boolean isAcceptable(SourceFile sourceFile, ExecutionContext executionContext) {
if (!super.isAcceptable(sourceFile, executionContext)) {
public boolean isAcceptable(SourceFile sourceFile, ExecutionContext ctx) {
if (!super.isAcceptable(sourceFile, ctx)) {
return false;
}

Expand Down Expand Up @@ -403,8 +403,8 @@ public WrapperPropertiesVisitor(GradleWrapper gradleWrapper) {
}

@Override
public Properties visitFile(Properties.File file, ExecutionContext executionContext) {
Properties p = super.visitFile(file, executionContext);
public Properties visitFile(Properties.File file, ExecutionContext ctx) {
Properties p = super.visitFile(file, ctx);
Set<Properties.Entry> checksumKey = FindProperties.find(p, DISTRIBUTION_SHA_256_SUM_KEY, false);
if (checksumKey.isEmpty()) {
Properties.Value propertyValue = new Properties.Value(Tree.randomId(), "", Markers.EMPTY, gradleWrapper.getDistributionChecksum().getHexValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
final MethodMatcher javaVersionToVersionMatcher = new MethodMatcher("org.gradle.api.JavaVersion toVersion(..)");

@Override
public J visitAssignment(J.Assignment assignment, ExecutionContext executionContext) {
J.Assignment a = (J.Assignment) super.visitAssignment(assignment, executionContext);
public J visitAssignment(J.Assignment assignment, ExecutionContext ctx) {
J.Assignment a = (J.Assignment) super.visitAssignment(assignment, ctx);

if (a.getVariable() instanceof J.Identifier) {
J.Identifier variable = (J.Identifier) a.getVariable();
Expand Down
Loading

0 comments on commit d0d73e6

Please sign in to comment.