Class Recipe

java.lang.Object
org.openrewrite.Recipe
All Implemented Interfaces:
Cloneable
Direct Known Subclasses:
AppendToTextFile, ChangeText, CompositeRecipe, CreateTextFile, DeleteSourceFiles, EndOfLineAtEndOfFile, Find, FindAndReplace, FindBuildToolFailures, FindGitProvenance, FindParseFailures, FindQuarks, FindSourceFiles, ListRuntimeClasspath, RenameFile, SetFilePermissions

public abstract class Recipe extends Object implements Cloneable
Provides a formalized link list data structure of recipes and a run(List) method which will apply each recipes visitor visit method to a list of sourceFiles

Requires a name, visitor. Optionally a subsequent Recipe can be linked via doNext(Recipe)}

An ExecutionContext controls parallel execution and lifecycle while providing a message bus for sharing state between recipes and their visitors

returns a list of results for each modified SourceFile

  • Field Details

  • Constructor Details

    • Recipe

      public Recipe()
  • Method Details

    • getJacksonPolymorphicTypeTag

      public String getJacksonPolymorphicTypeTag()
    • noop

      public static Recipe noop()
    • getDisplayName

      @Language("markdown") public abstract String getDisplayName()
      A human-readable display name for the recipe, initial capped with no period. For example, "Find text". The display name can be assumed to be rendered in documentation and other places where markdown is understood, so it is possible to use stylistic markers like backticks to indicate types. For example, "Find uses of `java.util.List`".
      Returns:
      The display name.
    • getDescription

      @Language("markdown") public String getDescription()
      A human-readable description for the recipe, consisting of one or more full sentences ending with a period.

      "Find methods by pattern." is an example. The description can be assumed to be rendered in documentation and other places where markdown is understood, so it is possible to use stylistic markers like backticks to indicate types. For example, "Find uses of `java.util.List`.".

      Returns:
      The display name.
    • getTags

      public Set<String> getTags()
      A set of strings used for categorizing related recipes. For example "testing", "junit", "spring". Any individual tag should consist of a single word, all lowercase.
      Returns:
      The tags.
    • getEstimatedEffortPerOccurrence

      @Nullable public @Nullable Duration getEstimatedEffortPerOccurrence()
      Returns:
      An estimated effort were a developer to fix manually instead of using this recipe.
    • getDescriptor

      public final RecipeDescriptor getDescriptor()
    • createRecipeDescriptor

      protected RecipeDescriptor createRecipeDescriptor()
    • getDataTableDescriptors

      public List<DataTableDescriptor> getDataTableDescriptors()
    • getLanguages

      @Deprecated public List<String> getLanguages()
      Deprecated.
      Returns:
      Describes the language type(s) that this recipe applies to, e.g. java, xml, properties.
    • getMaintainers

      public List<Maintainer> getMaintainers()
      Returns:
      a list of the organization(s) responsible for maintaining this recipe.
    • getContributors

      public List<Contributor> getContributors()
    • getExamples

      public List<RecipeExample> getExamples()
    • causesAnotherCycle

      @Incubating(since="7.3.0") public boolean causesAnotherCycle()
      Returns:
      Determines if another cycle is run when this recipe makes a change. In some cases, like changing method declaration names, a further cycle is needed to update method invocations of that declaration that were visited prior to the declaration change. But other visitors never need to cause another cycle, such as those that format whitespace or add search markers. Note that even when this is false, the recipe will still run on another cycle if any other recipe causes another cycle to run. But if every recipe reports no need to run another cycle (or if there are no changes made in a cycle), then another will not run.
    • doNext

      public Recipe doNext(Recipe recipe)
      Parameters:
      recipe - Recipe to add to this recipe's pipeline.
      Returns:
      This recipe.
    • getRecipeList

      public List<Recipe> getRecipeList()
    • getVisitor

      protected TreeVisitor<?,ExecutionContext> getVisitor()
      A recipe can optionally encasulate a visitor that performs operations on a set of source files. Subclasses of the recipe may override this method to provide an instance of a visitor that will be used when the recipe is executed.
      Returns:
      A tree visitor that will perform operations associated with the recipe.
    • getApplicableTest

      @Nullable protected @Nullable TreeVisitor<?,ExecutionContext> getApplicableTest()
      A recipe can optionally include an applicability test that can be used to determine whether it should run on a set of source files (or even be listed in a suggested list of recipes for a particular codebase).

      To identify a tree as applicable, the visitor should mark or otherwise modify any tree at any level. Any change made by the applicability test visitor will not be included in the results.

      Returns:
      A tree visitor that performs an applicability test.
    • addApplicableTest

      public Recipe addApplicableTest(TreeVisitor<?,ExecutionContext> test)
      A recipe can be configured with any number of applicable tests that can be used to determine whether it should run on a particular source file. If multiple applicable tests configured, the final result of the applicable test depends on all conditions being met, that is, a logical 'AND' relationship.

      To identify a SourceFile as applicable, the TreeVisitor should mark or change it at any level. Any mutation that the applicability test visitor makes on the tree will not be included in the results.

      Returns:
      This recipe.
    • addApplicableTest

      public Recipe addApplicableTest(Recipe test)
      A recipe can be configured with any number of applicable tests that can be used to determine whether it should run on a particular source file. If multiple applicable tests configured, the final result of the applicable test depends on all conditions being met, that is, a logical 'AND' relationship.

      To identify a SourceFile as applicable, the Recipe should mark or change it at any level. Any mutation that the applicability test recipe makes on the tree will not be included in the results.

      Returns:
      This recipe. Not the argument passed.
    • addDataTable

      public void addDataTable(DataTable<?> dataTable)
    • getApplicableTests

      public List<Recipe> getApplicableTests()
    • getSingleSourceApplicableTest

      @Nullable protected @Nullable TreeVisitor<?,ExecutionContext> getSingleSourceApplicableTest()
      A recipe can optionally include an applicability test that can be used to determine whether it should run on a particular source file.

      To identify a SourceFile as applicable, the visitor should mark it at any level. Any mutation that the applicability test visitor makes on the tree will not be included in the results.

      Returns:
      A tree visitor that performs an applicability test.
    • addSingleSourceApplicableTest

      public Recipe addSingleSourceApplicableTest(TreeVisitor<?,ExecutionContext> test)
      A recipe can be configured with any number of applicable tests that can be used to determine whether it should run on a particular source file. If multiple applicable tests configured, the final result of the applicable test depends on all conditions being met, that is, a logical 'AND' relationship.

      To identify a SourceFile as applicable, the TreeVisitor should mark or change it at any level. Any mutation that the applicability test visitor makes on the tree will not be included in the results.

      Returns:
      This recipe.
    • addSingleSourceApplicableTest

      public Recipe addSingleSourceApplicableTest(Recipe test)
      A recipe can be configured with any number of applicable tests that can be used to determine whether it should run on a particular source file. If multiple applicable tests configured, the final result of the applicable test depends on all conditions being met, that is, a logical 'AND' relationship.

      To identify a SourceFile as applicable, the Recipe should mark or change it at any level. Any mutation that the applicability test recipe makes on the tree will not be included in the results.

      Returns:
      This recipe. Not the argument passed.
    • getSingleSourceApplicableTests

      public List<Recipe> getSingleSourceApplicableTests()
    • visit

      protected List<SourceFile> visit(List<SourceFile> before, ExecutionContext ctx)
      Override this to generate new source files or delete source files. Note that here, as throughout OpenRewrite, we use referential equality to detect that a change has occured. To indicate to rewrite that the recipe has made changes a different instance must be returned than the instance passed in as "before".

      Currently, the list passed in as "before" is not immutable, but you should treat it as such anyway.

      Parameters:
      before - The set of source files to operate on.
      ctx - The current execution context.
      Returns:
      A set of source files, with some files potentially added/deleted/modified.
    • run

      public final RecipeRun run(List<? extends SourceFile> before)
    • run

      public final RecipeRun run(List<? extends SourceFile> before, ExecutionContext ctx)
    • run

      public final RecipeRun run(List<? extends SourceFile> before, ExecutionContext ctx, int maxCycles)
    • run

      public final RecipeRun run(List<? extends SourceFile> before, ExecutionContext ctx, RecipeScheduler recipeScheduler, int maxCycles, int minCycles)
    • validate

      public Validated validate(ExecutionContext ctx)
    • validate

      public Validated validate()
      The default implementation of validate on the recipe will look for package and field level annotations that indicate a field is not-null. The annotations must have run-time retention and the simple name of the annotation must match one of the common names defined in NullUtils
      Returns:
      A validated instance based using non-null/nullable annotations to determine which fields of the recipe are required.
    • validateAll

      @Incubating(since="7.0.0") public final Collection<Validated> validateAll(ExecutionContext ctx)
    • validateAll

      public final Collection<Validated> validateAll()
    • getName

      public String getName()
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • clone

      public Object clone()
      Overrides:
      clone in class Object