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.
| Modifiers | Name | Description |
|---|---|---|
protected WriterController |
controller |
The controller coordinating all bytecode writers for the current class. |
| Constructor and description |
|---|
SwitchExpressionWriter(WriterController controller)Creates a switch-expression writer with the given controller. |
| Type Params | Return Type | Name and description |
|---|---|---|
|
protected static int |
enumConstantCount(ClassNode enumType)Counts the enum constants of enumType. |
|
protected static String |
enumConstantName(Expression expression, ClassNode enumType)The enum-constant name of expression when it denotes a constant of
enumType. |
|
protected static Integer |
intConstant(Expression expression)The int value of a case label that is an int, byte,
short or char constant. |
|
protected static boolean |
isIntegralType(ClassNode type)Whether type is int, byte, short or char.
|
|
protected static boolean |
isIntegralWrapper(ClassNode type)Whether type is Integer, Byte, Short or
Character. |
|
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)The string value of a case label that is a string constant. |
|
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)The enum type behind 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). |
|
public void |
writeSwitchExpression(SwitchExpression expression)Generates bytecode for a switch expression. |
|
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. |
The controller coordinating all bytecode writers for the current class.
Creates a switch-expression writer with the given controller.
controller - the writer controller Counts the enum constants of enumType. Fields of the redirect that
are enum constants are counted first. When that count is zero and the type
is resolved, Class.getEnumConstants is used, and a null
return from that call counts as zero.
enumType - the enum whose constants are counted The enum-constant name of expression when it denotes a constant of
enumType. Accepts EnumType.NAME, an unqualified name of
that constant, or a constant expression holding the enum value.
expression - the case labelenumType - the selector enum typenull when the label is not a constant of enumType The int value of a case label that is an int, byte,
short or char constant.
expression - the case labelnull when the label is not such a constant Whether type is int, byte, short or char.
A null type is not integral.
type - the type to testtrue for those four primitives Whether type is Integer, Byte, Short or
Character. A null type is not a wrapper.
type - the type to testtrue for those four wrappers Returns the type to store the selector under. Dynamic compilation boxes
so the subsequent isCase tests see a reference.
Resolves the type left on the operand stack at the switch-expression join.
The string value of a case label that is a string constant.
expression - the case labelnull when the label is not a string constant Throws IncompatibleClassChangeError for an enum constant added
after an exhaustive switch expression was compiled.
Throws IllegalStateException naming the unmatched selector.
The enum type behind type. A null type yields null.
An enum is returned as itself; otherwise the redirect is returned when that
redirect is an enum.
type - the selector typenull when type is not an enumEmits the default arm, or an unmatched-selector throw.
completeEnum - true when a complete enum switch should throw
IncompatibleClassChangeError instead of
IllegalStateException Emits one isCase test. Shared with StatementWriter via
BinaryExpressionHelper.writeIsCase.
Attempts a specialized dispatch (tableswitch / lookupswitch). The base writer has none; static compilation overrides this.
true if specialized bytecode was emittedGenerates bytecode for a switch expression. The result is left on the operand stack with the expression's resolved type.
expression - the switch expression to compile 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.
statement - the yield statement to compileCopyright © 2003-2026 The Apache Software Foundation. All rights reserved.