Package org.codehaus.groovy.classgen.asm
Class InvocationWriter
java.lang.Object
org.codehaus.groovy.classgen.asm.InvocationWriter
- Direct Known Subclasses:
InvokeDynamicWriter,StaticInvocationWriter
Handles method and constructor invocations, generating appropriate bytecode.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final MethodCallerMethod caller for casting an array of arguments to a varargs array.protected final WriterControllerThe controller coordinating all bytecode writers for the current class.protected ExpressionThe expression currently being compiled as a call, used for optimizations.static final MethodCallerDeprecated.Use direct closure invocation insteadstatic final MethodCallerMultiAdapterMulti-adapter for genericinvokeMethodcall-site variants.static final MethodCallerMultiAdapterMulti-adapter forinvokeMethodOnCurrentcall-site variants.static final MethodCallerMultiAdapterMulti-adapter forinvokeMethodOnSupercall-site variants.static final MethodCallerMultiAdapterMulti-adapter forinvokeStaticMethodcall-site variants. -
Constructor Summary
ConstructorsConstructorDescriptionInvocationWriter(WriterController controller) Creates an invocation writer with the given controller. -
Method Summary
Modifier and TypeMethodDescriptionvoidUnboxes a non-primitive value on the operand stack to a primitiveboolean.voidcastToNonPrimitiveIfNecessary(ClassNode sourceType, ClassNode targetType) Converts sourceType to a non-primitive by using Groovy casting.voidEmits a Groovyascoercion fromfromtotarget.protected voidfinnishConstructorCall(ConstructorNode cn, String ownerDescriptor, int argsToRemove) Emits theINVOKESPECIAL<init>instruction and updates the operand stack after a constructor call set up byprepareConstructorCall(org.codehaus.groovy.ast.ConstructorNode).protected StringgetMethodName(Expression message) Extracts the constant method name string from a message expression.protected voidloadArguments(List<Expression> arguments, Parameter[] parameters) Evaluates and loads each argument expression, handling varargs packing.static ArgumentListExpressionmakeArgumentList(Expression arguments) Converts an expression to an argument list.protected booleanmakeCachedCall(Expression origin, ClassExpression sender, Expression receiver, Expression message, Expression arguments, MethodCallerMultiAdapter adapter, boolean safe, boolean spreadSafe, boolean implicitThis, boolean containsSpreadExpression) Attempts to emit a call-site-cached invocation (or invokedynamic).protected voidmakeCall(Expression origin, ClassExpression sender, Expression receiver, Expression message, Expression arguments, MethodCallerMultiAdapter adapter, boolean safe, boolean spreadSafe, boolean implicitThis) Generates bytecode for a method invocation, choosing among direct call, call-site caching, and uncached (ScriptBytecodeAdapter) strategies.voidmakeCall(Expression origin, Expression receiver, Expression message, Expression arguments, MethodCallerMultiAdapter adapter, boolean safe, boolean spreadSafe, boolean implicitThis) Generates bytecode for a method call.protected booleanmakeClassForNameCall(Expression origin, Expression receiver, Expression message, Expression arguments) if Class.forName(x) is recognized, make a direct method callprotected booleanmakeDirectCall(Expression origin, Expression receiver, Expression message, Expression arguments, MethodCallerMultiAdapter adapter, boolean implicitThis, boolean containsSpreadExpression) Attempts to emit a direct (statically resolved) method call.final voidmakeSingleArgumentCall(Expression receiver, String message, Expression arguments) Delegates tomakeSingleArgumentCall(Expression, String, Expression, boolean)withsafe = false.voidmakeSingleArgumentCall(Expression receiver, String message, Expression arguments, boolean safe) Generates a single-argument method call via the call-site writer.protected voidmakeUncachedCall(Expression origin, ClassExpression sender, Expression receiver, Expression message, Expression arguments, MethodCallerMultiAdapter adapter, boolean safe, boolean spreadSafe, boolean implicitThis, boolean containsSpreadExpression) Emits an uncached invocation viaScriptBytecodeAdapter.protected StringEmits theNEWandDUPinstructions for a constructor call and returns the internal class name of the type being constructed.protected booleanEmits a constructor call for an anonymous inner class (AIC).protected booleanwriteDirectMethodCall(MethodNode target, boolean implicitThis, Expression receiver, TupleExpression args) Emits a direct (INVOKE*) bytecode call totargetwhen static type information is available.voidGenerates bytecode for a constructor call expression, choosing the appropriate strategy (direct, AIC, or normal call-site).voidGenerates bytecode for a standard (non-static, non-constructor) method call expression.voidGenerates bytecode for a static method call expression.protected voidEmits a normal (non-direct, non-AIC) constructor call via the call-site writer.voidGenerates bytecode for athis()orsuper()constructor call at the start of a constructor body.
-
Field Details
-
invokeMethodOnCurrent
Multi-adapter forinvokeMethodOnCurrentcall-site variants. -
invokeMethodOnSuper
Multi-adapter forinvokeMethodOnSupercall-site variants. -
invokeMethod
Multi-adapter for genericinvokeMethodcall-site variants. -
invokeStaticMethod
Multi-adapter forinvokeStaticMethodcall-site variants. -
invokeClosureMethod
Deprecated.Use direct closure invocation instead -
castToVargsArray
Method caller for casting an array of arguments to a varargs array. -
controller
The controller coordinating all bytecode writers for the current class. -
currentCall
The expression currently being compiled as a call, used for optimizations.
-
-
Constructor Details
-
InvocationWriter
Creates an invocation writer with the given controller.- Parameters:
controller- the writer controller
-
-
Method Details
-
makeCall
public void makeCall(Expression origin, Expression receiver, Expression message, Expression arguments, MethodCallerMultiAdapter adapter, boolean safe, boolean spreadSafe, boolean implicitThis) Generates bytecode for a method call.- Parameters:
origin- the original expressionreceiver- the receiver expressionmessage- the method name expressionarguments- the arguments expressionadapter- the method caller adaptersafe- whether to use safe navigationspreadSafe- whether to use spread-safe navigationimplicitThis- whether the receiver is implicit 'this'
-
makeCall
protected void makeCall(Expression origin, ClassExpression sender, Expression receiver, Expression message, Expression arguments, MethodCallerMultiAdapter adapter, boolean safe, boolean spreadSafe, boolean implicitThis) Generates bytecode for a method invocation, choosing among direct call, call-site caching, and uncached (ScriptBytecodeAdapter) strategies.- Parameters:
origin- the original expression triggering this callsender- the class from which the call is dispatchedreceiver- the receiver expressionmessage- the method name expressionarguments- the arguments expressionadapter- the multi-adapter selecting the correct runtime methodsafe- whether to use safe navigation (?.)spreadSafe- whether to use spread-safe navigation (*?.)implicitThis- whether the receiver is implicitthis
-
writeDirectMethodCall
protected boolean writeDirectMethodCall(MethodNode target, boolean implicitThis, Expression receiver, TupleExpression args) Emits a direct (INVOKE*) bytecode call totargetwhen static type information is available. Returnsfalseif a direct call cannot be made.- Parameters:
target- the resolved method node to call directlyimplicitThis- whether the receiver is implicitthisreceiver- the receiver expression (may benull)args- the argument tuple- Returns:
trueif the call was emitted,falseotherwise
-
loadArguments
Evaluates and loads each argument expression, handling varargs packing.- Parameters:
arguments- the argument expressions to evaluateparameters- the declared parameters of the target method
-
makeDirectCall
protected boolean makeDirectCall(Expression origin, Expression receiver, Expression message, Expression arguments, MethodCallerMultiAdapter adapter, boolean implicitThis, boolean containsSpreadExpression) Attempts to emit a direct (statically resolved) method call. Returnsfalseif no direct call is possible, causing the caller to fall through to the cached or uncached path.- Parameters:
origin- the original call expressionreceiver- the receiver expressionmessage- the method name expressionarguments- the arguments expressionadapter- the method caller adapterimplicitThis- whether the receiver is implicitthiscontainsSpreadExpression- whether any argument uses spread (*)- Returns:
trueif the call was emitted directly
-
makeCachedCall
protected boolean makeCachedCall(Expression origin, ClassExpression sender, Expression receiver, Expression message, Expression arguments, MethodCallerMultiAdapter adapter, boolean safe, boolean spreadSafe, boolean implicitThis, boolean containsSpreadExpression) Attempts to emit a call-site-cached invocation (or invokedynamic). Returnsfalseif the call cannot be cached, causing the caller to fall through to the uncached path.- Parameters:
origin- the original call expressionsender- the class from which the call is dispatchedreceiver- the receiver expressionmessage- the method name expressionarguments- the arguments expressionadapter- the method caller adaptersafe- whether to use safe navigationspreadSafe- whether to use spread-safe navigationimplicitThis- whether the receiver is implicitthiscontainsSpreadExpression- whether any argument uses spread- Returns:
trueif a cached call was emitted
-
makeUncachedCall
protected void makeUncachedCall(Expression origin, ClassExpression sender, Expression receiver, Expression message, Expression arguments, MethodCallerMultiAdapter adapter, boolean safe, boolean spreadSafe, boolean implicitThis, boolean containsSpreadExpression) Emits an uncached invocation viaScriptBytecodeAdapter. Used as the fallback when neither direct nor cached dispatch is applicable.- Parameters:
origin- the original call expressionsender- the class from which the call is dispatchedreceiver- the receiver expressionmessage- the method name expressionarguments- the arguments expressionadapter- the method caller adaptersafe- whether to use safe navigationspreadSafe- whether to use spread-safe navigationimplicitThis- whether the receiver is implicitthiscontainsSpreadExpression- whether any argument uses spread
-
makeClassForNameCall
protected boolean makeClassForNameCall(Expression origin, Expression receiver, Expression message, Expression arguments) if Class.forName(x) is recognized, make a direct method call -
makeArgumentList
Converts an expression to an argument list.- Returns:
argumentsif already an argument list or an argument list of the expression or expressions (in case of a tuple expression).- Since:
- 2.0.0
-
getMethodName
Extracts the constant method name string from a message expression. Returnsnullif the expression is not a constant string.- Parameters:
message- the method name expression- Returns:
- the method name string, or
nullif not determinable
-
writeInvokeMethod
Generates bytecode for a standard (non-static, non-constructor) method call expression.- Parameters:
call- the method call expression to compile
-
writeInvokeStaticMethod
Generates bytecode for a static method call expression.- Parameters:
call- the static method call expression to compile
-
writeInvokeConstructor
Generates bytecode for a constructor call expression, choosing the appropriate strategy (direct, AIC, or normal call-site).- Parameters:
call- the constructor call expression to compile
-
prepareConstructorCall
Emits theNEWandDUPinstructions for a constructor call and returns the internal class name of the type being constructed.- Parameters:
cn- the constructor node whose declaring class is being instantiated- Returns:
- the JVM internal name of the class being constructed
-
finnishConstructorCall
Emits theINVOKESPECIAL<init>instruction and updates the operand stack after a constructor call set up byprepareConstructorCall(org.codehaus.groovy.ast.ConstructorNode).- Parameters:
cn- the constructor node being invokedownerDescriptor- the JVM internal name of the owning classargsToRemove- the number of argument entries to pop from the tracked stack
-
writeNormalConstructorCall
Emits a normal (non-direct, non-AIC) constructor call via the call-site writer.- Parameters:
call- the constructor call expression to compile
-
writeAICCall
Emits a constructor call for an anonymous inner class (AIC). Returnsfalseif the call is not for an AIC.- Parameters:
call- the constructor call expression to compile- Returns:
trueif an AIC call was emitted
-
makeSingleArgumentCall
Delegates tomakeSingleArgumentCall(Expression, String, Expression, boolean)withsafe = false.- Parameters:
receiver- the receiver expressionmessage- the method namearguments- the single argument expression
-
makeSingleArgumentCall
public void makeSingleArgumentCall(Expression receiver, String message, Expression arguments, boolean safe) Generates a single-argument method call via the call-site writer.- Parameters:
receiver- the receiver expressionmessage- the method namearguments- the single argument expressionsafe- whether to use safe navigation (?.)
-
writeSpecialConstructorCall
Generates bytecode for athis()orsuper()constructor call at the start of a constructor body.- Parameters:
call- the constructor call expression representingthis()orsuper()
-
castToNonPrimitiveIfNecessary
Converts sourceType to a non-primitive by using Groovy casting. sourceType might be a primitive This might be done using SBA#castToType -
castNonPrimitiveToBool
Unboxes a non-primitive value on the operand stack to a primitiveboolean.- Parameters:
last- the type currently on top of the operand stack
-
coerce
Emits a Groovyascoercion fromfromtotarget.- Parameters:
from- the source type currently on the operand stacktarget- the target type to coerce to
-