When invokedynamic instruction or MethodHandles.convertArguments() needs to convert between primary types, an exception like the one below is thrown:
Exception in thread "main" java.lang.ClassCastException: java.lang.Boolean cannot be cast to java.lang.Byte
at sun.dyn.util.ValueConversions.unboxByte(ValueConversions.java:61)
at sun.dyn.util.ValueConversions.unboxByteRaw(ValueConversions.java:98)
at sun.dyn.ToGeneric$Adapter.return_I(ToGeneric.java:393)
at sun.dyn.ToGeneric$A1.invoke_I(ToGeneric.java:643)
A short test case for this:
import java.dyn.*;
public class R {
public static void main(String... args) throws Throwable {
MethodHandle mh1 = MethodHandles.identity(int.class);
MethodHandle mh2 = MethodHandles.convertArguments(mh1, MethodType.methodType(short.class, int.class));
short s = (short) mh2.invokeExact(123);
}
}