explicitCast
Applies a conversion from a source type to a destination type. Given a destination type dstType and an input value value , one of the following will happen:
- If
dstType is void.class , a ClassCastException is thrown.
- If
dstType is Object.class , value is returned as is.
Otherwise one of the following conversions is applied to value :
- If
dstType is a reference type, a reference cast is applied to value as if by calling dstType.cast(value) .
- If
dstType is a primitive type, then, if the runtime type of value is a primitive wrapper type (such as Integer ), a Java unboxing conversion is applied 5.1.8 followed by a Java casting conversion 5.5 converting either directly to dstType , or, if dstType is boolean , to int , which is then converted to either true or false depending on whether the least-significant-bit is 1 or 0 respectively. If the runtime type of value is not a primitive wrapper type a ClassCastException is thrown.
The result is the same as when using the following code:
MethodHandle id = MethodHandles.identity(dstType);
MethodType mt = MethodType.methodType(dstType, Object.class);
MethodHandle conv = MethodHandles.explicitCastArguments(id, mt);
return conv.invoke(value);
- Parameters:
-
lookup - unused
-
name - unused
-
dstType - the destination type of the conversion
-
value - the value to be converted
- Returns:
- the converted value
- Throws:
-
ClassCastException - when dstType is void , when a cast per (1) fails, or when dstType is a primitive type and the runtime type of value is not a primitive wrapper type (such as Integer )
- Since:
- 15
|
|