Class SwitchExpressionUtils

java.lang.Object
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
  • Method Details

    • isOptimizedSwitch

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

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

      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
    • isIntegralType

      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
    • isIntegralWrapper

      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
    • intConstant

      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
    • stringConstant

      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
    • unwrapEnumType

      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
    • enumConstantName

      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