MethodHandle InvokeExact 参数

MethodHandle InvokeExact parameter

我对方法句柄的方法参数感到困惑。我先建一个guardwithtest方法句柄如下图:

public class App 
{
    public static void trueTarget(String str, String own, String t){
        System.out.println("This is true target "+str+" "+own + " "+t);
    }

    public static void falseTarget(String str, String own, String t){
        System.out.println("This is false target " + str+"  "+own +" "+t);
    }

    public static void main( String[] args ) throws Throwable
    {
        MethodHandle test = MethodHandles.publicLookup().findVirtual(String.class, "startsWith", 
                MethodType.methodType(boolean.class, String.class));

        System.out.println((boolean)test.invokeExact("result", "res"));

        MethodHandle target = MethodHandles.lookup().findStatic(App.class, "trueTarget", MethodType.methodType(void.class, String.class, String.class, String.class));
        MethodHandle fallback = MethodHandles.lookup().findStatic(App.class, "falseTarget", MethodType.methodType(void.class, String.class, String.class, String.class));

        MethodHandle gwd = MethodHandles.guardWithTest(test, target, fallback);

        gwd.invokeExact("result", "data", "sijie");

    }
}

我的问题是如何将参数传递给三个方法句柄:test、trueTarget 和 faliover。 1、invokeExact的第一个参数"result"作为receiver传给了test guard,第二个参数"data"传给了startWith:

      String.startsWith(String)
      "result"          "data"  

但这三个参数传递给 falseTarget 为:

      falseTarget(String str, String own, String t)
                        "result"      "data"     "sijie"

那么,参数传递的规则是什么,它们如何匹配方法句柄引用的方法?

这出现在 findVirtual

的 Javadoc 中

When called, the handle will treat the first argument as a receiver and dispatch on the receiver's type to determine which method implementation to enter.

跟你描述的完全一样。 static 方法没有接收者,因此 invokeExact 的所有参数都被视为该方法的参数。