Package: org.codehaus.groovy.classgen.asm

## [Java] Class ClosureWriter

- org.codehaus.groovy.classgen.asm.ClosureWriter

-     public class ClosureWriter
      extends Object

  Generates bytecode for closure expressions.

- - ### Nested Class Summary

    Nested classes
    Modifiers | Name | Description
    --- | --- | ---
    `protected static class` | `ClosureWriter.CorrectAccessedVariableVisitor` | Visitor that rewrites VariableExpression nodes whose accessed variable is a FieldNode in an outer class to instead reference the corresponding field in the generated closure inner class.
    `protected interface` | `ClosureWriter.UseExistingReference` | Marker interface for using existing reference.

  - ### Field Summary

    Fields
    Modifiers | Name | Description
    --- | --- | ---
    `static String` | `OUTER_INSTANCE` | Field name for the outer instance reference.
    `static String` | `THIS_OBJECT` | Field name for the this object reference.
    `protected WriterController` | `controller` | The controller coordinating all bytecode writers for the current class.

  - ### Constructor Summary

    Constructors
    Constructor and description
    `ClosureWriter(WriterController controller)` Creates a closure writer with the given controller.

  - ### Methods Summary

    Methods
    Type Params | Return Type | Name and description
    --- | --- | ---
    `` | `protected ConstructorNode` | `addConstructor(ClosureExpression expression, Parameter[] localVariableParams, InnerClassNode answer, BlockStatement block)` Adds a synthetic `public` constructor to the closure inner class that accepts the outer instance, `this` object, and all captured local variable references.
    `` | `protected void` | `addFieldsForLocalVariables(InnerClassNode closureClass, Parameter[] localVariableParams)` Adds synthetic private fields to the closure inner class for each captured local variable parameter, promoting them to `Reference` holders.
    `` | `public boolean` | `addGeneratedClosureConstructorCall(ConstructorCallExpression call)` Emits a `super(outerInstance, thisObject)` constructor call for a generated closure class.
    `` | `protected void` | `addSerialVersionUIDField(ClassNode classNode)` Adds a synthetic `serialVersionUID` field to the closure class, derived from a hash of the class name.
    `` | `protected BlockStatement` | `createBlockStatementForConstructor(ClosureExpression expression, ClassNode outerClass, ClassNode thisClassNode)` Creates the block statement for the closure's synthetic constructor, setting up the `super(outerInstance, thisObject)` call and captured variable references.
    `` | `protected ClassNode` | `createClosureClass(ClosureExpression expression, int modifiers)` Creates a new inner class node representing the compiled form of a closure expression.
    `` | `protected Parameter[]` | `getClosureSharedVariables(ClosureExpression expression)` Collects the closure-shared local variables referenced by a closure expression as an array of Parameters, using the type chooser to infer each variable's type.
    `` | `public ClassNode` | `getOrAddClosureClass(ClosureExpression expression, int modifiers)` Returns the existing generated class for a closure expression, or creates and registers a new one if none exists yet.
    `` | `public static void` | `loadReference(String name, WriterController controller)` Loads a closure-shared variable reference onto the operand stack, looking it up as a field, local variable, or outer closure field as appropriate.
    `` | `protected void` | `loadThis()` Loads the effective `this` reference onto the operand stack — either the actual receiver for a regular method, or the result of `getThisObject()` for a generated closure/lambda.
    `` | `protected static void` | `removeInitialValues(Parameter[] params)` Strips initial-value expressions from parameters that are closure-shared, ensuring the closure constructor is not duplicated.
    `` | `public void` | `writeClosure(ClosureExpression expression)` Generates bytecode for a closure expression.

  - ### Inherited Methods Summary

    Inherited Methods
    Methods inherited from class | Name
    --- | ---
    `class Object` | `equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### public static final String OUTER_INSTANCE

      Field name for the outer instance reference.

    - #### public static final String THIS_OBJECT

      Field name for the this object reference.

    - #### protected final WriterController controller

      The controller coordinating all bytecode writers for the current class.

  - ### Constructor Detail

    - #### public ClosureWriter(WriterController controller)

      Creates a closure writer with the given controller.

      Parameters:
        `controller` - the writer controller

  - ### Method Detail

    - #### protected ConstructorNode addConstructor(ClosureExpression expression, Parameter[] localVariableParams, InnerClassNode answer, BlockStatement block)

      Adds a synthetic `public` constructor to the closure inner class that accepts the outer instance, `this` object, and all captured local variable references.

      Parameters:
        `expression` - the closure expression
        `localVariableParams` - parameters for closure-shared local variables
        `answer` - the closure inner class node
        `block` - the constructor body
      Returns:
        the created constructor node

    - #### protected void addFieldsForLocalVariables(InnerClassNode closureClass, Parameter[] localVariableParams)

      Adds synthetic private fields to the closure inner class for each captured local variable parameter, promoting them to `Reference` holders.

      Parameters:
        `closureClass` - the closure inner class node
        `localVariableParams` - the closure-shared local variable parameters

    - #### public boolean addGeneratedClosureConstructorCall(ConstructorCallExpression call)

      Emits a `super(outerInstance, thisObject)` constructor call for a generated closure class. Returns `false` if the current class is not a generated closure.

      Parameters:
        `call` - the constructor call expression representing `super(...)`
      Returns:
        `true` if the closure constructor call was emitted

    - #### protected void addSerialVersionUIDField(ClassNode classNode)

      Adds a synthetic `serialVersionUID` field to the closure class, derived from a hash of the class name.

      Parameters:
        `classNode` - the closure class node to add the field to

    - #### protected BlockStatement createBlockStatementForConstructor(ClosureExpression expression, ClassNode outerClass, ClassNode thisClassNode)

      Creates the block statement for the closure's synthetic constructor, setting up the `super(outerInstance, thisObject)` call and captured variable references.

      Parameters:
        `expression` - the closure expression
        `outerClass` - the class declaring the closure
        `thisClassNode` - the `this` type in scope at the closure declaration site
      Returns:
        the block statement for the constructor body

    - #### protected ClassNode createClosureClass(ClosureExpression expression, int modifiers)

      Creates a new inner class node representing the compiled form of a closure expression.

      Parameters:
        `expression` - the closure expression to compile
        `modifiers` - the access modifiers for the generated class
      Returns:
        the newly created closure class node

    - #### protected Parameter[] getClosureSharedVariables(ClosureExpression expression)

      Collects the closure-shared local variables referenced by a closure expression as an array of Parameters, using the type chooser to infer each variable's type.

      Parameters:
        `expression` - the closure expression
      Returns:
        the array of parameters representing captured shared variables

    - #### public ClassNode getOrAddClosureClass(ClosureExpression expression, int modifiers)

      Returns the existing generated class for a closure expression, or creates and registers a new one if none exists yet.

      Parameters:
        `expression` - the closure expression to compile as an inner class
        `modifiers` - the access modifiers for the generated class
      Returns:
        the generated closure class node

    - #### public static void loadReference(String name, WriterController controller)

      Loads a closure-shared variable reference onto the operand stack, looking it up as a field, local variable, or outer closure field as appropriate.

      Parameters:
        `name` - the variable name to load
        `controller` - the writer controller for the enclosing class

    - #### protected void loadThis()

      Loads the effective `this` reference onto the operand stack — either the actual receiver for a regular method, or the result of `getThisObject()` for a generated closure/lambda.

    - #### protected static void removeInitialValues(Parameter[] params)

      Strips initial-value expressions from parameters that are closure-shared, ensuring the closure constructor is not duplicated.

      Parameters:
        `params` - the parameters to mutate in-place

    - #### public void writeClosure(ClosureExpression expression)

      Generates bytecode for a closure expression.

      Parameters:
        `expression` - the closure expression