Package org.apache.groovy.ast.tools
Class SwitchExpressionUtils
java.lang.Object
org.apache.groovy.ast.tools.SwitchExpressionUtils
Shared classification of switch-expression labels for static compilation.
Intrinsic dispatch (
tableswitch / lookupswitch / enum name)
is used only when every case label is a compile-time constant of a type
javac would switch on. Mixed labels fall back to isCase.- Since:
- 6.0.0
-
Method Summary
Modifier and TypeMethodDescriptionstatic StringenumConstantName(Expression expression, ClassNode enumType) Extracts the enum constant name from a case label ofenumType, ornull.static IntegerintConstant(Expression expression) Extracts an int-family constant from a case label, ornull.static booleanisIntegralSelector(ClassNode type) True forbyte,short,char,intand their wrappers.static booleanisIntegralType(ClassNode type) True for the integral primitive typesjavacswitches on.static booleanisIntegralWrapper(ClassNode type) True for the wrappers of the integral primitive typesjavacswitches on.static booleanisOptimizedEnumSwitch(ClassNode selectorType, List<CaseStatement> caseStatements) True when the selector is an enum and every label names a constant of that enum.static booleanisOptimizedIntSwitch(ClassNode selectorType, List<CaseStatement> caseStatements) True when the selector is an integral primitive or wrapper and every label is an int-family constant.static booleanisOptimizedStringSwitch(ClassNode selectorType, List<CaseStatement> caseStatements) True when the selector isStringand every label is a string constant.static booleanisOptimizedSwitch(ClassNode selectorType, List<CaseStatement> caseStatements) True when the selector type and every case label permit tableswitch, lookupswitch, or enum-name dispatch.static StringstringConstant(Expression expression) Extracts a string constant from a case label, ornull.static ClassNodeunwrapEnumType(ClassNode type) Returns the enum type behindtype, ornullif it is not an enum.
-
Method Details
-
isOptimizedSwitch
True when the selector type and every case label permit tableswitch, lookupswitch, or enum-name dispatch. An empty case list, anulllabel, or a single non-constant label (a range, a variable, …) makes the whole switch anisCaseswitch.- Parameters:
selectorType- the inferred type of the selectorcaseStatements- the case arms- Returns:
trueif codegen can emit an intrinsic switch
-
isOptimizedIntSwitch
public static boolean isOptimizedIntSwitch(ClassNode selectorType, List<CaseStatement> caseStatements) True when the selector is an integral primitive or wrapper and every label is an int-family constant.- Parameters:
selectorType- the inferred type of the selectorcaseStatements- the case arms- Returns:
trueif int tableswitch / lookupswitch applies
-
isOptimizedStringSwitch
public static boolean isOptimizedStringSwitch(ClassNode selectorType, List<CaseStatement> caseStatements) True when the selector isStringand every label is a string constant.- Parameters:
selectorType- the inferred type of the selectorcaseStatements- the case arms- Returns:
trueif string lookupswitch applies
-
isOptimizedEnumSwitch
public static boolean isOptimizedEnumSwitch(ClassNode selectorType, List<CaseStatement> caseStatements) True when the selector is an enum and every label names a constant of that enum.- Parameters:
selectorType- the inferred type of the selectorcaseStatements- the case arms- Returns:
trueif enum-name dispatch applies
-
isIntegralSelector
True forbyte,short,char,intand their wrappers.- Parameters:
type- the selector type- Returns:
trueif the type can feed a JVM int switch
-
isIntegralType
True for the integral primitive typesjavacswitches on.- Parameters:
type- the type to test- Returns:
trueif the type isint,byte,short, orchar
-
isIntegralWrapper
True for the wrappers of the integral primitive typesjavacswitches on.- Parameters:
type- the type to test- Returns:
trueif the type isInteger,Byte,Short, orCharacter
-
intConstant
Extracts an int-family constant from a case label, ornull.- Parameters:
expression- the case label- Returns:
- the constant int value, or
nullif the label is not an int-family constant
-
stringConstant
Extracts a string constant from a case label, ornull.- Parameters:
expression- the case label- Returns:
- the string value, or
nullif the label is not a string constant
-
unwrapEnumType
Returns the enum type behindtype, ornullif it is not an enum.- Parameters:
type- the selector type- Returns:
- the enum
ClassNode, ornull
-
enumConstantName
Extracts the enum constant name from a case label ofenumType, ornull. AcceptsEnumType.NAME, an unqualified name that resolves to that constant, or a constant expression holding the enum value.- Parameters:
expression- the case labelenumType- the selector enum type- Returns:
- the constant name, or
nullif the label is not a constant ofenumType
-