Class TreeVisitor<T extends @Nullable Tree,P>

java.lang.Object
org.openrewrite.TreeVisitor<T,P>
Type Parameters:
T - The type of tree.
P - An input object that is passed to every visit method.
Direct Known Subclasses:
BinaryVisitor, CreateFileVisitor, FindRecipeRunException, ParseErrorVisitor, PlainTextVisitor, Preconditions.Check, QuarkVisitor, RemoteVisitor

public abstract class TreeVisitor<T extends @Nullable Tree,P> extends Object
Abstract TreeVisitor for processing elements

Always returns input type T provides Parameterizable P input which is mutable allowing context to be shared

postProcessing via afterVisit for conditionally chaining other operations with the expectation is that after TreeVisitors are invoked immediately after visiting SourceFile

  • Constructor Details

    • TreeVisitor

      public TreeVisitor()
  • Method Details

    • noop

      public static <T extends Tree, P> TreeVisitor<T,P> noop()
    • isAcceptable

      public boolean isAcceptable(SourceFile sourceFile, P p)
    • setCursor

      public void setCursor(@Nullable Cursor cursor)
    • getLanguage

      public @Nullable String getLanguage()
      Returns:
      Describes the language type that this visitor applies to, e.g. java, xml, properties.
    • doAfterVisit

      protected void doAfterVisit(TreeVisitor<?,P> visitor)
      Execute the visitor once after the whole source file has been visited. The visitor is executed against the whole source file. This operation only happens once immediately after the containing visitor visits the whole source file. A subsequent Recipe cycle will not run it.

      This method is ideal for one-off operations like auto-formatting, adding/removing imports, etc.

      Parameters:
      visitor - The visitor to run.
    • getAfterVisit

      protected List<TreeVisitor<?,P>> getAfterVisit()
    • getCursor

      public final Cursor getCursor()
    • updateCursor

      public final Cursor updateCursor(T currentValue)
      Updates the cursor on this visitor and returns the updated cursor.
      Parameters:
      currentValue - The new (current) value of the cursor based on a mutation of what was formerly the Cursor.getValue().
      Returns:
      The updated cursor.
    • preVisit

      public @Nullable T preVisit(@NonNull T tree, P p)
    • postVisit

      public @Nullable T postVisit(@NonNull T tree, P p)
    • visit

      public @Nullable T visit(@Nullable Tree tree, P p, Cursor parent)
    • visitNonNull

      public @NonNull T visitNonNull(Tree tree, P p)
      By calling this method, you are asserting that you know that the outcome will be non-null when the compiler couldn't otherwise prove this to be the case. This method is a shortcut for having to assert the non-nullability of the returned tree.
      Parameters:
      tree - A non-null tree.
      p - A state object that passes through the visitor.
      Returns:
      A non-null tree.
    • visitNonNull

      public @NonNull T visitNonNull(Tree tree, P p, Cursor parent)
    • collect

      @Incubating(since="7.31.0") public static <R extends Tree, C extends Collection<R>> C collect(TreeVisitor<?,ExecutionContext> visitor, Tree tree, C initial)
    • collect

      @Incubating(since="7.31.0") public static <U extends Tree, R, C extends Collection<R>> C collect(TreeVisitor<?,ExecutionContext> visitor, Tree tree, C initial, Class<U> matchOn, Function<U,R> map)
    • reduce

      @Incubating(since="7.31.0") public P reduce(Iterable<? extends Tree> trees, P p)
    • reduce

      @Incubating(since="7.31.0") public P reduce(Tree tree, P p)
    • reduce

      @Incubating(since="7.31.0") public P reduce(Tree tree, P p, Cursor parent)
    • visit

      public @Nullable T visit(@Nullable Tree tree, P p)
    • visit

      public void visit(@Nullable List<? extends T> nodes, P p)
    • defaultValue

      public @Nullable T defaultValue(@Nullable Tree tree, P p)
    • visitAndCast

      @Incubating(since="7.0.0") protected final <T2 extends Tree> T2 visitAndCast(T2 t, P p, BiFunction<T2,P,Tree> callSuper)
    • visitAndCast

      @Incubating(since="7.0.0") protected final <T2 extends T> @Nullable T2 visitAndCast(@Nullable Tree tree, P p)
    • visitMarkers

      public Markers visitMarkers(@Nullable Markers markers, P p)
    • visitMarker

      public <M extends Marker> M visitMarker(Marker marker, P p)
    • isAdaptableTo

      public boolean isAdaptableTo(Class<? extends TreeVisitor> adaptTo)
    • visitorTreeType

      protected Class<? extends Tree> visitorTreeType(Class<? extends TreeVisitor> v)
    • adapt

      public <R extends Tree, V extends TreeVisitor<R, P>> V adapt(Class<? extends V> adaptTo)
    • stopAfterPreVisit

      @Incubating(since="8.0.0") public void stopAfterPreVisit()
      Can be used in preVisit(Tree, Object) to prevent a call to visit(Tree, Object) and postVisit(Tree, Object), therefore stopping further navigation deeper down the tree.