Package org.codehaus.groovy.classgen.asm

# Class SwitchExpressionWriter

java.lang.Object
org.codehaus.groovy.classgen.asm.SwitchExpressionWriter

Direct Known Subclasses:
  `StaticTypesSwitchExpressionWriter`

public class SwitchExpressionWriter extends Object
Emits JVM bytecode for a `SwitchExpression`. Completing arms leave the result on the operand stack and jump to a shared join point — the same shape javac uses for JEP 361 switch expressions.

This writer implements sequential `isCase` matching so Class, regex, Collection and Closure cases keep working. Tableswitch / lookupswitch specializations live on the static-compilation subclass.

Since:
  6.0.0

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `protected final WriterController`
  `controller`
  The controller coordinating all bytecode writers for the current class.

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `SwitchExpressionWriter(WriterController controller)`
  Creates a switch-expression writer with the given controller.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `protected static int`
  `enumConstantCount(ClassNode enumType)`
  `protected static String`
  `enumConstantName(Expression expression, ClassNode enumType)`
  `protected static Integer`
  `intConstant(Expression expression)`
  `protected static boolean`
  `isIntegralType(ClassNode type)`
  `protected static boolean`
  `isIntegralWrapper(ClassNode type)`
  `protected ClassNode`
  `prepareSelectorType(OperandStack operandStack)`
  Returns the type to store the selector under.
  `protected ClassNode`
  `resolveResultType(SwitchExpression expression)`
  Resolves the type left on the operand stack at the switch-expression join.
  `protected static String`
  `stringConstant(Expression expression)`
  `protected void`
  `throwIncompatibleClassChangeError()`
  Throws `IncompatibleClassChangeError` for an enum constant added after an exhaustive switch expression was compiled.
  `protected void`
  `throwUnmatchedSelector(int selectorIndex, ClassNode selectorType)`
  Throws `IllegalStateException` naming the unmatched selector.
  `protected static ClassNode`
  `unwrapEnumType(ClassNode type)`
  `protected void`
  `writeDefaultOrThrow(SwitchExpression expression, int selectorIndex, ClassNode selectorType, boolean completeEnum)`
  Emits the default arm, or an unmatched-selector throw.
  `protected void`
  `writeIsCaseComparison(CaseStatement caseStatement, int selectorIndex, ClassNode selectorType)`
  Emits one `isCase` test.
  `protected boolean`
  `writeOptimizedSwitch(SwitchExpression expression, int selectorIndex, ClassNode selectorType)`
  Attempts a specialized dispatch (tableswitch / lookupswitch).
  `void`
  `writeSwitchExpression(SwitchExpression expression)`
  Generates bytecode for a switch expression.
  `void`
  `writeYield(YieldStatement statement)`
  Generates bytecode for a `yield` statement: evaluate the operand, cast it to the enclosing switch-expression result type, apply intervening finally blocks, and jump to the expression join point.

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Field Details

  - ### controller

    protected final WriterController controller
    The controller coordinating all bytecode writers for the current class.

- ## Constructor Details

  - ### SwitchExpressionWriter

    public SwitchExpressionWriter(WriterController controller)
    Creates a switch-expression writer with the given controller.
    Parameters:
      `controller` - the writer controller

- ## Method Details

  - ### writeSwitchExpression

    public void writeSwitchExpression(SwitchExpression expression)
    Generates bytecode for a switch expression. The result is left on the operand stack with the expression's resolved type.
    Parameters:
      `expression` - the switch expression to compile

  - ### writeYield

    public void writeYield(YieldStatement statement)
    Generates bytecode for a `yield` statement: evaluate the operand, cast it to the enclosing switch-expression result type, apply intervening finally blocks, and jump to the expression join point.
    Parameters:
      `statement` - the yield statement to compile

  - ### prepareSelectorType

    protected ClassNode prepareSelectorType(OperandStack operandStack)
    Returns the type to store the selector under. Dynamic compilation boxes so the subsequent `isCase` tests see a reference.

  - ### writeOptimizedSwitch

    protected boolean writeOptimizedSwitch(SwitchExpression expression, int selectorIndex, ClassNode selectorType)
    Attempts a specialized dispatch (tableswitch / lookupswitch). The base writer has none; static compilation overrides this.
    Returns:
      `true` if specialized bytecode was emitted

  - ### writeIsCaseComparison

    protected void writeIsCaseComparison(CaseStatement caseStatement, int selectorIndex, ClassNode selectorType)
    Emits one `isCase` test. Shared with `StatementWriter` via `BinaryExpressionHelper.writeIsCase(int, org.codehaus.groovy.ast.ClassNode, org.codehaus.groovy.ast.expr.Expression)`.

  - ### writeDefaultOrThrow

    protected void writeDefaultOrThrow(SwitchExpression expression, int selectorIndex, ClassNode selectorType, boolean completeEnum)
    Emits the default arm, or an unmatched-selector throw.
    Parameters:
      `completeEnum` - `true` when a complete enum switch should throw `IncompatibleClassChangeError` instead of `IllegalStateException`

  - ### throwUnmatchedSelector

    protected void throwUnmatchedSelector(int selectorIndex, ClassNode selectorType)
    Throws `IllegalStateException` naming the unmatched selector.

  - ### throwIncompatibleClassChangeError

    protected void throwIncompatibleClassChangeError()
    Throws `IncompatibleClassChangeError` for an enum constant added after an exhaustive switch expression was compiled.

  - ### resolveResultType

    protected ClassNode resolveResultType(SwitchExpression expression)
    Resolves the type left on the operand stack at the switch-expression join.

  - ### isIntegralType

    protected static boolean isIntegralType(ClassNode type)

  - ### isIntegralWrapper

    protected static boolean isIntegralWrapper(ClassNode type)

  - ### intConstant

    protected static Integer intConstant(Expression expression)

  - ### stringConstant

    protected static String stringConstant(Expression expression)

  - ### unwrapEnumType

    protected static ClassNode unwrapEnumType(ClassNode type)

  - ### enumConstantCount

    protected static int enumConstantCount(ClassNode enumType)

  - ### enumConstantName

    protected static String enumConstantName(Expression expression, ClassNode enumType)