Class SwitchBootstraps

java.lang.Object
java.lang.runtime.SwitchBootstraps

public final class SwitchBootstraps extends Object
Bootstrap methods for linking invokedynamic call sites that implement the selection functionality of the switch statement. The bootstraps take additional static arguments corresponding to the case labels of the switch, implicitly numbered sequentially from [0..N).
Since:
21
  • Method Summary

    Modifier and Type
    Method
    Description
    static CallSite
    enumSwitch(MethodHandles.Lookup lookup, String invocationName, MethodType invocationType, Object... labels)
    Bootstrap method for linking an invokedynamic call site that implements a switch on a target of an enum type.
    static CallSite
    typeSwitch(MethodHandles.Lookup lookup, String invocationName, MethodType invocationType, Object... labels)
    Bootstrap method for linking an invokedynamic call site that implements a switch over a target value.

    Methods declared in class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    Modifier and Type
    Method
    Description
    protected Object
    Answers a new instance of the same class as the receiver, whose slots have been filled in with the values in the slots of the receiver.
    boolean
    Compares the argument to the receiver, and answers true if they represent the same object using a class specific comparison.
    protected void
    Deprecated, for removal: This API element is subject to removal in a future version.
    May cause performance issues, deadlocks and hangs.
    final Class<? extends Object>
    Answers the unique instance of java.lang.Class which represents the class of the receiver.
    int
    Answers an integer hash code for the receiver.
    final void
    Causes one thread which is waiting on the receiver to be made ready to run.
    final void
    Causes all threads which are waiting on the receiver to be made ready to run.
    Answers a string containing a concise, human-readable description of the receiver.
    final void
    Causes the thread which sent this message to be made not ready to run pending some change in the receiver (as indicated by notify or notifyAll).
    final void
    wait(long time)
    Causes the thread which sent this message to be made not ready to run either pending some change in the receiver (as indicated by notify or notifyAll) or the expiration of the timeout.
    final void
    wait(long time, int frac)
    Causes the thread which sent this message to be made not ready to run either pending some change in the receiver (as indicated by notify or notifyAll) or the expiration of the timeout.
  • Method Details

    • typeSwitch

      public static CallSite typeSwitch(MethodHandles.Lookup lookup, String invocationName, MethodType invocationType, Object... labels)
      Bootstrap method for linking an invokedynamic call site that implements a switch over a target value. The static arguments labels are an array of case labels which must be non-null and of type String, Integer, Class, or EnumDesc. In addition, when preview features are enabled, Long, Float, Double, and Boolean labels are also permitted.

      The type of the returned CallSite's method handle will have a return type of int. It has two parameters: the first argument will be a value of the (target) type and the second will be int (restart).

      If the target is null, then the method of the call site returns -1.

      If the target is not null, then the method of the call site returns the index of the first element in the labels array starting from the restart index matching one of the following conditions:

      • the element is of type Class that is assignable from the target's class
      • the element is of type String and equals to the target
      • the element is of type Integer and == to the target after unboxing if necessary
      • (Preview) the element is of type Long or Boolean and == to the target after unboxing if necessary
      • (Preview) the element is of type Float or Double and equals to the target after boxing if necessary
      • the element is of type EnumDesc, that describes an enum constant that is == to the target

      If no element in the labels array matches the target, then the method of the call site return the length of the labels array.

      The value of the restart index must be between 0 (inclusive) and the length of the labels array (inclusive), both or an IndexOutOfBoundsException is thrown.

      Parameters:
      lookup - the full-privilege lookup context of the caller
      invocationName - unused, null is permitted
      invocationType - The invocation type of the CallSite with two parameters, a target type, an int, and int as a return type.
      labels - case labels as described above
      Returns:
      a CallSite returning the first matching element as described above
      Throws:
      IllegalArgumentException - if any element in the labels array is null
      IllegalArgumentException - if the invocation type is not a method type of first parameter of a target type, second parameter of type int and with int as its return type
      IllegalArgumentException - if labels contains an element that is not of type String, Integer, Long, Float, Double, Boolean, Class or EnumDesc
      IllegalArgumentException - if preview features are disabled and if labels contains an element that is of type Long, Float, Double, or Boolean
      See Java Virtual Machine Specification:
      4.4.6 The CONSTANT_NameAndType_info Structure
      4.4.10 The CONSTANT_Dynamic_info and CONSTANT_InvokeDynamic_info Structures
    • enumSwitch

      public static CallSite enumSwitch(MethodHandles.Lookup lookup, String invocationName, MethodType invocationType, Object... labels)
      Bootstrap method for linking an invokedynamic call site that implements a switch on a target of an enum type. The static arguments are used to encode the case labels associated to the switch construct, where each label can be encoded in two ways:
      • as a String value, which represents the name of the enum constant associated with the label
      • as a Class value, which represents the enum type associated with a type test pattern

      The returned CallSite's method handle will have a return type of int and accepts two parameters: the first argument will be an Enum instance (target) and the second will be int (restart).

      If the target is null, then the method of the call site returns -1.

      If the target is not null, then the method of the call site returns the index of the first element in the labels array starting from the restart index matching one of the following conditions:

      • the element is of type Class that is assignable from the target's class; or
      • the element is of type String and equals to the target enum constant's Enum.name().

      If for a given target there is no element in the labels fulfilling one of the above conditions, then the method of the call site returns the length of the labels array.

      The value of the restart index must be between 0 (inclusive) and the length of the labels array (inclusive), or an IndexOutOfBoundsException is thrown.

      API Note:
      It is permissible for the labels array to contain String values that do not represent any enum constants at runtime.
      Parameters:
      lookup - the full-privilege lookup context of the caller
      invocationName - unused, null is permitted
      invocationType - The invocation type of the CallSite with two parameters, an enum type, an int, and int as a return type.
      labels - case labels - String constants and Class instances, in any combination
      Returns:
      a CallSite returning the first matching element as described above
      Throws:
      IllegalArgumentException - if any element in the labels array is null
      IllegalArgumentException - if any element in the labels array is an empty String
      IllegalArgumentException - if the invocation type is not a method type whose first parameter type is an enum type, second parameter of type int and whose return type is int
      IllegalArgumentException - if labels contains an element that is not of type String or Class equal to the target enum type
      See Java Virtual Machine Specification:
      4.4.6 The CONSTANT_NameAndType_info Structure
      4.4.10 The CONSTANT_Dynamic_info and CONSTANT_InvokeDynamic_info Structures