Class ASTUtils


  • public final class ASTUtils
    extends Object
    Visitor-backed helpers for traversing and querying AST nodes.
    • Method Detail

      • forEachStatement

        public static void forEachStatement​(Statement root,
                                            Consumer<Statement> action)
        Walks every statement under a root in depth-first order.
        Parameters:
        root - statement to walk
        action - invoked once per visited statement
      • collect

        public static <T extends StatementList<T> collect​(Statement root,
                                                            Class<T> type)
        Collects the statements under a root that are instances of one type.
        Type Parameters:
        T - statement type collected
        Parameters:
        root - statement to walk
        type - type to match
        Returns:
        the matching statements in visit order
      • filter

        public static List<Statement> filter​(Statement root,
                                             Predicate<Statement> pred)
        Collects the statements under a root that satisfy a predicate.
        Parameters:
        root - statement to walk
        pred - test applied to each visited statement
        Returns:
        the accepted statements in visit order
      • forEachExpression

        public static void forEachExpression​(Statement root,
                                             Consumer<Expression> action)
        Walks every expression under a statement tree in depth-first order.
        Parameters:
        root - statement to walk
        action - invoked once per visited expression
      • collectExpressions

        public static <T extends ExpressionList<T> collectExpressions​(Statement root,
                                                                        Class<T> type)
        Collects the expressions in a statement tree that are instances of one type.
        Type Parameters:
        T - expression type collected
        Parameters:
        root - statement to walk
        type - type to match
        Returns:
        the matching expressions in visit order
      • filterExpressions

        public static List<Expression> filterExpressions​(Statement root,
                                                         Predicate<Expression> pred)
        Collects the expressions in a statement tree that satisfy a predicate.
        Parameters:
        root - statement to walk
        pred - test applied to each visited expression
        Returns:
        the accepted expressions in visit order