I am looking at the code generated by MethodHandleNatives.linkMethod:

(note the output was edited for brevity)

$ cat HelloMH.java
import java.lang.invoke.*;

public class HelloMH {
    public static void main(String ...args) throws Throwable {
        MethodHandles.Lookup lookup = MethodHandles.lookup();
        MethodType mt = MethodType.methodType(void.class, float.class);
        MethodHandle mh = lookup.findStatic(HelloMH.class, "callme", mt);

    private static void callme(float x) {
        System.out.println("Hello MH.invoke: " + x);

$ javac HelloMH.java

$ java -Djava.lang.invoke.MethodHandle.DUMP_CLASS_FILES=true \
    -XX:+UnlockDiagnosticVMOptions -XX:+ShowHiddenFrames -cp . HelloMH
Hello MH.invoke: 4.0
java.lang.Exception: Stack trace
    at java.base/java.lang.Thread.dumpStack(Thread.java:1434)
    at HelloMH.callme(HelloMH.java:13)
    at java.base/java.lang.invoke.LambdaForm$DM.invokeStatic_LF_V()
    at java.base/java.lang.invoke.LambdaForm$MH.invokeExact_MT_LFL_V()
    at HelloMH.main(HelloMH.java:8)

$ javap -c 'DUMP_CLASS_FILES/java/lang/invoke/LambdaForm$MH.class'
final class java.lang.invoke.LambdaForm$MH {
  static void invokeExact_MT000_LFL_V(java.lang.Object, float, java.lang.Object);
       0: aload_0
       1: checkcast     #14    // class java/lang/invoke/MethodHandle
       4: dup

Why is the checkcast necessary here?

I thought the verifier would ensure that local#0 must of type MethodHandle.

Is this checkcast needed only for reflective invocation of MethodHandle.invokeExact()?

- Ioi

mlvm-dev mailing list

Reply via email to