Class ScanningRecipe<T>

java.lang.Object
org.openrewrite.Recipe
org.openrewrite.ScanningRecipe<T>
Type Parameters:
T - The type of the accumulator where scanning data is held until the transformation phase.
All Implemented Interfaces:
Cloneable
Direct Known Subclasses:
AppendToTextFile, CreateTextFile, FindCollidingSourceFiles, FindCommitters, FindGitProvenance, FindLstProvenance, ListRuntimeClasspath

public abstract class ScanningRecipe<T> extends Recipe
A recipe that may first scan a repository and study it in one pass over the repository's source files before, in another pass, it decides how to transform the code.
New source file generation is part of this type as well, since in almost every case we check that a file doesn't yet exist (and perhaps other conditions) before deciding to generate a file.
  • Constructor Details

    • ScanningRecipe

      public ScanningRecipe()
  • Method Details

    • getInitialValue

      public abstract T getInitialValue(ExecutionContext ctx)
      Returns:
      The initial value of the accumulator before any source files have been iterated over.
    • getScanner

      public abstract TreeVisitor<?,ExecutionContext> getScanner(T acc)
      A visitor that is called for each source file in the repository in an initial pass. Scanning data should be accumulated to acc. The first source file to visit will receive an acc value that is supplied by getInitialValue(ExecutionContext).
      Any changes that the scanning visitor makes to the source file will be discarded.
      Parameters:
      acc - The accumulated scanning data.
      Returns:
      A visitor that is called to collect scanning data on each source file.
    • generate

      public Collection<? extends SourceFile> generate(T acc, Collection<SourceFile> generatedInThisCycle, ExecutionContext ctx)
      Generate new source files to add to the repository using information collected from scanning.
      Parameters:
      acc - The accumulated scanning data.
      generatedInThisCycle - Files generated by other recipes in this cycle.
      Returns:
      A list of new source files.
    • generate

      public Collection<? extends SourceFile> generate(T acc, ExecutionContext ctx)
      Generate new source files to add to the repository using information collected from scanning.
      Parameters:
      acc - The accumulated scanning data.
      Returns:
      A list of new source files.
    • getVisitor

      public TreeVisitor<?,ExecutionContext> getVisitor(T acc)
      A visitor that is called in a second pass to perform transformation on each source file. To delete a source file, return null from the TreeVisitor.visit(Tree, Object) method.
      Parameters:
      acc - The accumulated scanning data.
      Returns:
      A visitor that is called to perform transformation on each source file.
    • getAccumulator

      public T getAccumulator(Cursor cursor, ExecutionContext ctx)
    • getVisitor

      public final TreeVisitor<?,ExecutionContext> getVisitor()
      Description copied from class: Recipe
      A recipe can optionally encapsulate 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.
      Overrides:
      getVisitor in class Recipe
      Returns:
      A tree visitor that will perform operations associated with the recipe.