Package: org.apache.groovy.ast.tools

## [Java] Class SwitchExpressionUtils

- org.apache.groovy.ast.tools.SwitchExpressionUtils

-     public final class SwitchExpressionUtils
      extends Object

  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

- - ### Methods Summary

    Methods
    Type Params | Return Type | Name and description
    --- | --- | ---
    `` | `public static String` | `enumConstantName(Expression expression, ClassNode enumType)` Extracts the enum constant name from a case label of `enumType`, or `null`.
    `` | `public static Integer` | `intConstant(Expression expression)` Extracts an int-family constant from a case label, or `null`.
    `` | `public static boolean` | `isIntegralSelector(ClassNode type)` True for `byte`, `short`, `char`, `int` and their wrappers.
    `` | `public static boolean` | `isIntegralType(ClassNode type)` True for the integral primitive types `javac` switches on.
    `` | `public static boolean` | `isIntegralWrapper(ClassNode type)` True for the wrappers of the integral primitive types `javac` switches on.
    `` | `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.
    `` | `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.
    `` | `public static boolean` | `isOptimizedStringSwitch(ClassNode selectorType, List<CaseStatement> caseStatements)` True when the selector is String and every label is a string constant.
    `` | `public static boolean` | `isOptimizedSwitch(ClassNode selectorType, List<CaseStatement> caseStatements)` True when the selector type and every case label permit tableswitch, lookupswitch, or enum-name dispatch.
    `` | `public static String` | `stringConstant(Expression expression)` Extracts a string constant from a case label, or `null`.
    `` | `public static ClassNode` | `unwrapEnumType(ClassNode type)` Returns the enum type behind `type`, or `null` if it is not an enum.

  - ### Inherited Methods Summary

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

- - ### Method Detail

    - #### public static String enumConstantName(Expression expression, ClassNode enumType)

      Extracts the enum constant name from a case label of `enumType`, or `null`. Accepts `EnumType.NAME`, an unqualified name that resolves to that constant, or a constant expression holding the enum value.

      Parameters:
        `expression` - the case label
        `enumType` - the selector enum type
      Returns:
        the constant name, or `null` if the label is not a constant of `enumType`

    - #### public static Integer intConstant(Expression expression)

      Extracts an int-family constant from a case label, or `null`.

      Parameters:
        `expression` - the case label
      Returns:
        the constant int value, or `null` if the label is not an int-family constant

    - #### public static boolean isIntegralSelector(ClassNode type)

      True for `byte`, `short`, `char`, `int` and their wrappers.

      Parameters:
        `type` - the selector type
      Returns:
        `true` if the type can feed a JVM int switch

    - #### public static boolean isIntegralType(ClassNode type)

      True for the integral primitive types `javac` switches on.

      Parameters:
        `type` - the type to test
      Returns:
        `true` if the type is `int`, `byte`, `short`, or `char`

    - #### public static boolean isIntegralWrapper(ClassNode type)

      True for the wrappers of the integral primitive types `javac` switches on.

      Parameters:
        `type` - the type to test
      Returns:
        `true` if the type is `Integer`, `Byte`, `Short`, or `Character`

    - #### 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 selector
        `caseStatements` - the case arms
      Returns:
        `true` if enum-name dispatch applies

    - #### 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 selector
        `caseStatements` - the case arms
      Returns:
        `true` if int tableswitch / lookupswitch applies

    - #### public static boolean isOptimizedStringSwitch(ClassNode selectorType, List<CaseStatement> caseStatements)

      True when the selector is String and every label is a string constant.

      Parameters:
        `selectorType` - the inferred type of the selector
        `caseStatements` - the case arms
      Returns:
        `true` if string lookupswitch applies

    - #### public static boolean isOptimizedSwitch(ClassNode selectorType, List<CaseStatement> caseStatements)

      True when the selector type and every case label permit tableswitch, lookupswitch, or enum-name dispatch. An empty case list, a `null` label, or a single non-constant label (a range, a variable, …) makes the whole switch an `isCase` switch.

      Parameters:
        `selectorType` - the inferred type of the selector
        `caseStatements` - the case arms
      Returns:
        `true` if codegen can emit an intrinsic switch

    - #### public static String stringConstant(Expression expression)

      Extracts a string constant from a case label, or `null`.

      Parameters:
        `expression` - the case label
      Returns:
        the string value, or `null` if the label is not a string constant

    - #### public static ClassNode unwrapEnumType(ClassNode type)

      Returns the enum type behind `type`, or `null` if it is not an enum.

      Parameters:
        `type` - the selector type
      Returns:
        the enum ClassNode, or `null`