Class ASTEditor


  • public class ASTEditor
    extends Object
    Fluent editor that walks a method body, dispatching registered handlers to rewrite AST nodes.
    • Constructor Detail

      • ASTEditor

        public ASTEditor​(BlockStmt methodBody,
                         String methodName,
                         String methodDescriptor,
                         String ownerClass)
        Creates a new AST editor for the given method body.
        Parameters:
        methodBody - the block to edit
        methodName - the enclosing method's name, exposed to handlers
        methodDescriptor - the enclosing method's descriptor, exposed to handlers
        ownerClass - the internal name of the declaring class, exposed to handlers
      • ASTEditor

        public ASTEditor​(BlockStmt methodBody)
        Creates a new AST editor with minimal context.
        Parameters:
        methodBody - the block to edit
    • Method Detail

      • onMethodCall

        public ASTEditor onMethodCall​(MethodCallHandler handler)
        Registers a handler for method call expressions.
        Parameters:
        handler - the handler to run on every call site
        Returns:
        this editor
      • onFieldAccess

        public ASTEditor onFieldAccess​(FieldAccessHandler handler)
        Registers a handler for field access expressions.
        Parameters:
        handler - the handler to run on every field access
        Returns:
        this editor
      • onNewExpr

        public ASTEditor onNewExpr​(NewExprHandler handler)
        Registers a handler for new object expressions.
        Parameters:
        handler - the handler to run on every allocation
        Returns:
        this editor
      • onNewArray

        public ASTEditor onNewArray​(NewArrayHandler handler)
        Registers a handler for new array expressions.
        Parameters:
        handler - the handler to run on every array allocation
        Returns:
        this editor
      • onCast

        public ASTEditor onCast​(CastHandler handler)
        Registers a handler for cast expressions.
        Parameters:
        handler - the handler to run on every cast
        Returns:
        this editor
      • onInstanceOf

        public ASTEditor onInstanceOf​(InstanceOfHandler handler)
        Registers a handler for instanceof expressions.
        Parameters:
        handler - the handler to run on every instanceof test
        Returns:
        this editor
      • onBinaryExpr

        public ASTEditor onBinaryExpr​(BinaryExprHandler handler)
        Registers a handler for binary expressions.
        Parameters:
        handler - the handler to run on every binary expression
        Returns:
        this editor
      • onUnaryExpr

        public ASTEditor onUnaryExpr​(UnaryExprHandler handler)
        Registers a handler for unary expressions.
        Parameters:
        handler - the handler to run on every unary expression
        Returns:
        this editor
      • onArrayAccess

        public ASTEditor onArrayAccess​(ArrayAccessHandler handler)
        Registers a handler for array access expressions.
        Parameters:
        handler - the handler to run on every array access
        Returns:
        this editor
      • onArrayRead

        public ASTEditor onArrayRead​(ArrayAccessHandler handler)
        Registers a handler for array read operations only.
        Parameters:
        handler - the handler to run on array accesses whose type is READ
        Returns:
        this editor
      • onArrayStore

        public ASTEditor onArrayStore​(ArrayAccessHandler handler)
        Registers a handler for array store operations only.
        Parameters:
        handler - the handler to run on array accesses whose type is STORE
        Returns:
        this editor
      • onReturn

        public ASTEditor onReturn​(ReturnHandler handler)
        Registers a handler for return statements.
        Parameters:
        handler - the handler to run on every return
        Returns:
        this editor
      • onThrow

        public ASTEditor onThrow​(ThrowHandler handler)
        Registers a handler for throw statements.
        Parameters:
        handler - the handler to run on every throw
        Returns:
        this editor
      • onIf

        public ASTEditor onIf​(IfHandler handler)
        Registers a handler for if statements.
        Parameters:
        handler - the handler to run on every if
        Returns:
        this editor
      • onLoop

        public ASTEditor onLoop​(LoopHandler handler)
        Registers a handler for loop statements (for, while, do-while, for-each).
        Parameters:
        handler - the handler to run on every loop
        Returns:
        this editor
      • onTryCatch

        public ASTEditor onTryCatch​(TryCatchHandler handler)
        Registers a handler for try-catch statements.
        Parameters:
        handler - the handler to run on every try-catch
        Returns:
        this editor
      • onAssignment

        public ASTEditor onAssignment​(AssignmentHandler handler)
        Registers a handler for assignment expressions.
        Parameters:
        handler - the handler to run on every assignment
        Returns:
        this editor
      • onExpr

        public ASTEditor onExpr​(ExprMatcher matcher,
                                ExpressionHandler handler)
        Registers a handler for expressions matching the given matcher.
        Parameters:
        matcher - selects which expressions the handler sees
        handler - the handler to run on each match
        Returns:
        this editor
      • onStmt

        public ASTEditor onStmt​(StmtMatcher matcher,
                                StatementHandler handler)
        Registers a handler for statements matching the given matcher.
        Parameters:
        matcher - selects which statements the handler sees
        handler - the handler to run on each match
        Returns:
        this editor
      • apply

        public void apply()
        Applies all registered handlers and modifies the AST in place.
      • applyAndReturn

        public BlockStmt applyAndReturn()
        Applies all registered handlers and returns the modified method body.
        Returns:
        the method body, edited in place
      • findExpressions

        public List<Expression> findExpressions​(ExprMatcher matcher)
        Finds all expressions matching the given matcher.
        Parameters:
        matcher - the predicate expressions are tested against
        Returns:
        the matching expressions in traversal order
      • findStatements

        public List<Statement> findStatements​(StmtMatcher matcher)
        Finds all statements matching the given matcher.
        Parameters:
        matcher - the predicate statements are tested against
        Returns:
        the matching statements in traversal order