Class MethodCallExpression

All Implemented Interfaces:
GroovydocHolder<AnnotatedNode>, MethodCall, NodeMetaDataHandler

public class MethodCallExpression extends Expression implements MethodCall
Represents a method call on an object or class, including receiver object, method name/expression, and arguments. Supports safe navigation (null-safe calls with ?), spread-safe operations, implicit this, and direct method targets for optimization. Method names can be constant strings or dynamic expressions (e.g., computed method names).
See Also:
  • Field Details

    • NO_ARGUMENTS

      public static final Expression NO_ARGUMENTS
  • Constructor Details

    • MethodCallExpression

      public MethodCallExpression(Expression objectExpression, String method, Expression arguments)
      Creates a method call with a string method name.
      Parameters:
      objectExpression - the receiver object on which the method is invoked; must not be null
      method - the name of the method as a String; must not be null
      arguments - the method arguments as an Expression; will be wrapped in a TupleExpression if not already one
    • MethodCallExpression

      public MethodCallExpression(Expression objectExpression, Expression method, Expression arguments)
      Creates a method call with an expression-based method name (supports dynamic method names).
      Parameters:
      objectExpression - the receiver object on which the method is invoked; must not be null
      method - the method name as an Expression, which may be a ConstantExpression for static names or other expression types for dynamic names; must not be null
      arguments - the method arguments as an Expression; will be wrapped in a TupleExpression if not already one
  • Method Details

    • visit

      public void visit(GroovyCodeVisitor visitor)
      Description copied from class: ASTNode
      Accepts a code visitor for AST traversal and transformation. Subclasses must implement this method to support visitor pattern-based processing. The visitor pattern enables decoupling of AST structure from processing logic.
      Overrides:
      visit in class ASTNode
      Parameters:
      visitor - the GroovyCodeVisitor to process this node
    • transformExpression

      public Expression transformExpression(ExpressionTransformer transformer)
      Description copied from class: Expression
      Transforms this expression and any nested expressions according to the provided transformer. This method is called during AST transformation phases and must recursively transform any nested expressions to support full AST tree transformation.
      Specified by:
      transformExpression in class Expression
      Parameters:
      transformer - the ExpressionTransformer to apply
      Returns:
      a transformed copy of this expression (or this expression itself if no changes are needed)
    • getArguments

      public Expression getArguments()
      Returns the method arguments as a TupleExpression.
      Specified by:
      getArguments in interface MethodCall
      Returns:
      the method arguments; typically a TupleExpression, never null
    • setArguments

      public void setArguments(Expression arguments)
      Sets the method arguments. If the provided expression is not a TupleExpression, it will be automatically wrapped in one for internal consistency.
      Parameters:
      arguments - the method arguments as an Expression; must not be null
    • getMethod

      public Expression getMethod()
      Returns the method name or reference expression. This may be a ConstantExpression for static method names or other expression types for dynamically computed names.
      Returns:
      the method name Expression; never null
    • setMethod

      public void setMethod(Expression method)
      Sets the method name or reference expression.
      Parameters:
      method - the method name as an Expression; must not be null
    • getMethodAsString

      public String getMethodAsString()
      Returns the method name as a string if it is a constant static name, or null if the method name is computed dynamically.
      Specified by:
      getMethodAsString in interface MethodCall
      Returns:
      the method name as a String if this is a static method call, or null if the method name is a dynamic expression
    • getObjectExpression

      public Expression getObjectExpression()
      Returns the object (receiver) on which this method is invoked.
      Returns:
      the receiver Expression; never null
    • setObjectExpression

      public void setObjectExpression(Expression objectExpression)
      Sets the object (receiver) on which this method is invoked.
      Parameters:
      objectExpression - the receiver Expression; must not be null
    • getReceiver

      public ASTNode getReceiver()
      Returns the receiver as an ASTNode. This is used by the MethodCall interface.
      Specified by:
      getReceiver in interface MethodCall
      Returns:
      the receiver ASTNode
    • getText

      public String getText()
      Description copied from class: ASTNode
      Returns a human-readable text representation of this AST node. Used for debugging and error messages. Default implementation returns a message indicating the representation is not yet implemented for this node type.
      Specified by:
      getText in interface MethodCall
      Overrides:
      getText in class ASTNode
      Returns:
      text representation of this node, or placeholder for unimplemented types
    • isSafe

      public boolean isSafe()
      Indicates whether this is a safe method call. In a safe call, if the receiver object is null, the method call returns null instead of throwing a NullPointerException (e.g., obj?.method()).
      Returns:
      true if this is a safe method call; false otherwise
    • setSafe

      public void setSafe(boolean safe)
      Sets whether this should be a safe method call.
      Parameters:
      safe - true for safe navigation; false otherwise
    • isSpreadSafe

      public boolean isSpreadSafe()
      Indicates whether this method call uses spread-safe navigation. Spread-safe operations iterate over collection elements and apply the method call to each element safely.
      Returns:
      true if spread-safe navigation is enabled; false otherwise
    • setSpreadSafe

      public void setSpreadSafe(boolean value)
      Sets whether this method call should use spread-safe navigation.
      Parameters:
      value - true to enable spread-safe navigation; false otherwise
    • isImplicitThis

      public boolean isImplicitThis()
      Indicates whether this method call implicitly refers to the current object (this). If true, no explicit receiver object was specified (e.g., method() vs obj.method()).
      Returns:
      true if the method call implicitly refers to this; false if an explicit receiver object was specified
    • setImplicitThis

      public void setImplicitThis(boolean implicitThis)
      Sets whether this method call implicitly refers to the current object.
      Parameters:
      implicitThis - true if this is an implicit this call; false otherwise
    • getGenericsTypes

      public GenericsType[] getGenericsTypes()
      Returns the generic type parameters for this method call, if specified. Used for generic method invocations like obj.<String>method().
      Returns:
      an array of GenericsType parameters, or null if no generics are specified
    • setGenericsTypes

      public void setGenericsTypes(GenericsType[] genericsTypes)
      Sets the generic type parameters for this method call.
      Parameters:
      genericsTypes - an array of GenericsType parameters; may be null
    • isUsingGenerics

      public boolean isUsingGenerics()
      Indicates whether generic type parameters are specified for this method call.
      Returns:
      true if generic type parameters are present; false otherwise
    • getMethodTarget

      public MethodNode getMethodTarget()
      Returns the target MethodNode if this method call has been resolved to a specific method. When a target is set, the method call will execute the direct target outside of the MOP (method resolution order), enabling direct method invocation for optimization.
      Returns:
      the target MethodNode, or null if no specific target is set
    • setMethodTarget

      public void setMethodTarget(MethodNode mn)
      Sets a direct method target for this method call. When set, the method call will invoke the specified target directly, bypassing the MOP (method resolution order) for efficiency. WARNING: A method call made this way will run outside of the MOP and may not respect dynamic dispatch or meta-programming extensions.
      Parameters:
      mn - the target MethodNode; may be null to clear the target
    • setSourcePosition

      public void setSourcePosition(ASTNode node)
      Description copied from class: ASTNode
      Sets the source position information using another ASTNode as reference. Copies all position data (line/column start and end) from the source node, enabling consistent source location tracking for synthetic or transformed nodes.
      Overrides:
      setSourcePosition in class ASTNode
      Parameters:
      node - the reference node providing position information
    • toString

      public String toString()
      Overrides:
      toString in class Object