diff --git a/rewrite-core/src/main/java/org/openrewrite/config/DeclarativeRecipe.java b/rewrite-core/src/main/java/org/openrewrite/config/DeclarativeRecipe.java index 4bc8ded1db5..44704bf02f0 100644 --- a/rewrite-core/src/main/java/org/openrewrite/config/DeclarativeRecipe.java +++ b/rewrite-core/src/main/java/org/openrewrite/config/DeclarativeRecipe.java @@ -144,9 +144,6 @@ public String getDescription() { @NonFinal transient boolean preconditionApplicable; - @NonFinal - transient boolean preconditionEverApplicable; - @Override public TreeVisitor getVisitor() { return new TreeVisitor() { @@ -159,7 +156,6 @@ public boolean isAcceptable(SourceFile sourceFile, ExecutionContext ctx) { public @Nullable Tree visit(@Nullable Tree tree, ExecutionContext ctx) { Tree t = precondition.visit(tree, ctx); preconditionApplicable = t != tree; - preconditionEverApplicable |= preconditionApplicable; return tree; } }; @@ -201,7 +197,7 @@ public List getRecipeList() { @Value @EqualsAndHashCode(callSuper = false) - static class BellwetherDecoratedScanningRecipe extends ScanningRecipe { + static class BellwetherDecoratedScanningRecipe extends ScanningRecipe { DeclarativeRecipe.PreconditionBellwether bellwether; ScanningRecipe delegate; @@ -228,16 +224,12 @@ public T getInitialValue(ExecutionContext ctx) { @Override public TreeVisitor getScanner(T acc) { - return Preconditions.check(bellwether.getVisitor(), delegate.getScanner(acc)); + return delegate.getScanner(acc); } @Override public Collection generate(T acc, ExecutionContext ctx) { - try { - return bellwether.isPreconditionEverApplicable() ? delegate.generate(acc, ctx) : super.generate(acc, ctx); - } finally { - bellwether.preconditionEverApplicable = false; - } + return delegate.generate(acc, ctx); } @Override @@ -253,18 +245,18 @@ public List getRecipeList() { @Override public final List getRecipeList() { - if(preconditions.isEmpty()) { + if (preconditions.isEmpty()) { return recipeList; } TreeVisitor andPreconditions = null; for (Recipe precondition : preconditions) { - if(isScanningRecipe(precondition)) { + if (isScanningRecipe(precondition)) { throw new IllegalArgumentException( getName() + " declares the ScanningRecipe " + precondition.getName() + " as a precondition." + "ScanningRecipe cannot be used as Preconditions."); } - if(andPreconditions == null) { + if (andPreconditions == null) { andPreconditions = precondition.getVisitor(); } else { andPreconditions = Preconditions.and(andPreconditions, precondition.getVisitor()); @@ -278,11 +270,11 @@ public final List getRecipeList() { } private static boolean isScanningRecipe(Recipe recipe) { - if(recipe instanceof ScanningRecipe) { + if (recipe instanceof ScanningRecipe) { return true; } for (Recipe r : recipe.getRecipeList()) { - if(isScanningRecipe(r)) { + if (isScanningRecipe(r)) { return true; } } @@ -292,7 +284,7 @@ private static boolean isScanningRecipe(Recipe recipe) { private static List decorateWithPreconditionBellwether(PreconditionBellwether bellwether, List recipeList) { List mappedRecipeList = new ArrayList<>(recipeList.size()); for (Recipe recipe : recipeList) { - if(recipe instanceof ScanningRecipe) { + if (recipe instanceof ScanningRecipe) { mappedRecipeList.add(new BellwetherDecoratedScanningRecipe<>(bellwether, (ScanningRecipe) recipe)); } else { mappedRecipeList.add(new BellwetherDecoratedRecipe(bellwether, recipe)); @@ -324,8 +316,8 @@ public void addValidation(Validated validated) { @Override public Validated validate() { return Validated.test("initialization", - "initialize(..) must be called on DeclarativeRecipe prior to use.", - this, r -> initValidation != null) + "initialize(..) must be called on DeclarativeRecipe prior to use.", + this, r -> initValidation != null) .and(validation) .and(initValidation); } diff --git a/rewrite-core/src/test/java/org/openrewrite/config/DeclarativeRecipeTest.java b/rewrite-core/src/test/java/org/openrewrite/config/DeclarativeRecipeTest.java index d981f50423f..8aff75f0e5f 100644 --- a/rewrite-core/src/test/java/org/openrewrite/config/DeclarativeRecipeTest.java +++ b/rewrite-core/src/test/java/org/openrewrite/config/DeclarativeRecipeTest.java @@ -88,18 +88,37 @@ void yamlPrecondition() { toText: 2 - org.openrewrite.text.ChangeText: toText: 3 + """, "org.openrewrite.PreconditionTest"), + text("1", "3"), + text("2") + ); + } + + @Test + void yamlPreconditionWithScanningRecipe() { + rewriteRun( + spec -> spec.recipeFromYaml(""" + --- + type: specs.openrewrite.org/v1beta/recipe + name: org.openrewrite.PreconditionTest + preconditions: + - org.openrewrite.text.Find: + find: 1 + recipeList: - org.openrewrite.text.CreateTextFile: relativeFileName: test.txt fileContents: "test" """, "org.openrewrite.PreconditionTest") .afterRecipe(run -> { assertThat(run.getChangeset().getAllResults()).anySatisfy( - s -> assertThat(s.getAfter().getSourcePath()).isEqualTo(Paths.get("test.txt")) + s -> { + assertThat(s.getAfter()).isNotNull(); + assertThat(s.getAfter().getSourcePath()).isEqualTo(Paths.get("test.txt")); + } ); - System.out.println(run); - }), - text("1", "3"), - text("2") + }) + .expectedCyclesThatMakeChanges(1), + text("1") ); }