Class ExpressionEditor


  • public class ExpressionEditor
    extends Object
    Expression-only view over ASTEditor.
    • Constructor Detail

      • ExpressionEditor

        public ExpressionEditor​(BlockStmt methodBody,
                                String methodName,
                                String methodDescriptor,
                                String ownerClass)
        Creates an expression 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

      • onMethodCall

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

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

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

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

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

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

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

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

        public ExpressionEditor onArrayAccess​(ArrayAccessHandler handler)
        Registers a handler for array accesses, reads and stores alike.
        Parameters:
        handler - the handler to run on each access; its context says whether it is a read or a store
        Returns:
        this editor
      • onArrayRead

        public ExpressionEditor onArrayRead​(ArrayAccessHandler handler)
        Registers a handler for array reads only.
        Parameters:
        handler - the handler to run on each array read
        Returns:
        this editor
      • onArrayStore

        public ExpressionEditor onArrayStore​(ArrayAccessHandler handler)
        Registers a handler for array stores only.
        Parameters:
        handler - the handler to run on each array store
        Returns:
        this editor
      • onExpr

        public ExpressionEditor onExpr​(ExprMatcher matcher,
                                       ExpressionHandler handler)
        Registers a handler for the expressions a matcher selects.
        Parameters:
        matcher - selects which expressions reach the handler
        handler - the handler to run on each match
        Returns:
        this editor
      • onAnyExpr

        public ExpressionEditor onAnyExpr​(ExpressionHandler handler)
        Registers a handler for every expression in the body.
        Parameters:
        handler - the handler to run on each expression
        Returns:
        this editor
      • replaceMethodCall

        public ExpressionEditor replaceMethodCall​(String ownerClass,
                                                  String methodName,
                                                  ExpressionEditor.MethodCallReplacer replacer)
        Replaces all method calls matching the criteria.
        Parameters:
        ownerClass - the owner class to match
        methodName - the method name to match
        replacer - function to create replacement expression
        Returns:
        this editor for chaining
      • removeMethodCall

        public ExpressionEditor removeMethodCall​(String ownerClass,
                                                 String methodName)
        Removes all method calls matching the criteria.
        Parameters:
        ownerClass - the owner class to match
        methodName - the method name to match
        Returns:
        this editor for chaining
      • replaceFieldAccess

        public ExpressionEditor replaceFieldAccess​(String ownerClass,
                                                   String fieldName,
                                                   ExpressionEditor.FieldAccessReplacer replacer)
        Replaces all field accesses matching the criteria.
        Parameters:
        ownerClass - the owner class to match
        fieldName - the field name to match
        replacer - function to create replacement expression
        Returns:
        this editor for chaining
      • replaceNewExpr

        public ExpressionEditor replaceNewExpr​(String className,
                                               ExpressionEditor.NewExprReplacer replacer)
        Replaces all new expressions of a specific class.
        Parameters:
        className - the class name to match
        replacer - function to create replacement expression
        Returns:
        this editor for chaining
      • findExpressions

        public List<Expression> findExpressions​(ExprMatcher matcher)
        Collects the expressions a matcher selects.
        Parameters:
        matcher - selects which expressions to collect
        Returns:
        the matching expressions
      • findMethodCalls

        public List<Expression> findMethodCalls()
        Returns:
        every method call expression in the body
      • findMethodCalls

        public List<Expression> findMethodCalls​(String methodName)
        Collects the calls to one method name, whatever the owner.
        Parameters:
        methodName - the method name to match
        Returns:
        the matching call expressions
      • findMethodCalls

        public List<Expression> findMethodCalls​(String ownerClass,
                                                String methodName)
        Collects the calls to one method on one owner.
        Parameters:
        ownerClass - the owning class to match
        methodName - the method name to match
        Returns:
        the matching call expressions
      • findFieldAccesses

        public List<Expression> findFieldAccesses()
        Returns:
        every field access expression in the body
      • findNewExpressions

        public List<Expression> findNewExpressions​(String className)
        Collects the allocations of one class.
        Parameters:
        className - the allocated class to match
        Returns:
        the matching new expressions
      • findArrayAccesses

        public List<Expression> findArrayAccesses()
        Returns:
        every array access expression in the body
      • apply

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

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