Class SwitchBootstraps
public final class SwitchBootstraps extends Object
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 |
Bootstrap method for linking an invokedynamic call site that implements a switch on a target of an enum type. |
static CallSite |
typeSwitch |
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 |
clone() |
Creates and returns a copy of this object. |
boolean |
equals |
Indicates whether some other object is "equal to" this one. |
protected void |
finalize() |
Deprecated, for removal: This API element is subject to removal in a future version. Finalization is deprecated and subject to removal in a future release. |
final Class |
getClass() |
Returns the runtime class of this Object. |
int |
hashCode() |
Returns a hash code value for this object. |
final void |
notify() |
Wakes up a single thread that is waiting on this object's monitor. |
final void |
notifyAll() |
Wakes up all threads that are waiting on this object's monitor. |
String |
toString() |
Returns a string representation of the object. |
final void |
wait() |
Causes the current thread to wait until it is awakened, typically by being notified or interrupted. |
final void |
wait |
Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed. |
final void |
wait |
Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed. |
Method Details
typeSwitch
public static CallSite typeSwitch(MethodHandles.Lookup lookup, String invocationName, MethodType invocationType, Object... labels)
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
Classthat is assignable from the target's class - the element is of type
Stringandequalsto the target - the element is of type
Integerand==to the target after unboxing if necessary - (Preview) the element is of type
LongorBooleanand==to the target after unboxing if necessary - (Preview) the element is of type
FloatorDoubleandequalsto 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,nullis permitted -
invocationType- The invocation type of theCallSitewith two parameters, a target type, anint, andintas a return type. -
labels- case labels as described above - Returns:
- a
CallSitereturning 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 typeintand withintas its return type -
IllegalArgumentException- iflabelscontains an element that is not of typeString,Integer,Long,Float,Double,Boolean,ClassorEnumDesc -
IllegalArgumentException- if preview features are disabled and iflabelscontains an element that is of typeLong,Float,Double, orBoolean - 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)
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
Stringvalue, which represents the name of the enum constant associated with the label - as a
Classvalue, 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
Classthat is assignable from the target's class; or - the element is of type
Stringand equals to the target enum constant'sEnum.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
labelsarray to containStringvalues that do not represent any enum constants at runtime. - Parameters:
-
lookup- the full-privilege lookup context of the caller -
invocationName- unused,nullis permitted -
invocationType- The invocation type of theCallSitewith two parameters, an enum type, anint, andintas a return type. -
labels- case labels -Stringconstants andClassinstances, in any combination - Returns:
- a
CallSitereturning 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 emptyString -
IllegalArgumentException- if the invocation type is not a method type whose first parameter type is an enum type, second parameter of typeintand whose return type isint -
IllegalArgumentException- iflabelscontains an element that is not of typeStringorClassequal 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
© 1993, 2025, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.