Class SwitchBootstraps
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 TypeMethodDescriptionstatic CallSiteenumSwitch(MethodHandles.Lookup lookup, String invocationName, MethodType invocationType, Object... labels) Bootstrap method for linking aninvokedynamiccall site that implements aswitchon a target of an enum type.static CallSitetypeSwitch(MethodHandles.Lookup lookup, String invocationName, MethodType invocationType, Object... labels) Bootstrap method for linking aninvokedynamiccall site that implements aswitchon a target of a reference type.Methods declared in class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitModifier and TypeMethodDescriptionprotected Objectclone()Creates and returns a copy of this object.booleanIndicates whether some other object is "equal to" this one.protected voidfinalize()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 thisObject.inthashCode()Returns a hash code value for this object.final voidnotify()Wakes up a single thread that is waiting on this object's monitor.final voidWakes up all threads that are waiting on this object's monitor.toString()Returns a string representation of the object.final voidwait()Causes the current thread to wait until it is awakened, typically by being notified or interrupted.final voidwait(long timeoutMillis) 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 voidwait(long timeoutMillis, int nanos) 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) Bootstrap method for linking aninvokedynamiccall site that implements aswitchon a target of a reference type. The static arguments are an array of case labels which must be non-null and of typeStringorIntegerorClassorEnumDesc.The type of the returned
CallSite's method handle will have a return type ofint. It has two parameters: the first argument will be anObjectinstance (target) and the second will beint(restart).If the
targetisnull, then the method of the call site returns -1.If the
targetis notnull, then the method of the call site returns the index of the first element in thelabelsarray starting from therestartindex 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
StringorIntegerand equals to the target. - the element is of type
EnumDesc, that describes a constant that is equals to the target.
If no element in the
labelsarray matches the target, then the method of the call site return the length of thelabelsarray.The value of the
restartindex must be between0(inclusive) and the length of thelabelsarray (inclusive), both or anIndexOutOfBoundsExceptionis thrown.- Parameters:
lookup- Represents a lookup context with the accessibility privileges of the caller. When used withinvokedynamic, this is stacked automatically by the VM.invocationName- unused,nullis permittedinvocationType- The invocation type of theCallSitewith two parameters, a reference type, anint, andintas a return type.labels- case labels -StringandIntegerconstants andClassandEnumDescinstances, in any combination- Returns:
- a
CallSitereturning the first matching element as described above - Throws:
NullPointerException- if any argument isnull, unless noted otherwiseIllegalArgumentException- if any element in the labels array is nullIllegalArgumentException- if the invocation type is not a method type of first parameter of a reference type, second parameter of typeintand withintas its return typeIllegalArgumentException- iflabelscontains an element that is not of typeString,Integer,Long,Float,Double,Boolean,ClassorEnumDescIllegalArgumentException- iflabelscontains an element that is not of typeBooleanwhentargetis aBoolean.class- 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
- the element is of type
-
enumSwitch
public static CallSite enumSwitch(MethodHandles.Lookup lookup, String invocationName, MethodType invocationType, Object... labels) Bootstrap method for linking aninvokedynamiccall site that implements aswitchon 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 ofintand accepts two parameters: the first argument will be anEnuminstance (target) and the second will beint(restart).If the
targetisnull, then the method of the call site returns -1.If the
targetis notnull, then the method of the call site returns the index of the first element in thelabelsarray starting from therestartindex 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
targetthere is no element in thelabelsfulfilling one of the above conditions, then the method of the call site returns the length of thelabelsarray.The value of the
restartindex must be between0(inclusive) and the length of thelabelsarray (inclusive), or anIndexOutOfBoundsExceptionis thrown.- API Note:
- It is permissible for the
labelsarray to containStringvalues that do not represent any enum constants at runtime. - Parameters:
lookup- Represents a lookup context with the accessibility privileges of the caller. When used withinvokedynamic, this is stacked automatically by the VM.invocationName- unused,nullis permittedinvocationType- 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:
NullPointerException- if any argument isnull, unless noted otherwiseIllegalArgumentException- if any element in the labels array is nullIllegalArgumentException- if any element in the labels array is an emptyStringIllegalArgumentException- if the invocation type is not a method type whose first parameter type is an enum type, second parameter of typeintand whose return type isintIllegalArgumentException- 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
- as a
-