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 e213360c10d..a4a8ca20568 100644 --- a/rewrite-core/src/main/java/org/openrewrite/config/DeclarativeRecipe.java +++ b/rewrite-core/src/main/java/org/openrewrite/config/DeclarativeRecipe.java @@ -70,13 +70,13 @@ public boolean causesAnotherCycle() { private final List preconditions = new ArrayList<>(); public void addPrecondition(Recipe recipe) { - preconditions.add(recipe); + uninitializedPreconditions.add(recipe); } @JsonIgnore - private Validated validation = Validated.test("initialization", - "initialize(..) must be called on DeclarativeRecipe prior to use.", - this, r -> uninitializedRecipes.isEmpty()); + private Validated validation = Validated.none(); + @JsonIgnore + private Validated initValidation = null; @Override public Duration getEstimatedEffortPerOccurrence() { @@ -85,11 +85,13 @@ public Duration getEstimatedEffortPerOccurrence() { } public void initialize(Collection availableRecipes, Map> recipeToContributors) { + initValidation = Validated.none(); initialize(uninitializedRecipes, recipeList, availableRecipes, recipeToContributors); initialize(uninitializedPreconditions, preconditions, availableRecipes, recipeToContributors); } private void initialize(List uninitialized, List initialized, Collection availableRecipes, Map> recipeToContributors) { + initialized.clear(); for (int i = 0; i < uninitialized.size(); i++) { Recipe recipe = uninitialized.get(i); if (recipe instanceof LazyLoadedRecipe) { @@ -103,7 +105,7 @@ private void initialize(List uninitialized, List initialized, Co } initialized.add(subRecipe); } else { - validation = validation.and( + initValidation = initValidation.and( invalid(name + ".recipeList" + "[" + i + "] (in " + source + ")", recipeFqn, @@ -118,7 +120,6 @@ private void initialize(List uninitialized, List initialized, Co initialized.add(recipe); } } - uninitialized.clear(); } @Value @@ -304,7 +305,11 @@ public void addValidation(Validated validated) { @Override public Validated validate() { - return validation; + return Validated.test("initialization", + "initialize(..) must be called on DeclarativeRecipe prior to use.", + this, r -> initValidation != null) + .and(validation) + .and(initValidation); } @Value