Class EditorContext


  • public class EditorContext
    extends Object
    Traversal context handed to AST editor handlers.
    • Constructor Detail

      • EditorContext

        public EditorContext​(BlockStmt methodBody,
                             String methodName,
                             String methodDescriptor,
                             String ownerClass)
        Creates a context for editing the given method body.
        Parameters:
        methodBody - the method body being edited
        methodName - the method name; a placeholder is used when null
        methodDescriptor - the method descriptor; defaults to ()V when null
        ownerClass - the internal name of the owning class; a placeholder is used when null
    • Method Detail

      • getMethodBody

        public BlockStmt getMethodBody()
        Returns:
        the method body being edited
      • getMethodName

        public String getMethodName()
        Returns:
        the name of the method being edited
      • getMethodDescriptor

        public String getMethodDescriptor()
        Returns:
        the descriptor of the method being edited
      • getOwnerClass

        public String getOwnerClass()
        Returns:
        the internal name of the class that owns this method
      • getCurrentStatement

        public Statement getCurrentStatement()
        Returns:
        the statement currently being visited
      • setCurrentStatement

        public void setCurrentStatement​(Statement stmt)
        Sets the current statement; called by the editor during traversal.
        Parameters:
        stmt - the statement now being visited
      • getEnclosingBlock

        public BlockStmt getEnclosingBlock()
        Returns:
        the block enclosing the current statement
      • setEnclosingBlock

        public void setEnclosingBlock​(BlockStmt block)
        Sets the enclosing block; called by the editor during traversal.
        Parameters:
        block - the block enclosing the current statement
      • getStatementIndex

        public int getStatementIndex()
        Returns:
        the index of the current statement within its enclosing block
      • setStatementIndex

        public void setStatementIndex​(int index)
        Sets the statement index; called by the editor during traversal.
        Parameters:
        index - the index of the current statement within its enclosing block
      • isInTryBlock

        public boolean isInTryBlock()
        Returns:
        true if the current location is inside a try block
      • isInLoop

        public boolean isInLoop()
        Returns:
        true if the current location is inside a loop
      • isInConditional

        public boolean isInConditional()
        Returns:
        true if the current location is inside a conditional (if/switch)
      • enterTry

        public void enterTry()
        Records entry into a try block.
      • exitTry

        public void exitTry()
        Records exit from a try block.
      • enterLoop

        public void enterLoop()
        Records entry into a loop.
      • exitLoop

        public void exitLoop()
        Records exit from a loop.
      • enterConditional

        public void enterConditional()
        Records entry into a conditional.
      • exitConditional

        public void exitConditional()
        Records exit from a conditional.
      • getVisibleVariables

        public Set<String> getVisibleVariables()
        Collects variables visible at the current location; simplified to the null-checked set.
        Returns:
        a copy of the null-checked variable names
      • isNullChecked

        public boolean isNullChecked​(Expression expr)
        Checks whether an expression has been marked null-checked; only simple variable references are tracked.
        Parameters:
        expr - the expression to test
        Returns:
        true if the expression is a tracked null-checked variable
      • markNullChecked

        public void markNullChecked​(String varName)
        Marks a variable as null-checked.
        Parameters:
        varName - the variable name; ignored when null
      • clearNullChecks

        public void clearNullChecks()
        Clears all null-check tracking.
      • factory

        public ASTFactory factory()
        Returns:
        the AST factory for creating new nodes
      • insertBefore

        public Replacement insertBefore​(Statement... stmts)
        Creates a replacement that inserts statements before the current statement.
        Parameters:
        stmts - the statements to insert
        Returns:
        the insert-before replacement
      • insertAfter

        public Replacement insertAfter​(Statement... stmts)
        Creates a replacement that inserts statements after the current statement.
        Parameters:
        stmts - the statements to insert
        Returns:
        the insert-after replacement
      • wrapWithNullCheck

        public Replacement wrapWithNullCheck​(Expression expr)
        Marks the expression's variable as null-checked without altering the node.
        Parameters:
        expr - the expression to check
        Returns:
        a keep replacement
      • findEnclosingStatement

        public Statement findEnclosingStatement​(Expression expr)
        Walks parent links to find the statement that contains an expression.
        Parameters:
        expr - the expression to search from
        Returns:
        the enclosing statement, or null if none is found
      • createNestedContext

        public EditorContext createNestedContext()
        Creates a copy of this context, carrying over depths and null-check state, for nested traversal.
        Returns:
        the nested context