Package org.codehaus.groovy.classgen.asm
Class SwitchExpressionWriter
java.lang.Object
org.codehaus.groovy.classgen.asm.SwitchExpressionWriter
- Direct Known Subclasses:
StaticTypesSwitchExpressionWriter
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
FieldsModifier and TypeFieldDescriptionprotected final WriterControllerThe controller coordinating all bytecode writers for the current class. -
Constructor Summary
ConstructorsConstructorDescriptionSwitchExpressionWriter(WriterController controller) Creates a switch-expression writer with the given controller. -
Method Summary
Modifier and TypeMethodDescriptionprotected static intenumConstantCount(ClassNode enumType) Counts the enum constants ofenumType.protected static StringenumConstantName(Expression expression, ClassNode enumType) The enum-constant name ofexpressionwhen it denotes a constant ofenumType.protected static IntegerintConstant(Expression expression) The int value of a case label that is anint,byte,shortorcharconstant.protected static booleanisIntegralType(ClassNode type) Whethertypeisint,byte,shortorchar.protected static booleanisIntegralWrapper(ClassNode type) WhethertypeisInteger,Byte,ShortorCharacter.protected ClassNodeprepareSelectorType(OperandStack operandStack) Returns the type to store the selector under.protected ClassNoderesolveResultType(SwitchExpression expression) Resolves the type left on the operand stack at the switch-expression join.protected static StringstringConstant(Expression expression) The string value of a case label that is a string constant.protected voidThrowsIncompatibleClassChangeErrorfor an enum constant added after an exhaustive switch expression was compiled.protected voidthrowUnmatchedSelector(int selectorIndex, ClassNode selectorType) ThrowsIllegalStateExceptionnaming the unmatched selector.protected static ClassNodeunwrapEnumType(ClassNode type) The enum type behindtype.protected voidwriteDefaultOrThrow(SwitchExpression expression, int selectorIndex, ClassNode selectorType, boolean completeEnum) Emits the default arm, or an unmatched-selector throw.protected voidwriteIsCaseComparison(CaseStatement caseStatement, int selectorIndex, ClassNode selectorType) Emits oneisCasetest.protected booleanwriteOptimizedSwitch(SwitchExpression expression, int selectorIndex, ClassNode selectorType) Attempts a specialized dispatch (tableswitch / lookupswitch).voidwriteSwitchExpression(SwitchExpression expression) Generates bytecode for a switch expression.voidwriteYield(YieldStatement statement) Generates bytecode for ayieldstatement: evaluate the operand, cast it to the enclosing switch-expression result type, apply intervening finally blocks, and jump to the expression join point.
-
Field Details
-
controller
The controller coordinating all bytecode writers for the current class.
-
-
Constructor Details
-
SwitchExpressionWriter
Creates a switch-expression writer with the given controller.- Parameters:
controller- the writer controller
-
-
Method Details
-
writeSwitchExpression
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
Generates bytecode for ayieldstatement: 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
Returns the type to store the selector under. Dynamic compilation boxes so the subsequentisCasetests 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:
trueif specialized bytecode was emitted
-
writeIsCaseComparison
protected void writeIsCaseComparison(CaseStatement caseStatement, int selectorIndex, ClassNode selectorType) Emits oneisCasetest. Shared withStatementWriterviaBinaryExpressionHelper.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-truewhen a complete enum switch should throwIncompatibleClassChangeErrorinstead ofIllegalStateException
-
throwUnmatchedSelector
ThrowsIllegalStateExceptionnaming the unmatched selector. -
throwIncompatibleClassChangeError
protected void throwIncompatibleClassChangeError()ThrowsIncompatibleClassChangeErrorfor an enum constant added after an exhaustive switch expression was compiled. -
resolveResultType
Resolves the type left on the operand stack at the switch-expression join. -
isIntegralType
Whethertypeisint,byte,shortorchar. Anulltype is not integral.- Parameters:
type- the type to test- Returns:
truefor those four primitives
-
isIntegralWrapper
WhethertypeisInteger,Byte,ShortorCharacter. Anulltype is not a wrapper.- Parameters:
type- the type to test- Returns:
truefor those four wrappers
-
intConstant
The int value of a case label that is anint,byte,shortorcharconstant.- Parameters:
expression- the case label- Returns:
- that int, or
nullwhen the label is not such a constant
-
stringConstant
The string value of a case label that is a string constant.- Parameters:
expression- the case label- Returns:
- that string, or
nullwhen the label is not a string constant
-
unwrapEnumType
The enum type behindtype. Anulltype yieldsnull. An enum is returned as itself; otherwise the redirect is returned when that redirect is an enum.- Parameters:
type- the selector type- Returns:
- the enum
ClassNode, ornullwhentypeis not an enum
-
enumConstantCount
Counts the enum constants ofenumType. 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 anullreturn from that call counts as zero.- Parameters:
enumType- the enum whose constants are counted- Returns:
- the number of enum constants found
-
enumConstantName
The enum-constant name ofexpressionwhen it denotes a constant ofenumType. AcceptsEnumType.NAME, an unqualified name of that constant, or a constant expression holding the enum value.- Parameters:
expression- the case labelenumType- the selector enum type- Returns:
- the constant name, or
nullwhen the label is not a constant ofenumType
-