Package org.codehaus.groovy.ast.expr
Class MethodCallExpression
java.lang.Object
org.codehaus.groovy.ast.ASTNode
org.codehaus.groovy.ast.AnnotatedNode
org.codehaus.groovy.ast.expr.Expression
org.codehaus.groovy.ast.expr.MethodCallExpression
- All Implemented Interfaces:
GroovydocHolder<AnnotatedNode>,MethodCall,NodeMetaDataHandler
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).-
Field Summary
FieldsFields inherited from class org.codehaus.groovy.ast.expr.Expression
EMPTY_ARRAYFields inherited from interface groovy.lang.groovydoc.GroovydocHolder
DOC_COMMENT -
Constructor Summary
ConstructorsConstructorDescriptionMethodCallExpression(Expression objectExpression, String method, Expression arguments) Creates a method call with a string method name.MethodCallExpression(Expression objectExpression, Expression method, Expression arguments) Creates a method call with an expression-based method name (supports dynamic method names). -
Method Summary
Modifier and TypeMethodDescriptionReturns the method arguments as aTupleExpression.Returns the generic type parameters for this method call, if specified.Returns the method name or reference expression.Returns the method name as a string if it is a constant static name, or null if the method name is computed dynamically.Returns the targetMethodNodeif this method call has been resolved to a specific method.Returns the object (receiver) on which this method is invoked.Returns the receiver as anASTNode.getText()Returns a human-readable text representation of this AST node.booleanIndicates whether this method call implicitly refers to the current object (this).booleanisSafe()Indicates whether this is a safe method call.booleanIndicates whether this method call uses spread-safe navigation.booleanIndicates whether generic type parameters are specified for this method call.voidsetArguments(Expression arguments) Sets the method arguments.voidsetGenericsTypes(GenericsType[] genericsTypes) Sets the generic type parameters for this method call.voidsetImplicitThis(boolean implicitThis) Sets whether this method call implicitly refers to the current object.voidsetMethod(Expression method) Sets the method name or reference expression.voidSets a direct method target for this method call.voidsetObjectExpression(Expression objectExpression) Sets the object (receiver) on which this method is invoked.voidsetSafe(boolean safe) Sets whether this should be a safe method call.voidsetSourcePosition(ASTNode node) Sets the source position information using another ASTNode as reference.voidsetSpreadSafe(boolean value) Sets whether this method call should use spread-safe navigation.toString()transformExpression(ExpressionTransformer transformer) Transforms this expression and any nested expressions according to the provided transformer.voidvisit(GroovyCodeVisitor visitor) Accepts a code visitor for AST traversal and transformation.Methods inherited from class org.codehaus.groovy.ast.expr.Expression
getType, setType, transformExpressions, transformExpressionsMethods inherited from class org.codehaus.groovy.ast.AnnotatedNode
addAnnotation, addAnnotation, addAnnotations, getAnnotations, getAnnotations, getDeclaringClass, getGroovydoc, getInstance, hasNoRealSourcePosition, isSynthetic, setDeclaringClass, setHasNoRealSourcePosition, setSyntheticMethods inherited from class org.codehaus.groovy.ast.ASTNode
copyNodeMetaData, getColumnNumber, getLastColumnNumber, getLastLineNumber, getLineNumber, getMetaDataMap, setColumnNumber, setLastColumnNumber, setLastLineNumber, setLineNumber, setMetaDataMapMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface org.codehaus.groovy.ast.NodeMetaDataHandler
copyNodeMetaData, getNodeMetaData, getNodeMetaData, getNodeMetaData, newMetaDataMap, putNodeMetaData, removeNodeMetaData, setNodeMetaData
-
Field Details
-
NO_ARGUMENTS
-
-
Constructor Details
-
MethodCallExpression
Creates a method call with a string method name.- Parameters:
objectExpression- the receiver object on which the method is invoked; must not be nullmethod- the name of the method as aString; must not be nullarguments- the method arguments as anExpression; will be wrapped in aTupleExpressionif not already one
-
MethodCallExpression
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 nullmethod- the method name as anExpression, which may be aConstantExpressionfor static names or other expression types for dynamic names; must not be nullarguments- the method arguments as anExpression; will be wrapped in aTupleExpressionif not already one
-
-
Method Details
-
visit
Description copied from class:ASTNodeAccepts 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:
visitin classASTNode- Parameters:
visitor- theGroovyCodeVisitorto process this node
-
transformExpression
Description copied from class:ExpressionTransforms 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:
transformExpressionin classExpression- Parameters:
transformer- theExpressionTransformerto apply- Returns:
- a transformed copy of this expression (or this expression itself if no changes are needed)
-
getArguments
Returns the method arguments as aTupleExpression.- Specified by:
getArgumentsin interfaceMethodCall- Returns:
- the method arguments; typically a
TupleExpression, never null
-
setArguments
Sets the method arguments. If the provided expression is not aTupleExpression, it will be automatically wrapped in one for internal consistency.- Parameters:
arguments- the method arguments as anExpression; must not be null
-
getMethod
Returns the method name or reference expression. This may be aConstantExpressionfor static method names or other expression types for dynamically computed names.- Returns:
- the method name
Expression; never null
-
setMethod
Sets the method name or reference expression.- Parameters:
method- the method name as anExpression; must not be null
-
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:
getMethodAsStringin interfaceMethodCall- Returns:
- the method name as a
Stringif this is a static method call, or null if the method name is a dynamic expression
-
getObjectExpression
Returns the object (receiver) on which this method is invoked.- Returns:
- the receiver
Expression; never null
-
setObjectExpression
Sets the object (receiver) on which this method is invoked.- Parameters:
objectExpression- the receiverExpression; must not be null
-
getReceiver
Returns the receiver as anASTNode. This is used by theMethodCallinterface.- Specified by:
getReceiverin interfaceMethodCall- Returns:
- the receiver
ASTNode
-
getText
Description copied from class:ASTNodeReturns 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:
getTextin interfaceMethodCall- Overrides:
getTextin classASTNode- 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()vsobj.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
Returns the generic type parameters for this method call, if specified. Used for generic method invocations likeobj.<String>method().- Returns:
- an array of
GenericsTypeparameters, or null if no generics are specified
-
setGenericsTypes
Sets the generic type parameters for this method call.- Parameters:
genericsTypes- an array ofGenericsTypeparameters; 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
Returns the targetMethodNodeif 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
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 targetMethodNode; may be null to clear the target
-
setSourcePosition
Description copied from class:ASTNodeSets 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:
setSourcePositionin classASTNode- Parameters:
node- the reference node providing position information
-
toString
-