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 aswitchover a target value.Methods declared in class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitModifier and TypeMethodDescriptionprotected Objectclone()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.booleanCompares the argument to the receiver, and answers true if they represent the same object using a class specific comparison.protected voidfinalize()Deprecated, for removal: This API element is subject to removal in a future version.May cause performance issues, deadlocks and hangs.getClass()Answers the unique instance of java.lang.Class which represents the class of the receiver.inthashCode()Answers an integer hash code for the receiver.final voidnotify()Causes one thread which iswaiting on the receiver to be made ready to run.final voidCauses all threads which arewaiting on the receiver to be made ready to run.toString()Answers a string containing a concise, human-readable description of the receiver.final voidwait()Causes the thread which sent this message to be made not ready to run pending some change in the receiver (as indicated bynotifyornotifyAll).final voidwait(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 bynotifyornotifyAll) or the expiration of the timeout.final voidwait(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 bynotifyornotifyAll) 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 aninvokedynamiccall site that implements aswitchover a target value. The static argumentslabelsare an array of case labels which must be non-null and of typeString,Integer,Class, orEnumDesc. In addition, when preview features are enabled,Long,Float,Double, andBooleanlabels are also permitted.The type of the returned
CallSite's method handle will have a return type ofint. It has two parameters: the first argument will be a value of the (target) type 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 - 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
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- the full-privilege lookup context of the callerinvocationName- unused,nullis permittedinvocationType- 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 nullIllegalArgumentException- if the invocation type is not a method type of first parameter of a target type, second parameter of typeintand withintas its return typeIllegalArgumentException- iflabelscontains an element that is not of typeString,Integer,Long,Float,Double,Boolean,ClassorEnumDescIllegalArgumentException- 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
- 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- the full-privilege lookup context of the callerinvocationName- 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:
IllegalArgumentException- 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
-