Class StatementEditor


  • public class StatementEditor
    extends Object
    Statement-only facade over ASTEditor.
    • Constructor Detail

      • StatementEditor

        public StatementEditor​(BlockStmt methodBody,
                               String methodName,
                               String methodDescriptor,
                               String ownerClass)
        Creates a statement editor for a method body.
        Parameters:
        methodBody - the method body to edit
        methodName - the name of the method
        methodDescriptor - the method descriptor
        ownerClass - the internal name of the owning class
    • Method Detail

      • onReturn

        public StatementEditor onReturn​(ReturnHandler handler)
        Registers a handler for return statements.
        Parameters:
        handler - the handler to invoke
        Returns:
        this editor
      • onThrow

        public StatementEditor onThrow​(ThrowHandler handler)
        Registers a handler for throw statements.
        Parameters:
        handler - the handler to invoke
        Returns:
        this editor
      • onIf

        public StatementEditor onIf​(IfHandler handler)
        Registers a handler for if statements.
        Parameters:
        handler - the handler to invoke
        Returns:
        this editor
      • onLoop

        public StatementEditor onLoop​(LoopHandler handler)
        Registers a handler for for, while, do-while and for-each statements.
        Parameters:
        handler - the handler to invoke
        Returns:
        this editor
      • onTryCatch

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

        public StatementEditor onAssignment​(AssignmentHandler handler)
        Registers a handler for assignment statements.
        Parameters:
        handler - the handler to invoke
        Returns:
        this editor
      • onStmt

        public StatementEditor onStmt​(StmtMatcher matcher,
                                      StatementHandler handler)
        Registers a handler for the statements a matcher accepts.
        Parameters:
        matcher - the predicate statements are tested against
        handler - the handler to invoke on a match
        Returns:
        this editor
      • onAnyStmt

        public StatementEditor onAnyStmt​(StatementHandler handler)
        Registers a handler that sees every statement.
        Parameters:
        handler - the handler to invoke
        Returns:
        this editor
      • insertBeforeReturns

        public StatementEditor insertBeforeReturns​(Statement... stmts)
        Registers a handler that inserts statements ahead of every return.
        Parameters:
        stmts - the statements to insert, in order
        Returns:
        this editor
      • insertBeforeValueReturns

        public StatementEditor insertBeforeValueReturns​(Statement... stmts)
        Registers a handler that inserts statements ahead of every value-carrying return.
        Parameters:
        stmts - the statements to insert, in order
        Returns:
        this editor
      • insertBeforeVoidReturns

        public StatementEditor insertBeforeVoidReturns​(Statement... stmts)
        Registers a handler that inserts statements ahead of every valueless return.
        Parameters:
        stmts - the statements to insert, in order
        Returns:
        this editor
      • wrapLoops

        public StatementEditor wrapLoops​(Statement before,
                                         Statement after)
        Registers a handler that replaces every loop with a block bracketing it.
        Parameters:
        before - the statement placed ahead of the loop
        after - the statement placed after the loop
        Returns:
        this editor
      • insertBeforeThrows

        public StatementEditor insertBeforeThrows​(Statement... stmts)
        Registers a handler that inserts statements ahead of every throw.
        Parameters:
        stmts - the statements to insert, in order
        Returns:
        this editor
      • removeStmts

        public StatementEditor removeStmts​(Class<? extends Statement> type)
        Registers a handler that drops every statement of a given node type.
        Parameters:
        type - the statement class to remove
        Returns:
        this editor
      • removeStmts

        public StatementEditor removeStmts​(StmtMatcher matcher)
        Registers a handler that drops every statement the matcher accepts.
        Parameters:
        matcher - the predicate statements are tested against
        Returns:
        this editor
      • 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
      • findReturns

        public List<Statement> findReturns()
        Finds all return statements.
        Returns:
        the matching statements in traversal order
      • findValueReturns

        public List<Statement> findValueReturns()
        Finds all returns that carry a value.
        Returns:
        the matching statements in traversal order
      • findVoidReturns

        public List<Statement> findVoidReturns()
        Finds all returns that carry no value.
        Returns:
        the matching statements in traversal order
      • findThrows

        public List<Statement> findThrows()
        Finds all throw statements.
        Returns:
        the matching statements in traversal order
      • findLoops

        public List<Statement> findLoops()
        Finds all for, while, do-while and for-each statements.
        Returns:
        the matching statements in traversal order
      • findIfs

        public List<Statement> findIfs()
        Finds all if statements.
        Returns:
        the matching statements in traversal order
      • findIfElses

        public List<Statement> findIfElses()
        Finds all if statements that have an else branch.
        Returns:
        the matching statements in traversal order
      • findTryCatches

        public List<Statement> findTryCatches()
        Finds all try-catch statements.
        Returns:
        the matching statements in traversal order
      • findSwitches

        public List<Statement> findSwitches()
        Finds all switch statements.
        Returns:
        the matching statements in traversal order
      • findVarDecls

        public List<Statement> findVarDecls()
        Finds all variable declaration statements.
        Returns:
        the matching statements in traversal order
      • apply

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

        public ASTEditor getDelegate()
        Returns:
        the wrapped editor, for operations this facade does not expose