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 CallSite
enumSwitch
(MethodHandles.Lookup lookup, String invocationName, MethodType invocationType, Object... labels) Bootstrap method for linking aninvokedynamic
call site that implements aswitch
on a target of an enum type.static CallSite
typeSwitch
(MethodHandles.Lookup lookup, String invocationName, MethodType invocationType, Object... labels) Bootstrap method for linking aninvokedynamic
call site that implements aswitch
on a target of a reference type.
-
Method Details
-
typeSwitch
public static CallSite typeSwitch(MethodHandles.Lookup lookup, String invocationName, MethodType invocationType, Object... labels) Bootstrap method for linking aninvokedynamic
call site that implements aswitch
on a target of a reference type. The static arguments are an array of case labels which must be non-null and of typeString
orInteger
orClass
orEnumDesc
.The type of the returned
CallSite
's method handle will have a return type ofint
. It has two parameters: the first argument will be anObject
instance (target
) and the second will beint
(restart
).If the
target
isnull
, then the method of the call site returns -1.If the
target
is notnull
, then the method of the call site returns the index of the first element in thelabels
array starting from therestart
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
orInteger
and 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
labels
array matches the target, then the method of the call site return the length of thelabels
array.The value of the
restart
index must be between0
(inclusive) and the length of thelabels
array (inclusive), both or anIndexOutOfBoundsException
is 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
- unusedinvocationType
- The invocation type of theCallSite
with two parameters, a reference type, anint
, andint
as a return type.labels
- case labels -String
andInteger
constants andClass
andEnumDesc
instances, in any combination- Returns:
- a
CallSite
returning the first matching element as described above - Throws:
NullPointerException
- if any argument isnull
IllegalArgumentException
- 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 typeint
and withint
as its return type,IllegalArgumentException
- iflabels
contains an element that is not of typeString
,Integer
,Long
,Float
,Double
,Boolean
,Class
orEnumDesc
.IllegalArgumentException
- iflabels
contains an element that is not of typeBoolean
whentarget
is 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 aninvokedynamic
call site that implements aswitch
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 ofint
and accepts two parameters: the first argument will be anEnum
instance (target
) and the second will beint
(restart
).If the
target
isnull
, then the method of the call site returns -1.If the
target
is notnull
, then the method of the call site returns the index of the first element in thelabels
array starting from therestart
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'sEnum.name()
.
If no element in the
labels
array matches the target, then the method of the call site return the length of thelabels
array.The value of the
restart
index must be between0
(inclusive) and the length of thelabels
array (inclusive), both or anIndexOutOfBoundsException
is 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
- unusedinvocationType
- The invocation type of theCallSite
with two parameters, an enum type, anint
, andint
as a return type.labels
- case labels -String
constants andClass
instances, in any combination- Returns:
- a
CallSite
returning the first matching element as described above - Throws:
NullPointerException
- if any argument isnull
IllegalArgumentException
- if any element in the labels array is null, if the invocation type is not a method type whose first parameter type is an enum type, second parameter of typeint
and whose return type isint
, or iflabels
contains an element that is not of typeString
orClass
of 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
-