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).
| Modifiers | Name | Description |
|---|---|---|
static Expression |
NO_ARGUMENTS |
| Fields inherited from class | Fields |
|---|---|
class Expression |
EMPTY_ARRAY |
| Constructor and description |
|---|
MethodCallExpression(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). |
| Type Params | Return Type | Name and description |
|---|---|---|
|
public Expression |
getArguments()Returns the method arguments as a TupleExpression. |
|
public GenericsType[] |
getGenericsTypes()Returns the generic type parameters for this method call, if specified. |
|
public Expression |
getMethod()Returns the method name or reference expression. |
|
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. |
|
public MethodNode |
getMethodTarget()Returns the target MethodNode if this method call has been resolved to a specific method. |
|
public Expression |
getObjectExpression()Returns the object (receiver) on which this method is invoked. |
|
public ASTNode |
getReceiver()Returns the receiver as an ASTNode. |
|
public String |
getText() |
|
public boolean |
isImplicitThis()Indicates whether this method call implicitly refers to the current object ( this).
|
|
public boolean |
isSafe()Indicates whether this is a safe method call. |
|
public boolean |
isSpreadSafe()Indicates whether this method call uses spread-safe navigation. |
|
public boolean |
isUsingGenerics()Indicates whether generic type parameters are specified for this method call. |
|
public void |
setArguments(Expression arguments)Sets the method arguments. |
|
public void |
setGenericsTypes(GenericsType[] genericsTypes)Sets the generic type parameters for this method call. |
|
public void |
setImplicitThis(boolean implicitThis)Sets whether this method call implicitly refers to the current object. |
|
public void |
setMethod(Expression method)Sets the method name or reference expression. |
|
public void |
setMethodTarget(MethodNode mn)Sets a direct method target for this method call. |
|
public void |
setObjectExpression(Expression objectExpression)Sets the object (receiver) on which this method is invoked. |
|
public void |
setSafe(boolean safe)Sets whether this should be a safe method call. |
|
public void |
setSourcePosition(ASTNode node) |
|
public void |
setSpreadSafe(boolean value)Sets whether this method call should use spread-safe navigation. |
|
public String |
toString() |
|
public Expression |
transformExpression(ExpressionTransformer transformer) |
|
public void |
visit(GroovyCodeVisitor visitor) |
| Methods inherited from class | Name |
|---|---|
class Expression |
getType, setType, transformExpression, transformExpressions, transformExpressions |
class AnnotatedNode |
addAnnotation, addAnnotation, addAnnotations, getAnnotations, getAnnotations, getDeclaringClass, getGroovydoc, getInstance, hasNoRealSourcePosition, isSynthetic, setDeclaringClass, setHasNoRealSourcePosition, setSynthetic |
class ASTNode |
copyNodeMetaData, getColumnNumber, getLastColumnNumber, getLastLineNumber, getLineNumber, getMetaDataMap, getText, setColumnNumber, setLastColumnNumber, setLastLineNumber, setLineNumber, setMetaDataMap, setSourcePosition, visit |
Creates a method call with a string method name.
objectExpression - the receiver object on which the method is invoked; must not be nullmethod - the name of the method as a String; must not be nullarguments - the method arguments as an Expression; will be wrapped in a
TupleExpression if not already oneCreates a method call with an expression-based method name (supports dynamic method names).
objectExpression - the receiver object on which the method is invoked; must not be nullmethod - the method name as an Expression, which may be a ConstantExpression
for static names or other expression types for dynamic names; must not be nullarguments - the method arguments as an Expression; will be wrapped in a
TupleExpression if not already oneReturns the method arguments as a TupleExpression.
Returns the generic type parameters for this method call, if specified.
Used for generic method invocations like obj.<String>method().
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 as a string if it is a constant static name, or null if the method name is computed dynamically.
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 object (receiver) on which this method is invoked.
Returns the receiver as an ASTNode. This is used by the MethodCall interface.
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()).
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()).
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.
Indicates whether generic type parameters are specified for this method call.
Sets the method arguments. If the provided expression is not a TupleExpression, it will be automatically wrapped in one for internal consistency.
arguments - the method arguments as an Expression; must not be nullSets the generic type parameters for this method call.
genericsTypes - an array of GenericsType parameters; may be nullSets whether this method call implicitly refers to the current object.
implicitThis - true if this is an implicit this call; false otherwiseSets the method name or reference expression.
method - the method name as an Expression; must not be nullSets 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.
mn - the target MethodNode; may be null to clear the targetSets the object (receiver) on which this method is invoked.
objectExpression - the receiver Expression; must not be nullSets whether this should be a safe method call.
safe - true for safe navigation; false otherwiseSets whether this method call should use spread-safe navigation.
value - true to enable spread-safe navigation; false otherwiseCopyright © 2003-2026 The Apache Software Foundation. All rights reserved.